Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

argparse Python modules in cli

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).

like image 850
user904542 Avatar asked Sep 19 '11 15:09

user904542


People also ask

Where do you put Argparse pythons?

It's fine to put the import argparse within the if __name__ == '__main__' block if argparse is only referred to within that block.

What is Python Argparse module?

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.

Is Argparse in standard library?

Argparse: It is a standard library (included with Python) and very simple to use because of the work that happens behind the scenes.


1 Answers

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/

like image 159
dkamins Avatar answered Nov 06 '22 06:11

dkamins