How to call a function defined in another py file

Martin Blume mblume at socha.net
Mon Feb 19 15:40:22 EST 2007


<silverburgh.meryl at gmail.com> schrieb
>>
>>> I have a function called 'test' defined in A.py.
>>> How can I call that function test in my another file B.py?
>>
>> In B.py:
>>   import A
>>   A.test()
>>
> 
> But Do I need to put A.py and B.py in the same directory?
No, but then you have to take certain precautions. (*)

> if not, where does python look for A.py ?
In the path defined by the (IIRC) PYTHONPATH (*)

> And do I need to compile A.py before I can import it to B.py?
No.

(*) you might want to read the fine documentation at
http://docs.python.org/tut/node8.html
which tells it much better than I do, and might give you 
some more ideas for googling.
I haven't had yet the necessity for cross-directory imports.

HTH
Martin






More information about the Python-list mailing list