当我使用“右键单击>>”后执行脚本时,Python 无法正常工作命令提示符在这里
这是一个奇怪的错误。我知道我的 PATH 变量发生了一些奇怪的事情,但不知道如何修复它。
如果我有一个脚本 C:\Test\test.py 并且我从 IDLE 中执行它,它工作得很好。如果我使用“运行”>“cmd.exe 打开命令提示符并手动导航,它可以正常工作。但如果我使用 Windows 7 方便的右键单击文件夹>>命令提示符然后在此处输入 test.py,它会失败并出现导入错误。
如果我使用上面的后一种方法,我也不能只输入“python”来访问 python shell 会话。
有什么想法吗?
编辑:打印有效的命令提示符的 python 路径会产生正确的路径。在非工作的“命令提示符此处”上打印它会产生:环境变量 python 未定义”。
This is a weird bug. I know it's something funky going on with my PATH variable, but no idea how to fix it.
If I have a script C:\Test\test.py and I execute it from within IDLE, it works fine. If I open up Command Prompt using Run>>cmd.exe and navigate manually it works fine. But if I use Windows 7's convenient Right Click on folder >> Command Prompt Here then type test.py it fails with import errors.
I also cannot just type "python" to reach a python shell session if I use the latter method above.
Any ideas?
Edit: printing the python path for the command prompt that works yields the correct paths. Printing it on the non-working "Command prompt here" yields: Environment variable python not defined".
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
首先,我在 Windows7(以及其他)上工作,并且使用“命令提示符此处”从命令行运行
python
对我来说很有效。通过运行“命令提示符”并运行set
,确保 PATH 环境变量中包含包含 python.exe 的目录。现在是导入错误。导入时,Python 在 sys.path 列表中指定的目录中查找模块。 PYTHONPATH 环境变量以及一些默认目录以及给定 Python 脚本的目录将添加到此列表中。但是,在 IDLE 中,此目录是 IDLE 的目录,因此这可能会导致您在从 IDLE 运行内容与从命令行运行内容时看到的差异。
请参阅 http://docs.python.org/tutorial/modules .html#the-module-search-path 了解详细信息。
以下是我关于如何解决此问题的建议。您没有提到您收到的导入错误,但尝试在 IDLE 中运行脚本并检查有问题的模块的
.__file__
属性以查看它们在哪里。然后将 IDLE 内部的 sys.path 与从命令行运行 Python 时获得的 sys.path 进行比较。这将为您提供解决导入错误所需的信息。First of all, I work on Windows7 (among others) and running
python
from the command line works for me using "Command Prompt Here". Make sure you have the directory containing python.exe in your PATH environment variable, by running "Command Prompt Here" and runningset
.Now for import errors. When importing, Python looks for modules in directories specified in the sys.path list. The PYTHONPATH environment variable is added to this list, along with some default directories, and the directory of the given Python script. However, in IDLE this directory is the directory of IDLE, so this could be causing the difference you are seeing when running things from IDLE compared to running them from the command line.
See http://docs.python.org/tutorial/modules.html#the-module-search-path for details.
Here is my advice on how to resolve this issue. You didn't mention what import errors you are recieving, but try running the script inside IDLE and checking the problematic modules'
.__file__
attribute to see where they are. Then compare the sys.path from inside IDLE to sys.path you get when running Python from the command line. This should give you the information required to resolve your import errors.我不太使用Windows,但也许当你打开右键->命令提示符,路径与手动导航不同。首先尝试打印你的路径(哦,我不知道如何做到这一点),看看它在两种情况下是否不同。
I don't use Windows much, but maybe when you open Right Click -> Command Prompt, the PATH is different from navigate manually. First try to print your PATH (oh I have no ideal how to do this) and see if it different in 2 situation.
您可以使用命令行上的“set”命令检查当前存在的环境变量。为了让 python 工作,你至少需要 PYTHONPATH 指向你的 python 库,并且 python.exe 的路径应该包含在你的 PATH 变量中。
You can check the currently present enviroment variables with the "set" command on the command line. For python to work you need at least PYTHONPATH pointing to your python libs and the path to python.exe should be included in your PATH variable.