473,664 Members | 3,022 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

newb: What is the purpose of if __name__ == "__main__":

What is the purpose of
if __name__ == "__main__":

If you have a module, does it get called automatically?

Dec 7 '06 #1
3 2291
johnny wrote:
What is the purpose of
if __name__ == "__main__":

If you have a module, does it get called automatically?
http://docs.python.org/lib/module-main.html

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco

Dec 7 '06 #2
johnny wrote:
What is the purpose of
if __name__ == "__main__":

If you have a module, does it get called automatically?
It is a common pattern to make one and the same source file usable as a
program and as a module. When you import some python module its
__name__ is never "__main__". When you run a python script __name__ is
always "__main__". This way you can control what shall happen when you
run the source file as a standalone program.

To put it simple: Everything in the `if __name__ == "__main__": ` block
is only executed when you run the source file as a program.

Hope that helps,
--
Soni Bergraj
http://www.YouJoy.org/
Dec 7 '06 #3
"johnny" <ra*******@gmai l.comwrites:
What is the purpose of
if __name__ == "__main__":
<URL:http://www.python.org/infogami-faq/tutor/tutor-what-is-if-name-main-for/>

--
\ "He that would make his own liberty secure must guard even his |
`\ enemy from oppression." -- Thomas Paine |
_o__) |
Ben Finney

Dec 7 '06 #4

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

Similar topics

2
3138
by: marco | last post by:
Hi, first of all ; sorry for my poor english ; i'm french ... and i hope you can understand below I use python (and wxpython) on a win32 platform, to build a simple "home theater pc". I want to release it in a package (with all needed to run) so i use (the wonderful) py2exe to build it ... it works like a charm ... nothing to say
4
1911
by: Amy G | last post by:
I have a program that needs a little help. Right now the program runs in my crontab. When it runs, it sets a few variables based on a query to a MySQL database. I would like to modify it so that it can run as it is... or if arguments are supplied, use those instead of querrying the database. Will using this statement help me out? if __name__ == "__main__":
51
6952
by: Noam Raphael | last post by:
Hello, I thought about a new Python feature. Please tell me what you think about it. Say you want to write a base class with some unimplemented methods, that subclasses must implement (or maybe even just declare an interface, with no methods implemented). Right now, you don't really have a way to do it. You can leave the methods with a "pass", or raise a NotImplementedError, but even in the best solution that I know of,
22
4903
by: Qopit | last post by:
Hi there, I'm pretty new to Python and am trying to figure out how to get "will this code compile?"-like code checking. To me this is a pretty basic language/environment requirement, especially when working with large projects. It is *much* better to catch errors at "compile-time" rather than at run-time. One thing I've "found" is the PyChecker module (conveniently embedded in SPE), but it doesn't seem to do that great of a job. ...
2
3819
by: wen | last post by:
and, in which case, the following case will happen: if __name__!='__main__': do_sth() any help would be appreciated.
2
4555
by: wen | last post by:
i have written some code in test.py as below: import __main__ if __name__!='__main__': print 1 print 2 when i run test.py, i got 2 on the screen.
9
2197
by: Emanuele Aina | last post by:
I have some code which does a lot of "in" on lists containing objects with no __eq__ defined. It all goes fast until I add the __lt__() method: then I have a slowdown comparable to the one I get using the overridden __eq__, while the __lt__ method is never called. Someone can explain me why?
1
12454
by: NachosRancheros | last post by:
Ok so I just started to program with Python about a week ago and I am trying to make a program that will take the path of a file and a shortcut command and save it to a text file. Eventually I want to have a little command line box that sits on the desktop that I can execute those commands from but that is for another day. At the moment I am just trying to figure out how to put the code together but I keep getting into trouble when I try to use...
3
1469
by: bsagert | last post by:
After a couple of weeks studying Python, I already have a few useful scripts, including one that downloads 1500 Yahoo stock quotes in 6 seconds. However, many things are puzzling to me. I keep on seeing things like "__main__" in scripts. A more obscure example would be "__add__" used in string concatenation. For example, I can use "Hello "+"world (or just "Hello" "world") to join those two words. But I can also use "Hello...
0
8437
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
8861
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8778
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...
0
7375
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
6187
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
5660
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4185
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2003
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1759
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.