469,336 Members | 5,570 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,336 developers. It's quick & easy.

How to - import code not in current directory

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.

Nov 22 '05 #1
10 34080
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" <co*******@gmail.com> schrieb im Newsbeitrag
news:11**********************@g14g2000cwa.googlegr oups.com... 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.

Nov 22 '05 #2
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" <co*******@gmail.com> schrieb im Newsbeitrag
news:11**********************@g14g2000cwa.googlegr oups.com... 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.

Nov 22 '05 #3
py
Claudio Grondi wrote:
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...

Nov 22 '05 #4
py
Claudio Grondi wrote:
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...

Nov 22 '05 #5
On Thu, 2005-11-17 at 08:41, py wrote:
Claudio Grondi wrote:
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.


That would be PYTHONPATH.

-Carsten
Nov 22 '05 #6
On Thu, 2005-11-17 at 08:41, py wrote:
Claudio Grondi wrote:
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.


That would be PYTHONPATH.

-Carsten
Nov 22 '05 #7
"py" <co*******@gmail.com> schrieb im Newsbeitrag
news:11**********************@z14g2000cwz.googlegr oups.com...
Claudio Grondi wrote:
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...

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

Claudio
Nov 22 '05 #8
"py" <co*******@gmail.com> schrieb im Newsbeitrag
news:11**********************@z14g2000cwz.googlegr oups.com...
Claudio Grondi wrote:
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...

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

Claudio
Nov 22 '05 #9
py
PYTHONPATH is perfect....check out this link for more info..

http://docs.python.org/tut/node8.html#searchPath

I just added the environment variable (on windows) named "PYTHONPATH"
and set it to "C:\some\other\directory"

:)

Nov 22 '05 #10
py
PYTHONPATH is perfect....check out this link for more info..

http://docs.python.org/tut/node8.html#searchPath

I just added the environment variable (on windows) named "PYTHONPATH"
and set it to "C:\some\other\directory"

:)

Nov 22 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Colin Brown | last post: by
4 posts views Thread by Matt Whiteley | last post: by
7 posts views Thread by Ron Adam | last post: by
reply views Thread by Laszlo Nagy | last post: by
9 posts views Thread by rsoh.woodhouse | last post: by
8 posts views Thread by tow | last post: by
1 post views Thread by Marylou17 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.