How can I execute __main__ from another Python module? [duplicate] - python

This question already has answers here:
Calling if __name__ == '__main__': in one module from a function in another module [closed]
(2 answers)
Closed 6 years ago.
I have a python file that I am not the maintainer of that looks like the code below. Without modifying the code to put everything under the if __name__, is there a way to import this from another module so I can execute it and pass arguments programmatically?
import configargparse
if __name__ == '__main__':
args = get_args()
#more code to execute...

You can't, for the most part. That code isn't exposed in a useful fashion. If you are able to modify the source file, the typical solution would be to move all of that code out of the if __name__ == '__main__' block and put it into a main() function instead.
It's possible to use the execfile function to sort of do what you want, as described here, but this is an ugly solution for a number of reasons (the import statement is being used only for side effects, because you're cheating and using it to get the filename, and module level variables will probably not behave as you expect when referenced as module.var, as described in some of the comments on that page).
And even in this example, it's not clear that there's a useful way to pass arguments. I guess you could set sys.argv explicitly, but look how hacky this is already smelling...

Related

How to sequentially run multiple script in python [duplicate]

This question already has answers here:
How can I make one python file run another? [duplicate]
(8 answers)
Closed 2 years ago.
I have a list of .py file that I need to run sequentially.
I usually do it manually, one by one.
Anyway I'd like to have a .py file by which I run these .py automatically.
In R I usually use the command source:
Source (file1, file2 etc)
Is there a way to do anything similar in python?
By the way, I use IDLE, so I'd like to have a solution using this program.
Thank you in advance
execfile(filename)
is probably what you want to use
for instance if you have a list of files to run
e.g.
files_to_run = ["path_to/File1", "path_to/File2", "path_to/File3", "path_to/File4"]
for file in files_to_run:
execfile(file)
simple import technically executes the file.
In python, it is common practice to use to define all the required functions int he module and function calls are defined inside if __name__ == "__main__": [means the code inside the if will execute only if the script called directly not when imported]
so that the same module can used as executable and also imported in another file.
Technically, if you did not use if __name__ == "__main__": in your files, simply import itself will execute them.

How do I find out the file that is the beginning of a Python program? [duplicate]

This question already has answers here:
Find path to currently running file
(8 answers)
Closed 7 years ago.
Let's assume that I have a program that consists of at least two files, magic.py which is part of the herebemagic module...
def foo():
# This line is what my question will be all about
pass
...and main.py, which starts the program:
import os
from herebemagic import magic
print("This is where this file lies: %s" % (repr(os.path.abspath(__file__)), ))
magic.foo()
As demonstrated, finding a module's file, and the path that it is in, is trivial. But how would I, in magic.py, get a reference to main.py's module (being the one that was invoked by the interpreter, not necessarily the one that imported herebemagic directly), so that I can get at its file?
Of course I could just figure it out in main.py, and pass it down to foo(), but in practice there can be an arbitrary number of modules in between the two, and in my case, it'd be ugly to pass it down (I'm loading a lot of modules with importlib, and the information is currently only relevant to one of the modules). Or I could use a builtin, but that is a namespace that no one wants to litter.
The Python executable is called with the name of the main script as command line argument. Python keeps this in sys.argv:
sys.argv[0]

Import a python module without running it [duplicate]

