26

I am trying to run a Python program but get the error

ImportError: No module named argparse

I found the question “argparse Python modules in cli” here on StackOverflow and tried the first comment, i.e. running the command

python -c "import argparse; print argparse"

which resulted in

<module 'argparse' from '/usr/lib/python2.7/argparse.pyc'>

For me it seems like there is Python 2.7 installed on the machine (of which I am not administrator) and the argparse module is present as well. So I wonder why the module is not found. On another machine the script runs as it should. In the post referred to above, there is the comment that maybe sys.path is broken. I have no clue what that means, or how I can change its value. Any ideas?

Community
  • 1
  • 1
alex
  • 2,252
  • 4
  • 23
  • 34
  • 3
    Are you using Python 2.6 or earlier? "Seems like" doesn't sound too convincing. What version number do you see when you start the interpreter? `sys.version`? – Junuxx Feb 26 '13 at 15:52
  • 7
    Does the Python script you're using have a shebang (a `#!` at the start of it) that tells it to use a different Python executable than the default? – David Robinson Feb 26 '13 at 15:53
  • 1
    We'll need to see more details on the script itself. What executable is used to run it, for example? – Martijn Pieters Feb 26 '13 at 15:54
  • The shebang is #!/usr/bin/env python. – alex Feb 26 '13 at 15:55
  • @Junuxx: from the result of the command (see my post) to me it looks like I am using Python 2.7. – alex Feb 26 '13 at 15:56
  • 3
    @AlexanderDück: Please try running the following line in the command line: `/usr/bin/env python -c "import argparse; print argparse"`. (Note the difference with the line you ran before). – David Robinson Feb 26 '13 at 15:56
  • @DavidRobinson: Still the same result as above: – alex Feb 26 '13 at 15:58
  • Could you run `dpkg -l | grep libpython`? Also, are you running the script via a IDEor via CLI? – unddoch Feb 26 '13 at 15:59
  • Could you try running a minimal script? For example, running the following commands: `echo -e '#!/usr/bin/env python\nimport argparse; print argparse' > test.py; python test.py`. – David Robinson Feb 26 '13 at 16:00
  • Back again. Sorry @all! I did a stupid mistake - though I was on the machine where the script was not working, but actually I already exited the SSH session and was on my machine where the script works. Current situation is that 'python -c "import argparse; print argparse"' yields the same error as in my post above. – alex Feb 26 '13 at 17:05
  • I now ran "python" and it turns out on that machine the version is 2.6.6. Is there a way I can install python 2.7 without administrator privileges or alternatively just somehow use argparse in version 2.6.6? – alex Feb 26 '13 at 17:06
  • 3
    virtualenv with https://pypi.python.org/pypi/argparse should work. Or just extract the module from the package and put it somewhere in the path. – unddoch Feb 26 '13 at 17:34
  • @alex You might answer your own question with the correct solution and accept it afterwards. – glglgl Nov 07 '13 at 06:47

7 Answers7

22

Try installing argparse:

easy_install argparse
Qantas 94 Heavy
  • 15,750
  • 31
  • 68
  • 83
brdido
  • 351
  • 1
  • 3
  • 12
  • 3
    The user has already a Python installation with `argparse` installed in `/usr/lib/python2.7`. Why reinstall it a second time?... – Maxime Lorant Dec 26 '13 at 23:54
11

On CentOS I solved this with yum install python-argparse. HT to LVA for the correct package name.

Erik Veland
  • 111
  • 1
  • 4
7

On a Debian system you can use the following command to install the argparse package:

sudo apt-get install python-argparse
LVA
  • 71
  • 1
  • 2
5

You're probably using a different version of Python with your script than the one you execute in command line. Make sure that the script is using this interpretor: /usr/lib/python2.7. This installation has argparse for sure, as you proved it with the import on your first post.

Why your script can use a different Python installation? It can be the result of a Shebang line of the first line of your script that could pointed to a different Python interpretor which doesn't have the argparse module installed.

EDIT: Another problem can be that your script clean the sys.path list, and it would be very bad because every modules pre-installed wouldn't be accessible...

Maxime Lorant
  • 34,607
  • 19
  • 87
  • 97
  • Thanks. Instead of running my script as "./myscript.py" I changed and did "python ./myscript.py" and it ran correctly, since it used my PATH's Python – mdiehl13 Feb 09 '18 at 22:32
0

You don't have the module installed to the correct version of python.There is one of two ways you can fix this

  1. Reinstall python and the module
  2. Change python paths are demonstrated at one of these links (osx, windows(You shouldn't have to do this on windows I selected xp because that is what I run),linux

One of these should work but if it doesn't try rebooting. GOOD LUCK!! :)

Community
  • 1
  • 1
Marcello B.
  • 4,177
  • 11
  • 45
  • 65
0

If your source file has the same name with argparse, and you put it in the current directory with your scripts, you may encountered the problem.

liunx
  • 751
  • 4
  • 13
  • 32
  • I'm sorry I do not understand your answer at all. What do you mean by "Source file has the same name with argpass" ? – 8bitjunkie Oct 20 '15 at 12:19
0

Run this command: yum install -y python-argparse. It can fix it when you are CentOS.

anothernode
  • 5,100
  • 13
  • 43
  • 62
huang botao
  • 405
  • 6
  • 13