Relative Package Import

R

Robert Hancock

mypackage/
__init__.py
push/
__init__.py
dest.py
feed/
__init__py
subject.py

In subject.py I have
from ..push import dest

But i receive the error:
Caught exception importing module subject:
File "/usr/local/python/lib/python2.5/site-packages/pychecker/
checker.py", line 621, in setupMainCode()
module = imp.load_module(self.moduleName, file, filename, smt)
File "subject.py", line 1, in <module>()
from ..feed import dest
ValueError: Attempted relative import in non-package

What am I missing?
 
R

Remy Blank

Robert said:
mypackage/
__init__.py
push/
__init__.py
dest.py
feed/
__init__py
^
Missing dot here? -----------|
In subject.py I have
from ..push import dest

But i receive the error:
Caught exception importing module subject:
File "/usr/local/python/lib/python2.5/site-packages/pychecker/
checker.py", line 621, in setupMainCode()
module = imp.load_module(self.moduleName, file, filename, smt)
File "subject.py", line 1, in <module>()
from ..feed import dest

This last line contradicts your statement above...

-- Remy
 
P

Peter Otten

Robert said:
mypackage/
__init__.py
push/
__init__.py
dest.py
feed/
__init__py
subject.py

In subject.py I have
from ..push import dest

But i receive the error:
Caught exception importing module subject:
File "/usr/local/python/lib/python2.5/site-packages/pychecker/
checker.py", line 621, in setupMainCode()
module = imp.load_module(self.moduleName, file, filename, smt)
File "subject.py", line 1, in <module>()
from ..feed import dest
ValueError: Attempted relative import in non-package

What am I missing?

When you run subject as a file

pychecker mypackage/feed/subject.py

subject.py is regarded as a standalone script, not as part of a package. Try

pychecker mypackage.feed.subject

instead to invoke the module via the standard import mechanism.

Peter
 
K

Kay Schluehr

When you run subject as a file

pychecker mypackage/feed/subject.py

subject.py is regarded as a standalone script, not as part of a package. Try

pychecker mypackage.feed.subject

instead to invoke the module via the standard import mechanism.

Peter

Since when are Python modules not regarded as standalone scripts but
as a "part of a package"?

The only indication that there is some duality and that it is relevant
at all are those relative imports that came in with Python 2.5. As it
seems this feature is justified by the artificial semantic differences
it produces.
 
P

Peter Otten

Kay said:
Since when are Python modules not regarded as standalone scripts but
as a "part of a package"?

The only indication that there is some duality and that it is relevant
at all are those relative imports that came in with Python 2.5. As it
seems this feature is justified by the artificial semantic differences
it produces.

I'm probably misunderstanding you, but the ambiguity I wanted to point out
has existed before and has nothing artificial:

$ tree
..
`-- mypackage
|-- __init__.py
|-- feed
| |-- __init__.py
| |-- subject.py
| `-- subject.py~
`-- push
|-- __init__.py
`-- dest.py

3 directories, 6 files
$ python2.4 -c "import mypackage.push.dest as d; print d.__name__"
mypackage.push.dest
$ cd mypackage/push/
$ python2.4 -c "import dest as d; print d.__name__"
dest

".." in the import goes one up in the package name, not in the filesystem
and therefore won't work in the second example.

Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top