473,394 Members | 1,817 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,394 software developers and data experts.

Importing some functions from a py file

Hi,

I think I got confused by the python import facility.

Say, in code1.py I have func1, func2, func3 and main.

In code2.py, I *only* want to use func2 from code1.py.

So, I did

from code1 import func2

But every time, I run code2.py, the main() of code1.py
is run.

I don't know why. Any hint please? Thanks.

__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com
Jul 19 '05 #1
1 1171
On Tue, 19 Apr 2005 19:13:13 -0700 (PDT), Anthony Liu
<an***********@yahoo.com> wrote:
I think I got confused by the python import facility.
Say, in code1.py I have func1, func2, func3 and main.
In code2.py, I *only* want to use func2 from code1.py.
So, I did

from code1 import func2

But every time, I run code2.py, the main() of code1.py
is run.

I don't know why. Any hint please? Thanks.


*All* of code1.py is executed when you import all or parts of it.

code1.py should look something like this:
===
import some, modules, maybe

def func1():
do_sth_1()

def func2():
do_sth_2()

def main():
do sth_m()

if __name__ == "__main__":
main()
===

The import, the defs, and the "if" and anything else you have at the
top level are statements that are executed.

If you run code1.py as a script, the if test passes, and main() is
invoked. Otherwise (you import all or bits of the code1 module), the
if test fails, and main() is not invoked.

Possibilities: (a) In your real code1.py, you don't actually have "def
main()", you have do_sth_m() at the top level, and it is being
executed unconditionally (b) you do have "def main()" as per my
example, but you have an unguarded "main()" at the top level (c)
something that we can't guess -- you may have to post a more detailed
description (cut-down version of the actual code would be a good
idea!).

HTH,
John

Jul 19 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: jean-marc | last post by:
As an application programmer, I'm not well versed in the material aspects of computing (memory, cpu, bus and all). My understanding of imports in Python is such: the __main__ program is the center...
6
by: Sam Lazarus | last post by:
I need to import data from a website, but the text is arranged badly: Name:John Doe Title:Grunt ID:314159 Name:Jane Doe Title:Queen-Bee ID:271828 etc...
1
by: Jenny | last post by:
I need to import dates from a txt file that come in the format 01JAN2003. Is there a way to do this when I am importing the file into access? Any advice would be great!
8
by: harry | last post by:
Hi Folks: I'm trying to do something that looks simple, but I can't make it work right. It's Access 2000 on a Win2000 computer. I create a database with 5 columbs. The data I need to import...
5
by: Howard Kaikow | last post by:
I have files to build a C project, including the makefile, what's the easiest way to import the project into C and C# in VS .NET Professional? I have both VS .NET 2002 and 2003. I have nothing...
3
by: Alex | last post by:
Dear list, I have a Dll it's .h and .lib files which have been compiled under Borland. I followed the steps indicated to transform the lib to a lib file readable by MSVC++ IMPDEF.EXE...
1
by: lled | last post by:
I have a text file which I import on a daily basis - it goes into the same table, is set up exactly the same each time. The only thing that is different is the file name. Is there anything more...
2
by: HMS Surprise | last post by:
Greetings, First I will admit I am new to Python but have experience with C++ and some Tcl/Tk. I am starting to use a tool called MaxQ that uses jython. I have been studying Rossum's tutorial...
3
by: Yitzak | last post by:
Hi I've got a mdb that is recreated monthly/weekly. I've written a bas module with lots of functions in it so that they can be used in queries. What is an easy way to import the bas module...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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...

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.