Import problem
News123
news123 at free.fr
Mon Mar 8 17:06:34 EST 2010
Hi Steven,
Steven D'Aprano wrote:
> On Sat, 06 Mar 2010 03:53:53 -0800, Johny wrote:
>
>>>>> import sys
>>>>> sys.path.append('C:\\A')
>>>>> from A.B import bmodule
>> Traceback (most recent call last):
>> File "<stdin>", line 1, in <module>
>> ImportError: No module named A.B
>
> The current directory is irrelevant, except that it is automatically
> added to the PYTHONPATH. That's why you can import A.B when the current
> directory is C.
Minor currection:
It doesn't seem to be the current directory, but the directory, where
the script is located in, which is auto-appended to the pythonpath
Please see following example:
$ python -V
Python 2.6.4
$ mkdir A
$ touch A/__init__
$ # create A/blla.py an A/blo.py
$ cat A/bla.py
print "I am bla"
import A.blo
print "and I found blo",dir(A.blo)
$ cat A/blo.py
avar = 3
print "I am blo"
$ python A/bla.py
I am bla
Traceback (most recent call last):
File "A/bla.py", line 2, in <module>
import A.blo
ImportError: No module named A.blo
However:
$ cat alternative_bla.py
import sys
sys.path.append(".")
print "I am bla"
import A.blo
print "and I found blo",dir(A.blo)
$ python A/alternativ_bla.py
I am bla
I am blo
and I found blo ['__builtins__', '__doc__', '__file__', '__name__',
'__package__', 'avar']
bye N
More information about the Python-list
mailing list