ImportError: No module named argparse
Asked Answered
S

7

27

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?

Sievert answered 26/2, 2013 at 15:50 Comment(13)
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?Triciatrick
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?Paphian
We'll need to see more details on the script itself. What executable is used to run it, for example?Brutal
The shebang is #!/usr/bin/env python.Sievert
@Junuxx: from the result of the command (see my post) to me it looks like I am using Python 2.7.Sievert
@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).Paphian
@DavidRobinson: Still the same result as above: <module 'argparse' from '/usr/lib/python2.7/argparse.pyc'>Sievert
Could you run dpkg -l | grep libpython? Also, are you running the script via a IDEor via CLI?Ursel
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.Paphian
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.Sievert
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?Sievert
virtualenv with pypi.python.org/pypi/argparse should work. Or just extract the module from the package and put it somewhere in the path.Ursel
@Sievert You might answer your own question with the correct solution and accept it afterwards.Define
A
22

Try installing argparse:

easy_install argparse
Aintab answered 26/12, 2013 at 15:42 Comment(1)
The user has already a Python installation with argparse installed in /usr/lib/python2.7. Why reinstall it a second time?...Whippet
T
11

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

Toffeenosed answered 27/4, 2016 at 23:27 Comment(0)
P
7

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

sudo apt-get install python-argparse
Pappus answered 3/11, 2015 at 15:45 Comment(0)
W
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...

Whippet answered 26/12, 2013 at 23:50 Comment(1)
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 PythonNetti
F
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!! :)

Fishback answered 3/9, 2013 at 4:47 Comment(0)
P
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.

Pee answered 3/9, 2013 at 4:52 Comment(1)
I'm sorry I do not understand your answer at all. What do you mean by "Source file has the same name with argpass" ?Jollify
M
0

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

Mcroberts answered 29/6, 2018 at 9:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.