How to call a function defined in another py file

S

silverburgh.meryl

Hi,

I have a function called 'test' defined in A.py.
How can I call that function test in my another file B.py?

Thank you.
 
S

silverburgh.meryl

In B.py:

import A

A.test()

HTH
Martin

But Do I need to put A.py and B.py in the same directory?
if not, where does python look for A.py ?
And do I need to compile A.py before I can import it to B.py?
 
M

Martin Blume

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
 
J

Jeremy Gransden

But Do I need to put A.py and B.py in the same directory?
if not, where does python look for A.py ?
No, they do not have to be in the same directory. A.py needs to be in
your path.
And do I need to compile A.py before I can import it to B.py?

No




jeremy
 
B

Bruno Desthuilliers

(e-mail address removed) a écrit :
Hi,

I have a function called 'test' defined in A.py.
How can I call that function test in my another file B.py?

Thank you.

# b.py

import A
A.test()
 

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,681
Members
48,796
Latest member
Greg L.

Latest Threads

Top