alex
alex

Reputation: 2482

ImportError: No module named argparse

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?

Upvotes: 27

Views: 110954

Answers (7)

huang botao
huang botao

Reputation: 416

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

Upvotes: 0

Erik Veland
Erik Veland

Reputation: 111

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

Upvotes: 11

LVA
LVA

Reputation: 71

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

sudo apt-get install python-argparse

Upvotes: 7

Maxime Lorant
Maxime Lorant

Reputation: 36161

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

Upvotes: 5

brdido
brdido

Reputation: 351

Try installing argparse:

easy_install argparse

Upvotes: 22

liunx
liunx

Reputation: 761

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.

Upvotes: 0

Marcello B.
Marcello B.

Reputation: 4440

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!! :)

Upvotes: 0

Related Questions