Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > issue on internal import in a package

Reply
Thread Tools

issue on internal import in a package

 
 
人言落日是天涯,望极天涯不见家
Guest
Posts: n/a
 
      02-27-2011
Here is a simple example:
[app]
[module]
__init__.py --> empty
a.py --> import b
b.py --> defined a function foo()
test.py

In the test.py, contains the below statement:
from module import a
Execute the test.py will get error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "module\a.py", line 1, in <module>
import b
ImportError: No module named b

Why the b.py can not be found by a.py?
 
Reply With Quote
 
 
 
 
人言落日是天涯,望极天涯不见家
Guest
Posts: n/a
 
      02-27-2011
On Feb 27, 8:11*pm, 人言落日是天涯,望极天涯不见家 <(E-Mail Removed)> wrote:
> Here is a simple example:
> [app]
> * * * [module]
> * * * * * * __init__.py * --> empty
> * * * * * * a.py * --> import b
> * * * * * * b.py *--> defined a function foo()
> * * * test.py
>
> In the test.py, contains the below statement:
> from module import a
> Execute the test.py will get error:
> Traceback (most recent call last):
> * File "<stdin>", line 1, in <module>
> * File "module\a.py", line 1, in <module>
> * * import b
> ImportError: No module named b
>
> Why the b.py can not be found by a.py?


PS. This issue occurred on Python3.2
It's okay in the Python2.6 & Python2.5
 
Reply With Quote
 
 
 
 
人言落日是天涯,望极天涯不见家
Guest
Posts: n/a
 
      02-27-2011
On Feb 27, 8:40*pm, Ben Finney <(E-Mail Removed)> wrote:
> 人言落日是天涯,望极天涯不见家 <(E-Mail Removed)> writes:
> > Here is a simple example:
> > [app]
> > * * * [module]
> > * * * * * * __init__.py * --> empty
> > * * * * * * a.py * --> import b
> > * * * * * * b.py *--> defined a function foo()
> > * * * test.py

>
> > In the test.py, contains the below statement:
> > from module import a
> > Execute the test.py will get error:

>
> This works fine for me::
>
> * * $ mkdir --parents app/module/
> * * $ touch app/module/__init__.py
> * * $ printf "import b\n" > app/module/a.py
> * * $ printf "def foo(): pass\n" > app/module/b.py
> * * $ printf "from module import a\n" > app/test.py
> * * $ find .
> * * .
> * * ./app
> * * ./app/module
> * * ./app/module/__init__.py
> * * ./app/module/a.py
> * * ./app/module/b.py
> * * ./app/test.py
>
> * * $ python app/test.py
>
> > Traceback (most recent call last):
> > * File "<stdin>", line 1, in <module>
> > * File "module\a.py", line 1, in <module>
> > * * import b
> > ImportError: No module named b

>
> > Why the b.py can not be found by a.py?