This question already has answers here:
Why is Python running my module when I import it, and how do I stop it?
(12 answers)
Closed 5 months ago.
I need to import only a single function from another python file which runs stuff in it, but when I import the function, it runs the entire code instead of importing just the function I want. Is there anyway to only import a single function from another .py file without running the entire code?
In another.py, move the code that you don't want to be ran into a block that only runs when the script is explicitly called to run and not just imported
def my_func(x):
return x
if __name__ == '__main__':
# Put that needs to run here
Now if you are in your_script.py, you can import the another module and the my_func function will not run at import.
from another import my_func # Importing won't run the function.
my_func(...) # You can run the function by explicitly calling it.
In the other python script , which you are going to import, you should put all the code that needs to be executed on running the script inside the following if block -
if '__main__' == __name__:
Only when running that python file as a script, the __name__ variable will be __main__ . When you import the script, any code inside this if condition would not run.
You could move the function in question to another file and import it into your file.
But the fact that you are running everything on import makes me think you need to move most of the stuff in your imported module into functions and call those only as need with a main guard.
def print_one():
print "one"
def print_two():
print "two"
def what_i_really_want_import():
print "this is what I wanted"
if __name__ == '__main__':
print_one()
print_two()
rather than what you probably have, which I guess looks like
print "one"
print "two"
def what_i_really_want_import():
print "this is what I wanted"
With the main guard anything in a function will not be executed at import time, though you can still call it if you need to. If name == "main" really means "am I running this script from the command line?" On an import, the if conditional will return false so your print_one(), print_two() calls will not take place.
There are some good reasons to leave things in a script to execute on import. Some of them are constants, initialization/configuration steps that you want to take place automatically. And having a module-level variable is an elegant way to achieve a singleton.
def print_one():
print "one"
def print_two():
print "two"
time_when_loaded = time.time()
class MySingleton(object):
pass
THE_ANSWER = 42
singleton = MySingleton()
But by and large, don't leave too much code to execute on load, otherwise you'll end up with exactly these problems.
# How to makes a module without being fully executed ?!
# You need to follow below structure
"""
def main():
# Put all your code you need to execute directly when this script run directly.
pass
if __name__ == '__main__':
main()
else:
# Put functions you need to be executed only whenever imported
"""
1.Open in editor
2. Find the definition
3. Copy paste the old fashioned away
Simplest solution is sometimes the dirtiest.

Understanding the main method of python [duplicate]

This question already has answers here:
What does if __name__ == "__main__": do?
(45 answers)
Closed 8 years ago.
I am new to Python, but I have experience in other OOP languages. My course does not explain the main method in python.
Please tell me how main method works in python ? I am confused because I am trying to compare it to Java.
def main():
# display some lines
if __name__ == "__main__": main()
How is main executed and why do I need this strange if to execute main. My code is terminated without output when I remove the if.
The minimal code -
class AnimalActions:
def quack(self): return self.strings['quack']
def bark(self): return self.strings['bark']
class Duck(AnimalActions):
strings = dict(
quack = "Quaaaaak!",
bark = "The duck cannot bark.",
)
class Dog(AnimalActions):
strings = dict(
quack = "The dog cannot quack.",
bark = "Arf!",
)
def in_the_doghouse(dog):
print(dog.bark())
def in_the_forest(duck):
print(duck.quack())
def main():
donald = Duck()
fido = Dog()
print("- In the forest:")
for o in ( donald, fido ):
in_the_forest(o)
print("- In the doghouse:")
for o in ( donald, fido ):
in_the_doghouse(o)
if __name__ == "__main__": main()
The Python approach to "main" is almost unique to the language(*).
The semantics are a bit subtle. The __name__ identifier is bound to the name of any module as it's being imported. However, when a file is being executed then __name__ is set to "__main__" (the literal string: __main__).
This is almost always used to separate the portion of code which should be executed from the portions of code which define functionality. So Python code often contains a line like:
#!/usr/bin/env python
from __future__ import print_function
import this, that, other, stuff
class SomeObject(object):
pass
def some_function(*args,**kwargs):
pass
if __name__ == '__main__':
print("This only executes when %s is executed rather than imported" % __file__)
Using this convention one can have a file define classes and functions for use in other programs, and also include code to evaluate only when the file is called as a standalone script.
It's important to understand that all of the code above the if __name__ line is being executed, evaluated, in both cases. It's evaluated by the interpreter when the file is imported or when it's executed. If you put a print statement before the if __name__ line then it will print output every time any other code attempts to import that as a module. (Of course, this would be anti-social. Don't do that).
I, personally, like these semantics. It encourages programmers to separate functionality (definitions) from function (execution) and encourages re-use.
Ideally almost every Python module can do something useful if called from the command line. In many cases this is used for managing unit tests. If a particular file defines functionality which is only useful in the context of other components of a system then one can still use __name__ == "__main__" to isolate a block of code which calls a suite of unit tests that apply to this module.
(If you're not going to have any such functionality nor unit tests than it's best to ensure that the file mode is NOT executable).
Summary: if __name__ == '__main__': has two primary use cases:
Allow a module to provide functionality for import into other code while also providing useful semantics as a standalone script (a command line wrapper around the functionality)
Allow a module to define a suite of unit tests which are stored with (in the same file as) the code to be tested and which can be executed independently of the rest of the codebase.
It's fairly common to def main(*args) and have if __name__ == '__main__': simply call main(*sys.argv[1:]) if you want to define main in a manner that's similar to some other programming languages. If your .py file is primarily intended to be used as a module in other code then you might def test_module() and calling test_module() in your if __name__ == '__main__:' suite.
(Ruby also implements a similar feature if __file__ == $0).
In Python, execution does NOT have to begin at main. The first line of "executable code"
is executed first.
def main():
print("main code")
def meth1():
print("meth1")
meth1()
if __name__ == "__main__":main() ## with if
Output -
meth1
main code
More on main() - http://ibiblio.org/g2swap/byteofpython/read/module-name.html
A module's __name__
Every module has a name and statements in a module can find out the name of its module. This is especially handy in one particular situation - As mentioned previously, when a module is imported for the first time, the main block in that module is run. What if we want to run the block only if the program was used by itself and not when it was imported from another module? This can be achieved using the name attribute of the module.
Using a module's __name__
#!/usr/bin/python
# Filename: using_name.py
if __name__ == '__main__':
print 'This program is being run by itself'
else:
print 'I am being imported from another module'
Output -
$ python using_name.py
This program is being run by itself
$ python
>>> import using_name
I am being imported from another module
>>>
How It Works -
Every Python module has it's __name__ defined and if this is __main__, it implies that the module is being run standalone by the user and we can do corresponding appropriate actions.
Python does not have a defined entry point like Java, C, C++, etc. Rather it simply executes a source file line-by-line. The if statement allows you to create a main function which will be executed if your file is loaded as the "Main" module rather than as a library in another module.
To be clear, this means that the Python interpreter starts at the first line of a file and executes it. Executing lines like class Foobar: and def foobar() creates either a class or a function and stores them in memory for later use.
If you import the module (.py) file you are creating now from another python script it will not execute the code within
if __name__ == '__main__':
...
If you run the script directly from the console, it will be executed.
Python does not use or require a main() function. Any code that is not protected by that guard will be executed upon execution or importing of the module.
This is expanded upon a little more at python.berkely.edu

