470,841 Members | 1,063 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

How to call a function defined in another py file

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.

Feb 19 '07 #1
6 29808
<si***************@gmail.comschrieb
>
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()
HTH
Martin
Feb 19 '07 #2
On Feb 19, 2:22 pm, "Martin Blume" <mbl...@socha.netwrote:
<silverburgh.me...@gmail.comschrieb
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()

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?

Feb 19 '07 #3
from a import test
be sure a is in your path.
jeremy

On Feb 19, 2007, at 3:20 PM, si***************@gmail.com wrote:
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.

--
http://mail.python.org/mailman/listinfo/python-list
Feb 19 '07 #4
<si***************@gmail.comschrieb
>>
>>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

Feb 19 '07 #5
si***************@gmail.com 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()
Feb 19 '07 #6

On Feb 19, 2007, at 3:27 PM, si***************@gmail.com wrote:
On Feb 19, 2:22 pm, "Martin Blume" <mbl...@socha.netwrote:
><silverburgh.me...@gmail.comschrieb
>>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()

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 ?
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
Feb 19 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by muelli75 | last post: by
6 posts views Thread by mygoogleaccount | last post: by
11 posts views Thread by dolphin | last post: by
reply views Thread by mihailmihai484 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.