473,395 Members | 1,496 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Relative Package Import

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?

Jul 8 '08 #1
7 2771
Robert Hancock wrote:
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

Jul 8 '08 #2
Robert Hancock wrote:
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
Jul 8 '08 #3
Robert Hancock wrote:
mypackage/
__init__.py
push/
__init__.py
dest.py
feed/
__init__py
subject.py

In subject.py I have
from ..push import dest
There is no such thing as relative package imports. See
http://www.python.org/doc/essays/packages.html

Thomas
>
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?
Jul 9 '08 #4
Thomas wrote:
Robert Hancock wrote:
>mypackage/
__init__.py
push/
__init__.py
dest.py
feed/
__init__py
subject.py

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

There is no such thing as relative package imports. See
http://www.python.org/doc/essays/packages.html
Unless you are using Python 1.5 the following document is a bit more
relevant:

http://www.python.org/dev/peps/pep-0328/

Peter

Jul 9 '08 #5
On 8 Jul., 21:09, Peter Otten <__pete...@web.dewrote:
Robert Hancock wrote:
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
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.
Jul 9 '08 #6
Kay Schluehr wrote:
On 8 Jul., 21:09, Peter Otten <__pete...@web.dewrote:
>Robert Hancock wrote:
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

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

Jul 10 '08 #7
Peter Otten wrote:
Thomas wrote:
>Robert Hancock wrote:
>>mypackage/
__init__.py
push/
__init__.py
dest.py
feed/
__init__py
subject.py

In subject.py I have
from ..push import dest
There is no such thing as relative package imports. See
http://www.python.org/doc/essays/packages.html

Unless you are using Python 1.5 the following document is a bit more
relevant:

http://www.python.org/dev/peps/pep-0328/
Oups, thanks for the pointer. I thought the old doc was still valid...
(gee, there should be forward references).

Thomas
Jul 10 '08 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Anders J. Munch | last post by:
Now 2.5 is out, and we have a syntax for explicit relative imports (PEP 328, http://www.python.org/dev/peps/pep-0328/, in case anyone wasn't paying attention). The long-term plan is that the...
1
by: Alexey Borzenkov | last post by:
After reading PEP-0328 I wanted to give relative imports a try: # somepkg/__init__.py <empty> # somepkg/test1.py from __future__ import absolute_import from . import test2 if __name__ ==...
12
by: Alan Isaac | last post by:
Are relative imports broken in 2.5? Directory ``temp`` contains:: __init__.py test1.py test2.py File contents: __init__.py and test2.py are empty test1.py contains a single line::
0
by: Alan Isaac | last post by:
What is the recommended packaging of demo scripts or test scripts for a package that has modules that use relative imports? Example: Suppose I have the package structure: package/...
1
by: Pat O'Hara | last post by:
Hey guys, I know this is a really stupid question, but I've tried googling and nothing came up. I also tried IRC, but it was too crowded and I didn't get much useful information. I'm using...
9
by: rbygscrsepda | last post by:
Hi, I'm a newbie at Python. :) Right now it's not letting me import * from any relative package name--i.e., a name that starts with a dot. For instance, none of the following work: from . import...
0
by: Kay Schluehr | last post by:
Since their introduction in Python 2.5 I only reviewed the new "relative import" notation briefly by reading the "What's new in Python 2.5" article. Now I wanted checkout if I get comfortable with...
4
by: DG | last post by:
Alright, I have searched and searched and read many conversations on the topic of relative and absolute imports and am still not getting the whole thing through my skull. Highlights of what I've...
0
by: Gabriel Genellina | last post by:
En Sat, 18 Oct 2008 05:52:04 -0300, Stef Mientki <stef.mientki@gmail.com> escribió: Why don't you let the caller tell you its own location, using __file__? The above code is too much magic for...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.