12

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).

tripleee
  • 175,061
  • 34
  • 275
  • 318
user904542
  • 6,965
  • 5
  • 20
  • 28
  • argparse.py is a pretty standard piece of python, it's possible that you've corrupted your sys.path. Please run this command and paste the result into your question: python -c "import argparse; print argparse" – ed. Sep 19 '11 at 15:50
  • 1
    what version of python are you using? – Andrew Cox Sep 19 '11 at 15:50

5 Answers5

6

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. However getopt is a good abstraction layer, and argparse 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/

dkamins
  • 21,450
  • 7
  • 55
  • 59
2

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.

Tim Fulmer
  • 14,970
  • 7
  • 28
  • 34
2

Just add the package manually if your using Centos 6 default Python 2.6.6

yum install python-argparse

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.

Tony-Caffe
  • 631
  • 1
  • 8
  • 13
1

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

Ned Batchelder
  • 364,293
  • 75
  • 561
  • 662
0

You're probably using an older version of Python.

The argparse module has been added pretty recently, in Python 2.7.

yak
  • 8,851
  • 2
  • 29
  • 23