Why use def main()? [duplicate]

This question already has answers here:
What does if __name__ == "__main__": do?
(45 answers)
Closed 3 years ago.
I've seen some code samples and tutorials that use
def main():
# my code here
if __name__ == "__main__":
main()
But why? Is there any reason not do define your functions at the top of the file, then just write code under it? ie
def my_function()
# my code here
def my_function_two()
# my code here
# some code
# call function
# print(something)
I just wonder if there is any rhyme to the main?
Without the main sentinel, the code would be executed even if the script were imported as a module.
Everyone else has already answered it, but I think I still have something else to add.
Reasons to have that if statement calling main() (in no particular order):
Other languages (like C and Java) have a main() function that is called when the program is executed. Using this if, we can make Python behave like them, which feels more familiar for many people.
Code will be cleaner, easier to read, and better organized. (yeah, I know this is subjective)
It will be possible to import that python code as a module without nasty side-effects.
This means it will be possible to run tests against that code.
This means we can import that code into an interactive python shell and test/debug/run it.
Variables inside def main are local, while those outside it are global. This may introduce a few bugs and unexpected behaviors.
But, you are not required to write a main() function and call it inside an if statement.
I myself usually start writing small throwaway scripts without any kind of function. If the script grows big enough, or if I feel putting all that code inside a function will benefit me, then I refactor the code and do it. This also happens when I write bash scripts.
Even if you put code inside the main function, you are not required to write it exactly like that. A neat variation could be:
import sys
def main(argv):
# My code here
pass
if __name__ == "__main__":
main(sys.argv)
This means you can call main() from other scripts (or interactive shell) passing custom parameters. This might be useful in unit tests, or when batch-processing. But remember that the code above will require parsing of argv, thus maybe it would be better to use a different call that pass parameters already parsed.
In an object-oriented application I've written, the code looked like this:
class MyApplication(something):
# My code here
if __name__ == "__main__":
app = MyApplication()
app.run()
So, feel free to write the code that better suits you. :)
if the content of foo.py
print __name__
if __name__ == '__main__':
print 'XXXX'
A file foo.py can be used in two ways.
imported in another file : import foo
In this case __name__ is foo, the code section does not get executed and does not print XXXX.
executed directly : python foo.py
When it is executed directly, __name__ is same as __main__ and the code in that section is executed and prints XXXX
One of the use of this functionality to write various kind of unit tests within the same module.
"What does if __name__==“__main__”: do?" has already been answered.
Having a main() function allows you to call its functionality if you import the module. The main (no pun intended) benefit of this (IMHO) is that you can unit test it.
Consider the second script. If you import it in another one, the instructions, as at "global level", will be executed.

Categories

Resources