How to import from a file which is not in the current directory?

K

Kurda Yon

Hi,

I would like to import a function from a file which is located not in
the same directory as the main program (from which the function needed
to be imported).

Could anybody pleas tell me how to do that?

Thank you in advance.
 
L

Luis M. González

Lets say that you are in directory "C:\\Python25" and you want to
import function "bar" defined in module "foo" located in your windows
desktop.
You do it like this:

So you add your desktop to "sys.path" and then import the function as
though it is in the same directory.
Hope this helps...

Luis
 
L

Luis M. González

Lets say that you are in directory "C:\\Python25" and you want to
import function "bar" defined in module "foo" located in your windows
desktop.
You do it like this:

So you add your desktop to "sys.path" and then import the function as
though it is in the same directory.
Hope this helps...
Luis
 

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

Staff online

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,162
Latest member
GertrudeMa
Top