Newbie question: WinNT Python 1.5.2 - import seems not to work the way I expect

Greg Landrum glandrum at my-deja.com
Wed Oct 25 09:51:13 EDT 2000


In article <qLmJ5.25623$6O5.2409048 at news1.rdc1.mb.home.com>,
  "Parzival" <parz at RemoveThisSpamBucket.home.com> wrote:
> I am running Python 1.5.2 on Win NT 4.0.
>
> I am trying to explore the "dopy" distributed object module, and the
modules
> fail with import errors. I tried to reduce this problem to its
simplest
> manifestation, and found that I got one result in IDLE, and another
running
> my test module from the command prompt. The contents of "dopy"
directory
> are:
>
> F:\Program Files\Python\dopy>ls
> Manual.html    __init__.py    dopy.pyc       naming.pyc     tb.pyc
> Manual.nml     __init__.pyc   dopyclient.py  pos.py         tcp.py
> README.html    backup         dopyserver.py  pos.pyc        tcp.pyc
> README.txt     dopy.py        naming.py      tb.py          testimp.py
>
> The test script "testimp.py" reads:
>
> import sys
> print sys.path
> from dopy import tcp
>
> The output of the script (the news client may split the lines):
>
> F:\Program Files\Python\dopy>testimp.py
[snip]
> Traceback (innermost last):
>   File "F:\PROGRA~1\Python\dopy\testimp.py", line 3, in ?
>     from dopy import tcp
> ImportError: cannot import name tcp
>

Because you are running this from the dopy directory, Python is finding
the file dopy.py before it finds the package dopy.  So it imports
dopy.py, which has no name tcp defined.  If you copy your test script
to another directory and run it there, it should work.

> When running the Python command prompt, the same commands produce the
> following output:
 [snip]
>
> I.e. the commands found in the script succeed.

I'm guessing that you did not start Python in the dopy directory.  If
you had, you would have encountered the same error.

> When I load testimp.py in the IDLE editor, and run it I get:
>
[snip]
>
> I.e. the script also succeeds. So, my question is why do I get this
> import failure when running testimp.py from the command prompt?
>

Same reason, Idle is not running from the dopy directory.

An aside: Thanks for providing so much information in your question,
it really made it easier to come up with an answer!

I hope this helps,
-greg



Sent via Deja.com http://www.deja.com/
Before you buy.



More information about the Python-list mailing list