[Tutor] import question (solved)

Ryan Davis ryan at acceleration.net
Mon Jan 10 17:23:39 CET 2005


The problem was with how I was starting the interpreter.  The sys.path didn't have all the needed folders.  I was running my scripts
in xemacs, using C-c C-c to re-run my script after making a change.  The problem turned out to be I wasn't using the package names
in the import statements.  Once I used the package names and started the interpreter from the root directory of my project, it all
worked as I expected.

So, to import class 'a' from /One/A.py, I had to use:
>>> import One.A.a

which seems a lot cleaner than having things like this at the top of each script:
>>> import sys
>>> sys.path.append('../')

Thank you all very much for you help.

Thanks,
Ryan 

-----Original Message-----
From: Jacob S. [mailto:keridee at jayco.net] 
Sent: Friday, January 07, 2005 9:37 PM
To: Alan Gauld; Ryan Davis; Tutor at python.org
Subject: Re: [Tutor] import question

> It should work as you describe it.
> Can you just clarify how you are doing this?
> Are you setting sys.path in the same program you
> are trying to run? Or is it set in a Python startup script?
>
> How do you run a.py? Are you importing it into an existing
> Python session(with sys.path set or are you ruinning the
> file standalone?
>
> Basically the only thing I can think of is that your setting
> of sys.path isn't being seen.
>
> Can you put a debug print statement to display sys.path
> just before the import b statement? That way we make
> absolutely certain Pythoncan see the folder.
>
> Alan G.

I had some similar trouble with site-packages when I tried to install psyco
a little while ago. Apparently, the sys.path does not include sub folders of
site-packages more than one branch deep. I fixed it by moving the main
module folder up into the site-package folder. IOW, from
C:\python24\lib\site-packages\psyco-1.3\psyco to
C:\python24\lib\site-packages\psyco. And then it worked! So maybe Ryan has
to specify the exact folder?

HTH,
Jacob Schmidt



More information about the Tutor mailing list