Linux argparse Python modules in cli

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7473609/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 06:16:10  来源:igfitidea点击:

argparse Python modules in cli

pythonlinuxcommand-lineargparse

提问by user904542

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

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

回答by Ned Batchelder

You can examine the search path for modules with:

You can examine the search path for modules with:

import sys
print "\n".join(sys.path)

But not having argparse is odd: it's in the standard library...

But not having argparse is odd: it's in the standard library...

回答by yak

You're probably using an older version of Python.

You're probably using an older version of Python.

The argparse module has been added pretty recently, in Python 2.7.

The argparse module has been added pretty recently, in Python 2.7.

回答by dkamins

The argparsemodule was added in Python 2.7. http://docs.python.org/library/argparse.html

The argparsemodule 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

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 getoptis a good abstraction layer, and argparseis even better.

Of course you can always handle the command-line manually simply by looking at sys.argv. However getoptis a good abstraction layer, and argparseis even better.

If you truly need argparsein 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 truly need argparsein 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/

回答by Tim Fulmer

If you're on CentOS and don't have an easy RPM to get to Python 2.7, JF's suggestion of pip install argparseis the way to go. Calling out this solution in a new answer. Thanks, JF.

If you're on CentOS and don't have an easy RPM to get to Python 2.7, JF's suggestion of pip install argparseis the way to go. Calling out this solution in a new answer. Thanks, JF.

回答by Tony-Caffe

Just add the package manually if your using Centos 6 default Python 2.6.6

Just add the package manually if your using Centos 6 default Python 2.6.6

yum install python-argparse

Thats all it took for me to get IPython to work. Odd that YUM didnt install it automatically when I used YUM to install IPython.

Thats all it took for me to get IPython to work. Odd that YUM didnt install it automatically when I used YUM to install IPython.