40

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.

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.

I should have clarified that the ideal solution would be something that does not require module installation.

8
  • You can simply install the argparse package on the machine. Commented Mar 11, 2013 at 2:12
  • 2
    pypi.python.org/pypi/argparse Commented Mar 11, 2013 at 2:12
  • 2
    @nneonneo: Second result on Google if you just search "argparse". Commented Mar 11, 2013 at 2:16
  • @nneonneo I obviously saw that, but it wasn't clear to me if it is exactly the same as the 2.7 argparse. Also I wanted to see if there is some solution that doesn't require installation of a module. Commented Mar 11, 2013 at 2:17
  • 11
    from __future__ import ... is only for language features, not whole modules. You can actually check what's available in __future__ by doing import __future__; dir(__future__). (I am not responsible for any paradoxes derived from seeing the future) Commented Mar 11, 2013 at 2:22

5 Answers 5

35

On Centos, Scientific, or Redhat, you can fix this by running the command:for

yum install python-argparse
Sign up to request clarification or add additional context in comments.

1 Comment

According to pkgs.org, the package python-argparse is only available on CentOS 6 repository. On CentOS 5, it is available on EPEL repository, which must be enabled. For more information, check the CentOS Wiki topic on Repositories.
33

You can install it via pip or easy_install: https://pypi.python.org/pypi/argparse

2 Comments

FIY, here's the command which I used. easy_install pip && pip install argparse yum doesn't have the package for this version of Python.
Why doesn't this work for me? It still complains that ImportError: No module named argparse after successfully running pip install argparse.
11

you can also get the argparse.py file (e.g. from https://pypi.python.org/pypi/argparse) and put it in the same folder as your main python file.

Comments

6

If you are on Debian-like systems, you may simply write this:

apt-get install python-argparse

Comments

0

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_64 that 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.

i.e. all files and directories beginning with argparse in /usr/lib/python2.6/site-packages

Only after doing all this did I discover that I could have simply installed the python argparse rpm python-argparse that comes with both distributions.

i.e. yum install python-argparse

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.