473,410 Members | 1,889 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,410 software developers and data experts.

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 30008
<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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: muelli75 | last post by:
Hi! Im getting insane by solving a problem .... I try to define a function which uses a code-snippet from another file. My base are the codes from the great book "WebDataBase-Book by H....
7
by: Tim ffitch | last post by:
Hi I have created a VB dll file that contains common functions I use across various projects in VB, Access and Excel. Rather than have to code the functions in each I decided to use the dll...
6
by: mygoogleaccount | last post by:
Hi, may be someone could help me ? i need to use cyrillic letters in a php application. I changed everything to UTF-8 and it works fine. The only problem are CYRILLIC SMALL LETTER ES...
11
by: dolphin | last post by:
Hi All! I have a question that how to call a function just using a string. For example There is a .cpp file named a.cpp.There are some functions::fun1() fun2() fun3(). I have another fucntion...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.