Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #102741
| Path | csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail |
|---|---|
| From | Tim Johnson <tim@akwebsoft.com> |
| Newsgroups | comp.lang.python |
| Subject | Re: Importing two modules of same name |
| Date | Tue, 9 Feb 2016 16:03:46 -0900 |
| Organization | AkWebsoft |
| Lines | 59 |
| Message-ID | <mailman.5.1455066229.7749.python-list@python.org> (permalink) |
| References | <20160209232303.GC2583@mail.akwebsoft.com> <56BA7809.1000206@oddbird.net> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=us-ascii |
| X-Trace | news.uni-berlin.de +Z9gaQIScp6gXprgTMhmkQy0a9Iyrc+1bbuHQf4U8RqQ== |
| Return-Path | <tim@akwebsoft.com> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.000 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'bootstrap': 0.07; 'constructor': 0.07; 'omit': 0.07; 'subject:two': 0.07; '.main': 0.09; '__future__': 0.09; 'deemed': 0.09; 'skip:# 60': 0.09; 'subject:modules': 0.09; 'subject:same': 0.09; 'python': 0.10; '2.7': 0.13; 'def': 0.13; 'commented': 0.16; 'necessary).': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'sqlalchemy': 0.16; 'tim,': 0.16; 'app': 0.16; 'config': 0.18; 'project,': 0.18; 'default,': 0.22; 'import': 0.24; 'cheers': 0.24; 'tim': 0.24; 'header:In-Reply-To:1': 0.24; 'header:User-Agent:1': 0.26; '(maybe': 0.29; "i'm": 0.30; 'code': 0.30; 'avoiding': 0.33; 'file': 0.34; 'skip:c 30': 0.35; 'comment': 0.35; 'there': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'setting': 0.37; 'thanks': 0.37; 'charset:us-ascii': 0.37; 'to:addr:python.org': 0.40; 'some': 0.40; 'your': 0.60; 'behavior': 0.61; 'further': 0.62; 'skip:a 40': 0.64; 'collision': 0.84; 'meyer': 0.84 |
| Mail-Followup-To | python-list@python.org |
| Content-Disposition | inline |
| In-Reply-To | <56BA7809.1000206@oddbird.net> |
| User-Agent | Mutt/1.5.21 (2010-09-15) |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.21rc2 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Xref | csiph.com comp.lang.python:102741 |
Show key headers only | View raw
* Carl Meyer <carl@oddbird.net> [160209 15:28]:
> Hi Tim,
<...>
> The proper way to do this in Python 2.7 is to place `from __future__
> import absolute_import` at the top of flask/app/__init__.py (maybe best
> at the top of every Python file in your project, to keep the behavior
> consistent). Once you have that future-import, `import config` will
> always import the top-level config.py. To import the "local" config.py,
> you'd either `from . import config` or `import app.config`.
>
> Python 3 behaves this way without the need for a future-import.
>
> If you omit the future-import in Python 2.7, `import config` will import
> the neighboring app/config.py by default, and there is no way to import
> the top-level config.py.
Thanks for setting me straight Carl.
I'm including the full package constructor (app/__init__.py) code
for other's edification and further comment (if deemed necessary).
Some commented annotation added
##################################################################
from __future__ import absolute_import
from flask import Flask
from flask.ext.bootstrap import Bootstrap
from flask.ext.mail import Mail
from flask.ext.moment import Moment
from flask.ext.sqlalchemy import SQLAlchemy
# Import top-level config
from config import config
# Import same-level config avoiding name collision
from . import config as cfg
bootstrap = Bootstrap()
mail = Mail()
moment = Moment()
db = SQLAlchemy()
def create_app(config_name):
app = Flask(__name__)
app.config.from_object(config[config_name])
config[config_name].init_app(app)
bootstrap.init_app(app)
mail.init_app(app)
moment.init_app(app)
db.init_app(app)
from .main import main as main_blueprint
app.register_blueprint(main_blueprint)
return app
Cheers
--
Tim
http://www.akwebsoft.com, http://www.tj49.com
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: Importing two modules of same name Tim Johnson <tim@akwebsoft.com> - 2016-02-09 16:03 -0900
csiph-web