473,661 Members | 2,449 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 30017
<si************ ***@gmail.comsc hrieb
>
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.n etwrote:
<silverburgh.me ...@gmail.comsc hrieb
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.comsc hrieb
>>
>>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.n etwrote:
><silverburgh.m e...@gmail.coms chrieb
>>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
9101
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. Williams & D. Lane". So I try to use
7
5891
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 route. The problem being that I can't call these functions from the query designer in Access. I decided that I would try the route of declaring the functions from the dll file the same way you would for the Windows API. Access then complains that...
6
4262
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 CYRILLIC SMALL LETTER YA
11
2862
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 void funcall( char *pch). if I pass a argument char* p1="fun1" .How do I call the function fun1() using that string "fun1"that I pass.
0
8432
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8343
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8758
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8545
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8633
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7364
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6185
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4346
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1986
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.