By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,100 Members | 2,979 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,100 IT Pros & Developers. It's quick & easy.

question about module resolution

P: n/a
Dear Experts,

I often find myself wanting to have a child module get some parameters
defined in a parent module. For example, imagine I have the following
directory structure and want something in baz.py to look at a value in
config.py. I end up putting in things like import sys;
sys.path.append('../..'). Is there a better way?

foo/
__init__.py
config.py
bar/
__init__.py
baz.py

Jan 17 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Emin wrote:
I often find myself wanting to have a child module get some parameters
defined in a parent module. For example, imagine I have the following
directory structure and want something in baz.py to look at a value in
config.py. I end up putting in things like import sys;
sys.path.append('../..'). Is there a better way?

foo/
__init__.py
config.py
bar/
__init__.py
baz.py
from __future__ import absolute_import
from .. import config

Peter
Jan 17 '07 #2

P: n/a
I put the lines you suggested in baz.py, but got an error:

Python 2.5 (r25:51908, Sep 19 2006, 09:52:17) [MSC v.1310 32 bit
(Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>import baz
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "baz.py", line 3, in <module>
from .. import config
ValueError: Attempted relative import in non-package

On Jan 17, 11:20 am, Peter Otten <__pete...@web.dewrote:
Emin wrote:
I often find myself wanting to have a child module get some parameters
defined in a parent module. For example, imagine I have the following
directory structure and want something in baz.py to look at a value in
config.py. I end up putting in things like import sys;
sys.path.append('../..'). Is there a better way?
foo/
__init__.py
config.py
bar/
__init__.py
baz.pyfrom __future__ import absolute_import
from .. import config

Peter
Jan 17 '07 #3

P: n/a
Emin wrote:
On Jan 17, 11:20 am, Peter Otten <__pete...@web.dewrote:
>Emin wrote:
I often find myself wanting to have a child module get some parameters
defined in a parent module. For example, imagine I have the following
directory structure and want something in baz.py to look at a value in
config.py. I end up putting in things like import sys;
sys.path.append('../..'). Is there a better way?
foo/
__init__.py
config.py
bar/
__init__.py
baz.py
>from __future__ import absolute_import
from .. import config
I put the lines you suggested in baz.py, but got an error:

Python 2.5 (r25:51908, Sep 19 2006, 09:52:17) [MSC v.1310 32 bit
(Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>import baz
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "baz.py", line 3, in <module>
from .. import config
ValueError: Attempted relative import in non-package
If you put foo/bar in the path and then

import baz

how do you think that baz is to know that it's actually foo.bar.baz?
Make sure that the parent directory of foo is in sys.path (e. g. by starting
the interpreter in that directory) and then import baz using its official
name.

$ tree
..
`-- foo
|-- __init__.py
|-- bar
| |-- __init__.py
| `-- baz.py
`-- config.py

2 directories, 4 files

$ cat foo/config.py
print "importing config"

$ cat foo/bar/baz.py
from __future__ import absolute_import
from .. import config

$ python2.5
Python 2.5 (r25:51908, Oct 3 2006, 08:48:09)
[GCC 3.3.3 (SuSE Linux)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>import foo.bar.baz
importing config

Peter

Jan 18 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.