Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python错误2不断出现_Python_Python 2.7_Powershell_Cmd - Fatal编程技术网

Python错误2不断出现

Python错误2不断出现,python,python-2.7,powershell,cmd,Python,Python 2.7,Powershell,Cmd,因此,我最近开始阅读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 皮尤之家酒店 C:\Python27 蟒蛇 %PY_HOME%\Lib;%PY_HOME

因此,我最近开始阅读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

  • 皮尤之家酒店
  • C:\Python27
  • 蟒蛇
  • %PY_HOME%\Lib;%PY_HOME%\DLLs;%PY_HOME%\Lib\Lib tk;C:\另一个库
此外,当我在CMD或PowerShell中键入Python时,它会返回我认为应该出现的三个>>>提示

我不确定还有什么地方可以添加或编辑,以允许我在CMD或terminal中运行Python脚本。非常感谢您的帮助。

当您打开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),则需要找出保存该文件的位置。或者它可能是@Tessellingheckler所说的。通过右键单击文件并选择“编辑”,可以使用“开始”菜单中的“最近使用的项目”打开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\”
  • 执行:pwd
  • 执行:dir
  • 执行:python ex1.py
  • 逐字粘贴所有输出。您需要将其粘贴为代码块(每行前面必须有四个空格)
示例输出:

  • 正在运行powershell.exe
C:\users\jsmith>pwd

路径:

C:\users\jsmith


如果对此有任何问题,请告诉我——Jim在练习1中警告不要使用IDE或IDLE。所以我猜您使用了记事本,在保存时没有更改文件类型,文件实际上保存为
ex1.py.txt
,但是您在资源管理器中有“隐藏已知类型的文件扩展名”,所以它看起来像是
ex1.py
?否则,请提供它的完整路径,例如
c:\>python c:\users\username\documents\python\ex1.py
或其他任何位置,然后查看是否运行该路径?使用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”。
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...)