How to - import code not in current directory

P

py

I have a python script that I want to test/debug. It contains a class
which extends from some other class which is located in some other
python file in a different directory.

For example:

[script to test]
c:\python_code\foo.py

[needed python files]
c:\some\other\directory\bar.py

....so I want to test/debug foo.py, which needs bar.py. foo.py imports
bar.py ...but in order to test out foo (in the python shell) I normally
copy bar.py into the same directory as foo.py...but this is painful.
Is there some way that I can have python know about the directory where
bar.py is located, like a system variable, etc? If so, how do I set
that up?

Thanks.
 
P

py

Claudio said:
so this should work in your case:

import sys
sys.path.append("C:\some\other\directory")
import bar

....that will certainly work. Only issue is that each time I start up
foo.py in the python shell I have to retype those three lines....kind
of why I was hoping for a environment variable or path setting that i
could stick in.

This will do for now...
 
C

Carsten Haese

...that will certainly work. Only issue is that each time I start up
foo.py in the python shell I have to retype those three lines....kind
of why I was hoping for a environment variable or path setting that i
could stick in.

That would be PYTHONPATH.

-Carsten
 
C

Claudio Grondi

This question seems to come up in this newsgroup quite often, so looking
through past threads will sure provide more details.

Here from "Re: how to import a module from a arbitraty path?"
posted to comp.lang.python by Simon Brunning on May 26, 2005 09:20 :

"
I have a program which is going to dynamicly load components from some
arbitrary defined paths. How to do that?

You can locate them with os.walk and fnmatch. Then you can temporarily
add the directory to sys.path, and import using __import__().
"

so this should work in your case:

import sys
sys.path.append("C:\some\other\directory")
import bar

Claudio


py said:
I have a python script that I want to test/debug. It contains a class
which extends from some other class which is located in some other
python file in a different directory.

For example:

[script to test]
c:\python_code\foo.py

[needed python files]
c:\some\other\directory\bar.py

...so I want to test/debug foo.py, which needs bar.py. foo.py imports
bar.py ...but in order to test out foo (in the python shell) I normally
copy bar.py into the same directory as foo.py...but this is painful.
Is there some way that I can have python know about the directory where
bar.py is located, like a system variable, etc? If so, how do I set
that up?

Thanks.
 
C

Claudio Grondi

py said:
...that will certainly work. Only issue is that each time I start up
foo.py in the python shell I have to retype those three lines....kind
of why I was hoping for a environment variable or path setting that i
could stick in.

This will do for now...
See the recent threads
"IDLE question" and
"newbie - How do I import automatically?"
in THIS newsgroup for further help.

Claudio
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top