Python 导入错误:没有名为 argparse 的模块
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15093444/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ImportError: No module named argparse
提问by alex
I am trying to run a Python program but get the error
我正在尝试运行 Python 程序但出现错误
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
我在 StackOverflow 上找到了问题“cli中的 argparse Python 模块”并尝试了第一个评论,即运行命令
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 argparsemodule 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.pathis broken. I have no clue what that means, or how I can change its value. Any ideas?
对我来说,机器上似乎安装了 Python 2.7(我不是管理员)并且该argparse模块也存在。所以我想知道为什么找不到模块。在另一台机器上,脚本按原样运行。在上面提到的帖子中,有sys.path一条评论可能被破坏了。我不知道这意味着什么,或者我如何改变它的价值。有任何想法吗?
回答by Suit Boy Apps
You don't have the module installed to the correct version of python.There is one of two ways you can fix this
您没有将该模块安装到正确版本的 python。有两种方法可以解决此问题
- Reinstall python and the module
- 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!! :)
其中之一应该可以工作,但如果它不尝试重新启动。祝你好运!!:)
回答by liunx
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.
如果你的源文件和 argparse 同名,并且你把它和你的脚本放在当前目录下,你可能会遇到这个问题。
回答by brdido
Try installing argparse:
尝试安装argparse:
easy_install argparse
回答by Maxime Lorant
You're probably using a different versionof 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 argparsefor sure, as you proved it with the import on your first post.
您在脚本中使用的 Python版本可能与在命令行中执行的版本不同。确保脚本正在使用此解释器:/usr/lib/python2.7. 这个安装argparse是肯定的,正如你在第一篇文章中的导入所证明的那样。
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 argparsemodule installed.
为什么您的脚本可以使用不同的 Python 安装?它可能是脚本第一行的 Shebang 行的结果,该行可能指向未argparse安装模块的不同 Python 解释器。
EDIT: Another problem can be that your script clean the sys.pathlist, and it would be very bad because every modules pre-installed wouldn't be accessible...
编辑:另一个问题可能是您的脚本清理了sys.path列表,这将非常糟糕,因为预安装的每个模块都无法访问...
回答by LVA
On a Debian system you can use the following command to install the argparse package:
在 Debian 系统上,您可以使用以下命令安装 argparse 包:
sudo apt-get install python-argparse
回答by Erik Veland
On CentOS I solved this with yum install python-argparse.
HT to LVA for the correct package name.
在 CentOS 上,我用yum install python-argparse. HT 到 LVA 以获得正确的包名称。
回答by huang botao
Run this command: yum install -y python-argparse. It can fix it when you are CentOS.
运行此命令:yum install -y python-argparse。当您是 CentOS 时,它可以修复它。

