argparse cli 中的 Python 模块
我试图从 Linux SSH Secure Shell 命令行环境运行 python 脚本,并且尝试导入 argparse 库,但它给出错误:“ImportError:没有名为 argparse 的模块”。
我认为这是因为 Linux shell 使用的 Python 环境中没有 argparse 库,如果我能找到 Python 环境所使用的库的目录,我想我可以修复它。将 argparse 库复制到其中,但我找不到该目录所在的位置。
我将不胜感激找到这个目录的任何帮助(我想我现在可以将 argparse 库包含在与我的 python 脚本相同的目录中,但我更愿意将 argparse 库放在其他 Python 库所在的位置,因为它应该是)。
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).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
Python 2.7 中添加了 argparse 模块。
http://docs.python.org/library/argparse.html
在 2.7 之前,处理命令行参数的最常见方法可能是 getopt。
http://docs.python.org/library/getopt.html
当然可以始终只需查看 sys.argv 即可手动处理命令行。然而,
getopt
是一个很好的抽象层,argparse
甚至更好。如果您确实在旧环境中需要
argparse
(有争议),有一个 Google Code 项目在维护它,您可以将其包含在您的项目中。 http://code.google.com/p/argparse/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
. Howevergetopt
is a good abstraction layer, andargparse
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/如果您使用的是 CentOS 并且没有简单的 RPM 来获取 Python 2.7,JF 建议的
pip install argparse
是不错的选择。在新的答案中调用这个解决方案。谢谢,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 argparse
is the way to go. Calling out this solution in a new answer. Thanks, JF.如果您使用 Centos 6 默认的 Python 2.6.6,只需手动添加包即可。
这就是我让 IPython 工作所需的全部工作。奇怪的是,当我使用 YUM 安装 IPython 时,YUM 没有自动安装它。
Just add the package manually if your using Centos 6 default Python 2.6.6
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.
您可以使用以下命令检查模块的搜索路径:
但没有 argparse 很奇怪:它在标准库中......
You can examine the search path for modules with:
But not having argparse is odd: it's in the standard library...
您可能使用的是较旧版本的 Python。
argparse 模块是最近在 Python 2.7 中添加的。
You're probably using an older version of Python.
The argparse module has been added pretty recently, in Python 2.7.