如何在 Python 2.6 中获得 argparse?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15330175/
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
How can I get argparse in Python 2.6?
提问by Bitwise
I have some Python 2.7 code written that uses the argparse module. Now I need to run it on a Python 2.6 machine and it won't work since argparse was added in 2.7.
我编写了一些使用 argparse 模块的 Python 2.7 代码。现在我需要在 Python 2.6 机器上运行它,它不会工作,因为 argparse 是在 2.7 中添加的。
Is there anyway I can get argparse in 2.6? I would like to avoid rewriting the code, since I will be transferring this kind of code between the machines often. Upgrading python is not an option.
无论如何我可以在 2.6 中获得 argparse 吗?我想避免重写代码,因为我会经常在机器之间传输这种代码。升级 python 不是一种选择。
I should have clarified that the ideal solution would be something that does not require module installation.
我应该澄清一下,理想的解决方案是不需要模块安装的东西。
采纳答案by Randall Hunt
You can install it via pip or easy_install: https://pypi.python.org/pypi/argparse
您可以通过 pip 或 easy_install 安装它:https: //pypi.python.org/pypi/argparse
回答by user3517231
On Centos, Scientific, or Redhat, you can fix this by running the command:for
在 Centos、Scientific 或 Redhat 上,您可以通过运行以下命令来解决此问题:for
yum install python-argparse
回答by Hyman James
you can also get the argparse.pyfile (e.g. from https://pypi.python.org/pypi/argparse) and put it in the same folder as your main python file.
您还可以获取该argparse.py文件(例如,来自https://pypi.python.org/pypi/argparse)并将其放在与您的主要 python 文件相同的文件夹中。
回答by Houssam Hammoudi
If you are on Debian-like systems, you may simply write this:
如果您使用的是类似 Debian 的系统,您可以简单地编写以下代码:
apt-get install python-argparse
回答by Phil
When I had this need I just installed argparse using pip: pip install argparse. This worked fine for the python-2.6.6-66.el6_8.x86_64that was installed on my vanilla CentOS 6.8 system. I later found out that this did not work on other systems running python-2.6.6-52.el6.x86_64. As a result I ended up copying argparse related files from one system to the other.
当我有这个需要时,我只是使用 pip: 安装了 argparse pip install argparse。这对于python-2.6.6-66.el6_8.x86_64安装在我的 vanilla CentOS 6.8 系统上的程序运行良好。后来我发现这在运行的其他系统上不起作用python-2.6.6-52.el6.x86_64。结果我最终将 argparse 相关文件从一个系统复制到另一个系统。
i.e. all files and directories beginning with argparse in /usr/lib/python2.6/site-packages
即 /usr/lib/python2.6/site-packages 中以 argparse 开头的所有文件和目录
Only after doing all this did I discover that I could have simply installed the python argparse rpm python-argparsethat comes with both distributions.
只有在完成所有这些之后,我才发现我可以简单地安装python-argparse两个发行版附带的 python argparse rpm 。
i.e. yum install python-argparse
IE yum install python-argparse

