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.

Bitwise
  • 7,577
  • 6
  • 33
  • 50
  • You can simply install the `argparse` package on the machine. – David Robinson Mar 11 '13 at 02:12
  • 2
    https://pypi.python.org/pypi/argparse – Blender Mar 11 '13 at 02:12
  • 2
    @nneonneo: Second result on Google if you just search "argparse". – Blender Mar 11 '13 at 02: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. – Bitwise Mar 11 '13 at 02:17
  • Well, it's clearly not available in Python 2.6. So, you clearly have to install or package something. By deduction, the best way is to just download the `argparse` module. (But, do note that you don't have to install it: you can just include it in your project directory as a direct dependency.) – nneonneo Mar 11 '13 at 02:19
  • @nneonneo there are some things which are not available in 2.6 per se but you can import future them, I thought there might be something like that. – Bitwise Mar 11 '13 at 02:20
  • 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) – nneonneo Mar 11 '13 at 02:22
  • All you need is one file, `argparse.py`. It can be from any Python2 compatible source. – hpaulj Apr 10 '14 at 00:01

5 Answers5

35

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

yum install python-argparse
AJMansfield
  • 4,039
  • 3
  • 29
  • 50
user3517231
  • 351
  • 3
  • 2
  • According to [pkgs.org](http://pkgs.org/search/python-argparse), the package `python-argparse` is only available on CentOS 6 repository. On CentOS 5, it is available on [EPEL repository](https://fedoraproject.org/wiki/EPEL), which must be enabled. For more information, check the [CentOS Wiki topic on Repositories](https://wiki.centos.org/AdditionalResources/Repositories). – TManhente Nov 11 '15 at 16:19
33

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

Randall Hunt
  • 12,132
  • 6
  • 32
  • 42
  • 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. – ITO Yosei Sep 20 '17 at 23:45
  • Why doesn't this work for me? It still complains that `ImportError: No module named argparse` after successfully running `pip install argparse`. – hepcat72 Mar 16 '18 at 16:59
10

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.

Jack James
  • 5,252
  • 6
  • 36
  • 38
6

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

apt-get install python-argparse
J0e3gan
  • 8,740
  • 10
  • 53
  • 80
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

Phil
  • 306
  • 2
  • 10