I am trying to run a python script from the Linux SSH Secure Shell command line environment, and I am trying to import the argparse library, but it gives the error: "ImportError: No module named argparse".
I think that this is because the Python environment that the Linux shell is using does not have the argparse library in it, and I think I can fix it fix it if I can find the directories for the libraries being used by the Python environment, and copy the argparse library into it, but I can not find where that directory is located.
I would appreciate any help on finding this directory (I suppose I could include the argparse library in the same directory as my python script for now, but I would much rather have the argparse library in the place where the other Python libraries are, as it should be).
It's fine to put the import argparse within the if __name__ == '__main__' block if argparse is only referred to within that block.
Argparse Module. The argparse module in Python helps create a program in a command-line-environment in a way that appears not only easy to code but also improves interaction. The argparse module also automatically generates help and usage messages and issues errors when users give the program invalid arguments.
Argparse: It is a standard library (included with Python) and very simple to use because of the work that happens behind the scenes.
The argparse
module was added in Python 2.7.
http://docs.python.org/library/argparse.html
Prior to 2.7, the most common way to handle command-line arguments was probably getopt
.
http://docs.python.org/library/getopt.html
Of course you can always handle the command-line manually simply by looking at sys.argv
. However getopt
is a good abstraction layer, and argparse
is even better.
If you truly need argparse
in older environments (debatable), there is a Google Code project maintaining it, and you can include that in your project. http://code.google.com/p/argparse/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With