Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Carl Meyer 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: 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: 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:102738 This is an OpenPGP/MIME signed message (RFC 4880 and 3156) --VGgOO1RwpiFLT76SjUVBxnweDo9tUuk2t Content-Type: text/plain; charset=windows-1252 Content-Transfer-Encoding: quoted-printable 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. >=20 > Scenario : > Web application developed at /some/dir/sites/flask/ >=20 > If I have a package - let us call it app and in my > /some/dir/sites/flask/app/__init__.py is the following: >=20 > from config import config >=20 > imports the config dictionary from /some/dir/sites/flask/config.py >=20 > (the real-case scenario is M. Grinberg's tutorial on Flask). >=20 > What if I wanted to add a module in the app package and call it from > __init__.py >=20 > 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 >=20 > 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 --VGgOO1RwpiFLT76SjUVBxnweDo9tUuk2t Content-Type: application/pgp-signature; name="signature.asc" Content-Description: OpenPGP digital signature Content-Disposition: attachment; filename="signature.asc" -----BEGIN PGP SIGNATURE----- Version: GnuPG v2.0.22 (GNU/Linux) iQIcBAEBCAAGBQJWungNAAoJEC0ft5FqUuEh+ZMQAJRNYAcA8VUUEPDNe5GRj+25 csAJCcWimxHXTWIRtOuiwoaLg16DSU6vkllxm4DeSjy5XuNxqrYli+Mh8+T7YSh2 bREt4iu7ftq1jzEtDbA0zjqnQ6KZd78mKWwxZKkuVcFkQg14nUPX8+1yUI7+8SiJ korWQpLukRgwwl6oW0+YW7BnR1Z7RBKvCAGbetduSSjfSkYmvLHFKfljp84EAD/P jD7x4erb6KIGsmiIw/IQ8G/OGiuGsFPBlcWA4idFx4HoWD74dzk0W5FcjnLEYg8d dxBswlUhvYmnKdl3bnLYjAiTASgzc8G03YjyNiT7Ab8+y1sMukSNJiZkTLkKP7A9 4lt2pPeR5tGYv4P3qDBk97lzBf5KeWDujuCdUcfU1tKBuvNuiHpH0jOhxH6XiBUq 1wFRlkGM6kTLF3WOd895LEGgOamJHGbChuislP9mu40Y3/ibug3LM866MrWb9Xtj rC4I8Kjnb4vL3YZirRddFpNkLedW/+yM9pjgjbXBb7KSwfBjNd/FbnJ1rBHkASXc dQ5/tm8A5ffSsWydeAnjstgcrF3JfntzdjMBM/tugiXI3VDq3ARmqIqvLaVBt96G 9ae6xDiR7EeduTo/sROYqKmQSUFVYf0ROdSS7mLI2HE9dMQwWzzAhdUZx87OZRCE 4hr/C/uD8Yx1G76MxvsN =M86D -----END PGP SIGNATURE----- --VGgOO1RwpiFLT76SjUVBxnweDo9tUuk2t--