Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #102738

Re: Importing two modules of same name

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Carl Meyer <carl@oddbird.net>
Newsgroups comp.lang.python
Subject Re: Importing two modules of same name
Date Tue, 9 Feb 2016 16:36:41 -0700
Lines 74
Message-ID <mailman.2.1455063648.7749.python-list@python.org> (permalink)
References <20160209232303.GC2583@mail.akwebsoft.com>
Mime-Version 1.0
Content-Type multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="VGgOO1RwpiFLT76SjUVBxnweDo9tUuk2t"
X-Trace news.uni-berlin.de oxvLv9UJQ7kcvtOR2wkTUgjO6VQH9lu4aRVZCa/ThyVQ==
Return-Path <carl@oddbird.net>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'omit': 0.07; 'subject:two': 0.07; '__future__': 0.09; 'imports': 0.09; 'satisfy': 0.09; 'subject:modules': 0.09; 'subject:same': 0.09; 'python': 0.10; '2.7': 0.13; ':))': 0.16; '__init__.py': 0.16; 'filename:fname piece:signature': 0.16; 'from:addr:carl': 0.16; 'practices.': 0.16; 'received:173.255': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'tim,': 0.16; 'wrote:': 0.16; 'app': 0.16; 'config': 0.18; 'project,': 0.18; '(the': 0.22; 'default,': 0.22; 'import': 0.24; 'tim': 0.24; 'header:In-Reply-To:1': 0.24; 'module': 0.25; 'header:User-Agent:1': 0.26; '(maybe': 0.29; 'dictionary': 0.29; 'tutorial': 0.29; 'realize': 0.32; 'problem': 0.33; 'skip:/ 20': 0.33; 'file': 0.34; 'this?': 0.34; 'add': 0.34; 'there': 0.36; 'modules': 0.36; 'to:addr:python-list': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'two': 0.37; 'received:org': 0.37; 'johnson': 0.37; 'wanted': 0.37; 'application': 0.39; 'to:addr:python.org': 0.40; 'your': 0.60; 'behavior': 0.61; 'charset:windows-1252': 0.62; 'developed': 0.66; 'past.': 0.66; 'skip:/ 30': 0.84
X-Enigmail-Draft-Status N1110
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.5.1
In-Reply-To <20160209232303.GC2583@mail.akwebsoft.com>
X-Spam-Status No (score -1.0): ALL_TRUSTED=-1, URIBL_BLOCKED=0.001
X-Spam-Bar -
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:102738

Show key headers only | View raw


[Multipart message — attachments visible in raw view] - view raw

Hi Tim,

On 02/09/2016 04:23 PM, Tim Johnson wrote:
> Before proceding, let me state that this is to satisfy my
> curiousity, not to solve any problem I am having.
> 
> Scenario :
> Web application developed at /some/dir/sites/flask/
> 
> If I have a package - let us call it app and in my
> /some/dir/sites/flask/app/__init__.py is the following:
> 
> from config import config
> 
> imports the config dictionary from  /some/dir/sites/flask/config.py
> 
> (the real-case scenario is M. Grinberg's tutorial on Flask).
> 
> What if I wanted to add a module in the app package and call it from
> __init__.py
> 
> That entails having two modules name config
> one at /some/dir/sites/flask/config.py
> and the other at /some/dir/sites/flask/app/config.py
> 
> What would be the proper way to do this? (If proper at all :)) I
> realize that it may not be best practices. And is a practice that I
> avoided in the past.

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.

Carl

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Importing two modules of same name Carl Meyer <carl@oddbird.net> - 2016-02-09 16:36 -0700

csiph-web