简体   繁体   English

cli中的argparse Python模块

[英]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". 我试图从Linux SSH安全Shell命令行环境运行python脚本,我试图导入argparse库,但它给出错误:“ImportError:没有名为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. 我认为这是因为Linux shell使用的Python环境中没有argparse库,我想我可以修复它,如果我能找到Python环境所使用的库的目录,并且将argparse库复制到其中,但我找不到该目录所在的位置。

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). 我很感激找到这个目录的任何帮助(我想我现在可以将argparse库包含在与我的python脚本相同的目录中,但我宁愿在其他Python库所在的地方使用argparse库,因为它应该)。

The argparse module was added in Python 2.7. argparse模块是在Python 2.7中添加的。 http://docs.python.org/library/argparse.html http://docs.python.org/library/argparse.html

Prior to 2.7, the most common way to handle command-line arguments was probably getopt . 在2.7之前,处理命令行参数的最常用方法可能是getopt http://docs.python.org/library/getopt.html http://docs.python.org/library/getopt.html

Of course you can always handle the command-line manually simply by looking at sys.argv . 当然,您只需通过查看sys.argv即可手动处理命令行。 However getopt is a good abstraction layer, and argparse is even better. 但是getopt是一个很好的抽象层,而argparse甚至更好。

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. 如果你真的需要在旧环境中使用argparse (有争议),那么有一个Google Code项目可以维护它,你可以在项目中包含它。 http://code.google.com/p/argparse/ http://code.google.com/p/argparse/

If you're on CentOS and don't have an easy RPM to get to Python 2.7, JF's suggestion of pip install argparse is the way to go. 如果您使用的是CentOS并且没有简单的RPM来获取Python 2.7,那么JF建议使用pip install argparsepip install argparse的方法。 Calling out this solution in a new answer. 在新的答案中调用此解决方案。 Thanks, JF. 谢谢,JF。

Just add the package manually if your using Centos 6 default Python 2.6.6 如果你使用Centos 6默认的Python 2.6.6,只需手动添加包

yum install python-argparse

Thats all it took for me to get IPython to work. 这就是让我让IPython工作的全部内容。 Odd that YUM didnt install it automatically when I used YUM to install IPython. 奇怪的是,当我使用YUM安装IPython时,YUM没有自动安装它。

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... 但没有argparse是奇怪的:它在标准库中......

You're probably using an older version of Python. 您可能正在使用旧版本的Python。

The argparse module has been added pretty recently, in Python 2.7. 最近在Python 2.7中添加了argparse模块。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM