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

Luis M. González luismgz at gmail.com
Wed Oct 22 22:52:07 EDT 2008


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:

>>> import sys
>>> sys.path.append('C:\\Users\\You\\Desktop')
>>> from foo import bar

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


On 22 oct, 18:38, Kurda Yon <kurda... at yahoo.com> wrote:
> 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.




More information about the Python-list mailing list