>
> I get no errors; the code appears to run fine. Perhaps the scenario is
> not exactly as you describe?
>
> --
> *\ * * * “If we listen only to those who are like us, we will squander |
> * `\ * the great opportunity before us: To live together peacefully in |
> _o__) * * * * * *a world of unresolved differences.” —David Weinberger |
> Ben Finney


Thanks for your reply.
What's the version of your Python? My version is 3.2.
Python 2.5/2.6 doesn't have this issue.
 
Reply With Quote
 
Frank Millman
Guest
Posts: n/a
 
      02-27-2011

"Ben Finney" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 人言落日是天涯,望极天涯不见家 <(E-Mail Removed)> writes:
>
>> Here is a simple example:
>> [app]
>> [module]
>> __init__.py --> empty
>> a.py --> import b
>> b.py --> defined a function foo()
>> test.py
>>
>> In the test.py, contains the below statement:
>> from module import a
>> Execute the test.py will get error:

>
> This works fine for me::
>
> $ mkdir --parents app/module/
> $ touch app/module/__init__.py
> $ printf "import b\n" > app/module/a.py
> $ printf "def foo(): pass\n" > app/module/b.py
> $ printf "from module import a\n" > app/test.py
> $ find .
> .
> ./app
> ./app/module
> ./app/module/__init__.py
> ./app/module/a.py
> ./app/module/b.py
> ./app/test.py
>
> $ python app/test.py
>
>> Traceback (most recent call last):
>> File "<stdin>", line 1, in <module>
>> File "module\a.py", line 1, in <module>
>> import b
>> ImportError: No module named b
>>
>> Why the b.py can not be found by a.py?

>
> I get no errors; the code appears to run fine. Perhaps the scenario is
> not exactly as you describe?
>


I get exactly the same result as the OP, using python 3.2 on both windows
and linux. It works using python 2.6.

I can fix it by changing a.py from 'import b' to 'from . import b'.

As I understand it, the reason is that python 3.x will no longer look for an
absolute import in the current package - it will only look in sys.path.

Frank Millman


 
Reply With Quote
 
人言落日是天涯,望极天涯不见家
Guest
Posts: n/a
 
      02-27-2011
On Feb 27, 9:22*pm, "Frank Millman" <(E-Mail Removed)> wrote:
> "Ben Finney" <(E-Mail Removed)> wrote in message
>
> news:(E-Mail Removed)...
>
>
>
> > 人言落日是天涯,望极天涯不见家 <(E-Mail Removed)> writes:

>
> >> Here is a simple example:
> >> [app]
> >> * * * [module]
> >> * * * * * * __init__.py * --> empty
> >> * * * * * * a.py * --> import b
> >> * * * * * * b.py *--> defined a function foo()
> >> * * * test.py

>
> >> In the test.py, contains the below statement:
> >> from module import a
> >> Execute the test.py will get error:

>
> > This works fine for me::

>
> > * *$ mkdir --parents app/module/
> > * *$ touch app/module/__init__.py
> > * *$ printf "import b\n" > app/module/a.py
> > * *$ printf "def foo(): pass\n" > app/module/b.py
> > * *$ printf "from module import a\n" > app/test.py
> > * *$ find .
> > * *.
> > * *./app
> > * *./app/module
> > * *./app/module/__init__.py
> > * *./app/module/a.py
> > * *./app/module/b.py
> > * *./app/test.py

>
> > * *$ python app/test.py

>
> >> Traceback (most recent call last):
> >> * File "<stdin>", line 1, in <module>
> >> * File "module\a.py", line 1, in <module>
> >> * * import b
> >> ImportError: No module named b

>
> >> Why the b.py can not be found by a.py?

>
> > I get no errors; the code appears to run fine. Perhaps the scenario is
> > not exactly as you describe?

>
> I get exactly the same result as the OP, using python 3.2 on both windows
> and linux. It works using python 2.6.
>
> I can fix it by changing a.py from 'import b' to 'from . import b'.
>
> As I understand it, the reason is that python 3.x will no longer look foran
> absolute import in the current package - it will only look in sys.path.
>
> Frank Millman


This behavior is by design or just a bug for Python3.x ?
 
Reply With Quote
 
Frank Millman
Guest
Posts: n/a
 
      02-27-2011

"人言落日是天涯,望极天涯不见家 " <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> On Feb 27, 9:22 pm, "Frank Millman" <(E-Mail Removed)> wrote:
>
> This behavior is by design or just a bug for Python3.x ?


Definitely by design.

Have a look at PEP 328 - http://www.python.org/dev/peps/pep-0328/

"In Python 2.4 and earlier, if you're reading a module located inside a
package, it is not clear whether
import foo
refers to a top-level module or to another module inside the package. As
Python's library expands, more and more existing package internal modules
suddenly shadow standard library modules by accident. It's a particularly
difficult problem inside packages because there's no way to specify which
module is meant. To resolve the ambiguity, it is proposed that foo will
always be a module or package reachable from sys.path. This is called an
absolute import."

HTH

Frank


 
Reply With Quote
 
人言落日是天涯,望极天涯不见家
Guest
Posts: n/a
 
      02-27-2011
On Feb 27, 9:38*pm, "Frank Millman" <(E-Mail Removed)> wrote:
> "人言落日是天涯,望极天涯不见家 " <(E-Mail Removed)> wrote in message
>
> news:(E-Mail Removed)...
>
> > On Feb 27, 9:22 pm, "Frank Millman" <(E-Mail Removed)> wrote:

>
> > This behavior is by design or just a bug for Python3.x ?

>
> Definitely by design.
>
> Have a look at PEP 328 -http://www.python.org/dev/peps/pep-0328/
>
> "In Python 2.4 and earlier, if you're reading a module located inside a
> package, it is not clear whether
> import foo
> refers to a top-level module or to another module inside the package. As
> Python's library expands, more and more existing package internal modules
> suddenly shadow standard library modules by accident. It's a particularly
> difficult problem inside packages because there's no way to specify which
> module is meant. To resolve the ambiguity, it is proposed that foo will
> always be a module or package reachable from sys.path. This is called an
> absolute import."
>
> HTH
>
> Frank


Yes, it's okay with the change in a.py with below line:
from . import b

But another issue occurred if I want to run the a.py separately.
$ cd module
$ python a.py
Traceback (most recent call last):
File "a.py", line 1, in <module>
from . import b
ValueError: Attempted relative import in non-package

Does that mean the relative import only allowed in the package.
And it cannot be run as __main__ program unless I change the relative
import back to absolute import?
I think this behavior is strange and difficult to use.


Doesn't
 
Reply With Quote
 
Frank Millman
Guest
Posts: n/a
 
      02-27-2011

"人言落日是天涯,望极天涯不见家 " <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> On Feb 27, 9:38 pm, "Frank Millman" <(E-Mail Removed)> wrote:
>> "人言落日是天涯,望极天涯不见家 " <(E-Mail Removed)> wrote in message
>>
>> news:(E-Mail Removed)...
>>
>> > On Feb 27, 9:22 pm, "Frank Millman" <(E-Mail Removed)> wrote:

>>
>> > This behavior is by design or just a bug for Python3.x ?

>>
>> Definitely by design.
>>
>> Have a look at PEP 328 -http://www.python.org/dev/peps/pep-0328/
>>
>> "In Python 2.4 and earlier, if you're reading a module located inside a
>> package, it is not clear whether
>> import foo
>> refers to a top-level module or to another module inside the package. As
>> Python's library expands, more and more existing package internal modules
>> suddenly shadow standard library modules by accident. It's a particularly
>> difficult problem inside packages because there's no way to specify which
>> module is meant. To resolve the ambiguity, it is proposed that foo will
>> always be a module or package reachable from sys.path. This is called an
>> absolute import."
>>
>> HTH
>>
>> Frank

>
> Yes, it's okay with the change in a.py with below line:
> from . import b
>
> But another issue occurred if I want to run the a.py separately.
> $ cd module
> $ python a.py
> Traceback (most recent call last):
> File "a.py", line 1, in <module>
> from . import b
> ValueError: Attempted relative import in non-package
>
> Does that mean the relative import only allowed in the package.
> And it cannot be run as __main__ program unless I change the relative
> import back to absolute import?
> I think this behavior is strange and difficult to use.
>


I think that PEP 366 addresses your question -

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

I came across this while investigating a separate problem. It looks a bit
complicated, and I don't have such a requirement, so I did not look at it
too closely.

Have a read and see if it solves your problem. If not post a follow-up
message here and see if someone else can offer more information.

Frank


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
on package import, have it conditionally import a subpackage Gabriel Rossetti Python 1 09-20-2009 09:18 PM
single package import v/s the entire package Parvinder Java 6 02-27-2005 02:02 PM
package module import name clash with global package George P Python 3 09-11-2004 01:19 PM
internal to internal NAT? Mike Cisco 1 04-21-2004 12:15 PM
import vs from module import : any performance issue? Pierre Rouleau Python 4 03-07-2004 05:06 PM



Advertisments