简体   繁体   English

使用鼠标右键>>在此处命令提示符后执行脚本时,Python无法正常工作

[英]Python doesn't work properly when I execute a script after using Right Click >> Command Prompt Here

This is a weird bug. 这是一个奇怪的错误。 I know it's something funky going on with my PATH variable, but no idea how to fix it. 我知道PATH变量有点时髦,但不知道如何解决。

If I have a script C:\\Test\\test.py and I execute it from within IDLE, it works fine. 如果我有脚本C:\\ Test \\ test.py并从IDLE内执行,则可以正常工作。 If I open up Command Prompt using Run>>cmd.exe and navigate manually it works fine. 如果我使用Run >> cmd.exe打开命令提示符并手动导航,则效果很好。 But if I use Windows 7's convenient Right Click on folder >> Command Prompt Here then type test.py it fails with import errors. 但是,如果我使用Windows 7的便捷功能,请右键单击文件夹>>“命令提示符”,然后在此处键入test.py,它将失败,并出现导入错误。

I also cannot just type "python" to reach a python shell session if I use the latter method above. 如果我使用上面的后一种方法,我也不能只是键入“ python”来到达python shell会话。

Any ideas? 有任何想法吗?

Edit: printing the python path for the command prompt that works yields the correct paths. 编辑:为有效的命令提示符打印python路径会产生正确的路径。 Printing it on the non-working "Command prompt here" yields: Environment variable python not defined". 在不工作的“此处的命令提示符”上打印它会产生:“未定义环境变量python”。

First of all, I work on Windows7 (among others) and running python from the command line works for me using "Command Prompt Here". 首先,我在Windows7(及其他)上工作,并使用“命令提示符”在命令行运行python Make sure you have the directory containing python.exe in your PATH environment variable, by running "Command Prompt Here" and running set . 通过运行“此处命令提示符”并运行set ,确保在PATH环境变量中具有包含python.exe的目录。

Now for import errors. 现在是导入错误。 When importing, Python looks for modules in directories specified in the sys.path list. 导入时,Python在sys.path列表中指定的目录中查找模块。 The PYTHONPATH environment variable is added to this list, along with some default directories, and the directory of the given Python script . PYTHONPATH环境变量以及一些默认目录和给定Python脚本的目录被添加到此列表中。 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. 但是,在IDLE中,此目录是IDLE的目录,因此与从命令行运行它们相比,这可能导致您从IDLE运行事物时看到的差异。

See http://docs.python.org/tutorial/modules.html#the-module-search-path for details. 有关详细信息,请参见http://docs.python.org/tutorial/modules.html#the-module-search-path

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. 您没有提到要接收的导入错误,而是尝试在IDLE中运行脚本并检查有问题的模块的.__file__属性以查看它们的位置。 Then compare the sys.path from inside IDLE to sys.path you get when running Python from the command line. 然后将IDLE内部的sys.path与从命令行运行Python时获得的sys.path进行比较。 This should give you the information required to resolve your import errors. 这应该为您提供解决导入错误所需的信息。

I don't use Windows much, but maybe when you open Right Click -> Command Prompt, the PATH is different from navigate manually. 我使用的Windows很少,但是也许当您打开“右键单击->命令提示符”时,PATH与手动导航不同。 First try to print your PATH (oh I have no ideal how to do this) and see if it different in 2 situation. 首先尝试打印您的PATH(哦,我不理想如何执行此操作),然后查看它是否在2种情况下有所不同。

You can check the currently present enviroment variables with the "set" command on the command line. 您可以在命令行上使用“ set”命令检查当前存在的环境变量。 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. 为了使python正常工作,您至少需要指向python库的PYTHONPATH,并且python.exe的路径应包含在PATH变量中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么有时在 PowerShell 的“python -c [command]”(以及命令提示符)中使用单引号将命令括起来时不起作用? - Why doesn't it work sometimes when using single quotes to surround the command in `python -c [command]` in PowerShell (and in command prompt as well)? docker python脚本无法正确执行 - docker python script doesn't execute properly 在命令提示符下使用 for 循环执行 python3 脚本 - Execute python3 script using for loop in command prompt 命令提示符中的 Python 不起作用,但在 python 空闲时它可以 - Python in command prompt doesn't work, but in python idle it does 为什么我不能在这里模拟右键? (使用Python) - Why can't I simulate a right click here? (with Python) 为什么这在Python IDLE shell中有效,但在我从命令提示符下作为Python脚本运行时却没有? - Why does this work in the Python IDLE shell but not when I run it as a Python script from the command prompt? Python脚本无法在命令提示符下打印输出 - Python script doesn't print output in command prompt 双击Python脚本不起作用 - Python script doesn't work with double click 命令提示符中的“python”不起作用(Windows 10) - "python" in command prompt doesn't work (windows 10) 如何从python中的命令提示符执行脚本 - How execute script from command prompt in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM