【问题标题】:Python Errno2 keeps occuringPython Errno 2 不断发生
【发布时间】:2017-08-09 10:19:20
【问题描述】:

所以我最近开始浏览 Zed Shaw 的 LPTHW,在练习 1 中它要求在 CMD 中运行 ex1.py,我不断收到以下错误消息:

python: 无法打开文件 'ex1.py': [Errno 2] 没有这样的文件或目录。

我已阅读其他答案并按照其中的说明进行操作,并将以下路径添加到系统变量中:

  • 路径

  • %PY_HOME%;%PY_HOME%\Lib;%PY_HOME%\DLLs;%PY_HOME%\Lib\lib-tk;

  • PY_HOME
  • C:\Python27
  • PYTHONPATH
  • %PY_HOME%\Lib;%PY_HOME%\DLLs;%PY_HOME%\Lib\lib-tk;C:\another-library

此外,当我在 CMD 或 PowerShell 中键入 Python 时,它会返回我认为应该出现的三个 >>> 提示。

我不确定还有什么可以添加或编辑以允许我在 CMD 或终端中运行 Python 脚本。非常感谢任何帮助。

【问题讨论】:

  • 在练习 1 中,他警告不要使用 IDE 或 IDLE。所以我猜你使用了记事本并且在保存时没有更改文件类型,文件实际上保存为ex1.py.txt,但是你在资源管理器中打开了“隐藏已知类型的文件扩展名”,所以看起来它被称为ex1.py?否则,提供它的完整路径,例如c:\> python c:\users\username\documents\python\ex1.py 或其他任何地方,看看它是否运行?

标签: python python-2.7 powershell cmd


【解决方案1】:

当您打开一个 shell(cmd.exe 或 powershell.exe)时,如果您可以键入 python 并获得“>>>”提示,那么您的路径是可以的。在 shell 中,使用 dir 命令查看您的文件是否存在:

C:\users\jsmith> dir

Directory: C:\users\jsmith

Mode                LastWriteTime     Length Name
----                -------------     ------ ----
-a----         8/5/2016  11:33 PM       3331 script5.py

如果您没有看到该文件 (ex1.py),您需要弄清楚您保存它的位置。或者这可能是@TessellatingHeckler 所说的。您可以通过右键单击文件并选择编辑来使用“开始”菜单中的“最近项目”打开 ex1.py 吗?如果是这样,您可以从菜单中选择文件并另存为以找到保存文件的目录。然后使用 shell 中的路径在 python 中加载它:

python "C:\Users\jsmith\My Documents\ex1.py"

故障排除步骤:

  • 您的 shell 使用的是 cmd.exe 还是 powershell.exe?
  • 打开你的shell后,运行命令:pwd
  • 运行命令:dir
  • 使用完整路径执行 Python 脚本:python "C:\Users\jsmith\My Documents\ex1.py"
  • 现在切换到脚本所在的目录:cd "C:\Users\jsmith\My Documents\"
  • 执行:密码
  • 执行:目录
  • 执行:python ex1.py
  • 逐字粘贴所有输出。您需要将其粘贴为代码块(每行前面必须有四个空格)。

示例输出:

  • 运行 powershell.exe

C:\users\jsmith> 密码

路径:

C:\users\jsmith

C:\users\jsmith> dir                                

Directory: C:\users\jsmith                      

Mode                LastWriteTime     Length Name                  
----                -------------     ------ ----                  
d-----        2/15/2017   3:54 PM            Desktop
(...) 

C:\users\jsmith> python "C:\apps\working\python\general\input-intro1.py"

Enter your name:  George

Hello George

cd \apps\working\python\general

C:\apps\working\python\general> pwd

Path:

C:\apps\working\python\general

C:\apps\working\python\general> dir                                

Directory: C:\apps\working\python\general                      

Mode                LastWriteTime     Length Name                  
----                -------------     ------ ----                  
d-----        2/15/2017   3:54 PM            sprintintro           
-a----        10/6/2015   9:18 PM        222 input-intro1.py    

C:\users\jsmith> python input-intro1.py\
(show error message...)

如果对此有任何疑问,请告诉我--Jim

【讨论】:

  • 使用 CMD 中的完整文档位置并使用 Python 命令在 CMD 中运行文档,谢谢。有没有办法通过 dir 和 cd 命令运行它?我可以使用这些命令导航到该文件夹​​,但是当我运行 python ex1.py 时,它会再次返回 Errno 2 消息。你知道有什么问题吗?
  • @YusufBismillah - 请查看我上面修改后的答案并提供所需的输出。
  • @YusufBismillah - 当您执行任何程序(例如 Python)并告诉它加载文件(例如,您的脚本“ex1.py”)时,它会在当前工作目录中查找。这通常是您启动程序的目录。如果您尝试加载的文件/脚本不在当前工作目录中,则需要提供相对或绝对路径才能到达它。例如,python directory1\ex1.py 或 python "C:\Users\jsmith\My Documents\Directory1\ex1.py"。
猜你喜欢
  • 2010-10-28
  • 2020-07-20
  • 2015-05-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多