Notes on python modules and python scripts
It is very easy to make a python module: just write a bunch of functions and variables in a .py file, e.g. mymodule.py . You can then import it as a module within python with
As mentioned in the python documentation on modules: "Within a module, the module’s name (as a string) is available as the value of the global variable
When you run a python module as a script (
you can make the file usable as a script as well as an importable module, because the code that parses the command line only runs if the module is executed as the “main” file. When you import the module, those lines are not executed.
Also, here is a nice post on python modules and PYTHONPATH.
Now that we can run our python module from the command line, let's make it nicer by adding the possibility to add arguments. You can find some nice posts about it. For example, this one. I'll just summarize.
You first need to define your options and keywords:
Now use the getopt module to parse the arguments:
Then loop through your options:
So, in summary, here is how most python scripts should look like:
Finally, to make your python script executable. Start the file with
or whatever your python path is. You'll also need to change the permissions on the file (
import mymodule
.
As mentioned in the python documentation on modules: "Within a module, the module’s name (as a string) is available as the value of the global variable
__name__
".
When you run a python module as a script (
python mymodule.py <arguments>
), the code in the module will be executed, just as if you imported it, but with the __name__
set to "__main__"
. That means that by adding this code at the end of your module:
if __name__ == "__main__":
my_command_line_1
my_command_line_2
you can make the file usable as a script as well as an importable module, because the code that parses the command line only runs if the module is executed as the “main” file. When you import the module, those lines are not executed.
Also, here is a nice post on python modules and PYTHONPATH.
Now that we can run our python module from the command line, let's make it nicer by adding the possibility to add arguments. You can find some nice posts about it. For example, this one. I'll just summarize.
You first need to define your options and keywords:
letters = 'm:cv:G' # : means an argument needs to be passed after the letter
keywords = ['month=','create','variable=','Group'] # = means an argument needs to be passed after the keyword
Now use the getopt module to parse the arguments:
import getopt
import sys
opts, extraparams = getopt.getopt(sys.argv[1:],letters,keywords) # keywords are optional
# starts at the second element of argv since the first one is the script name
# extraparms are extra arguments passed after all option/keywords are assigned
# opts is a list containing the pair "option"/"value"
Then loop through your options:
var='default'
month=3
for o,val in opts:
if o in ['-v','--variable']:
var = val
elif o in ['-m','--model']:
month = int(val)
elif o in ['-c','--create']:
doSomething()
elif o in ['-G','--Group']:
doSomethingElse()
So, in summary, here is how most python scripts should look like:
if __name__ == "__main__":
letters = 'm:cv:G'
keywords = ['month=','create','variable=','Group']
import getopt
import sys
opts, extraparams = getopt.getopt(sys.argv[1:],letters,keywords)
var='default'
month=3
for o,val in opts:
if o in ['-v','--variable']:
var = val
elif o in ['-m','--model']:
month = int(val)
elif o in ['-c','--create']:
doSomething()
elif o in ['-G','--Group']:
doSomethingElse()
Finally, to make your python script executable. Start the file with
#! /usr/local/bin/python
or whatever your python path is. You'll also need to change the permissions on the file (
chmod +x mypythonscript.py
).
Comments
Post a Comment