简体   繁体   English

无法在命令提示符下运行 .py 脚本

[英]Unable to Run .py script in command prompt

I know this is a super simple problem, but none of the other articles I have looked at have helped.我知道这是一个非常简单的问题,但我看过的其他文章都没有帮助。 I'm trying to run a hello.py file from command prompt and it is giving me this error message.我正在尝试从命令提示符运行 hello.py 文件,它给了我这个错误消息。

Microsoft Windows [Version 10.0.14393]
(c) 2016 Microsoft Corporation. All rights reserved.
C:\Users\Davidjb7>py hello.py
C:\Users\Davidjb7\AppData\Local\Programs\Python\Python36-32\python.exe: can't open file 'hello.py': [Errno 2] No such file or directory

I really don't know why it is though, because hello.py is within the directory Python36-32, as shown here.我真的不知道为什么会这样,因为 hello.py 位于 Python36-32 目录中,如下所示。

C:\Users\Davidjb7\AppData\Local\Programs\Python\Python36-32>dir


Volume in drive C is Windows8_OS
 Volume Serial Number is DC59-907F

 Directory of C:\Users\Davidjb7\AppData\Local\Programs\Python\Python36-32

03/09/2017  04:06 PM    <DIR>          .
03/09/2017  04:06 PM    <DIR>          ..
02/26/2017  10:14 AM    <DIR>          DLLs
02/26/2017  10:14 AM    <DIR>          Doc
03/09/2017  04:05 PM             1,021 hello.py
02/26/2017  10:14 AM    <DIR>          include
02/26/2017  10:14 AM    <DIR>          Lib
02/26/2017  10:14 AM    <DIR>          libs
12/23/2016  07:25 AM            30,049 LICENSE.txt
12/23/2016  07:10 AM           258,657 NEWS.txt
03/09/2017  04:06 PM    <DIR>          python
12/23/2016  07:21 AM            97,944 python.exe
12/23/2016  07:19 AM            52,888 python3.dll
12/23/2016  07:18 AM         3,258,008 python36.dll
12/23/2016  07:22 AM            96,408 pythonw.exe
12/23/2016  07:10 AM             8,434 README.txt
02/26/2017  10:15 AM    <DIR>          Scripts
02/26/2017  10:14 AM    <DIR>          tcl
02/26/2017  10:14 AM    <DIR>          Tools
06/09/2016  10:46 PM            83,784 vcruntime140.dll
               9 File(s)      3,887,193 bytes
              11 Dir(s)  49,034,211,328 bytes free

C:\Users\Davidjb7\AppData\Local\Programs\Python\Python36-32>

If someone could help me understand what I am doing wrong that would be awesome.如果有人能帮助我理解我做错了什么,那就太棒了。 And please be very clear in your descriptions, I'm not well versed.请在您的描述中非常清楚,我不精通。 (Yet I hope) (但我希望)

You have to run the python command in the directory that contains the file, or you must give the full path of the file.您必须在包含该文件的目录中运行 python 命令,或者您必须提供文件的完整路径。

If you wanted to run it as it was you would do:如果你想按原样运行它,你会这样做:

py C:\Users\Davidjb7\AppData\Local\Programs\Python\Python36-32\hello.py

If you move the file into your current working directory when programming, you should just be able to run py hello.py.如果您在编程时将文件移动到当前工作目录中,您应该能够运行 py hello.py。

You could also try this:你也可以试试这个:

  1. Find a .py file (any, even a blank).找到一个 .py 文件(任何,甚至是空白文件)。
  2. Right click on it and choose: "Open with>" and then select "Choose program...".右键单击它并选择:“打开方式>”,然后选择“选择程序...”。
  3. This pops up a list of all programs - select python, and check the box "Always use the selected program to open this kind of file" and then click OK.这会弹出所有程序的列表——选择python,并选中“始终使用所选程序打开此类文件”框,然后单击“确定”。

Checking this box resets file associations and fixes this problem for the command line.选中此框会重置文件关联并修复命令行的此问题。

The problem can arise again if you tell Windows to open up .py files in a text editor as default.如果您告诉 Windows 默认在文本编辑器中打开 .py 文件,则问题可能会再次出现。

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

相关问题 如何在命令提示符下运行 .py 文件 (Windows 7) - how to run .py files in command prompt (Windows 7) 尝试在命令提示符中从 IDLE 运行 .py 脚本时出现“SyntaxError: Invalid Syntax” - "SyntaxError: Invalid Syntax" when trying to run a .py script from IDLE in Command Prompt Python:无法使用Python脚本打开新的命令提示符会话并通过命令行(Windows)运行程序 - Python: Unable to open a new command prompt session and run a program through command line (windows) using Python script 脚本无法像在PyScripter中那样在命令提示符下运行 - Script does not run on command prompt as in PyScripter 使用python脚本在命令提示符下运行命令 - Run commands in command prompt using python script 如何从Windows命令提示符运行.py文件? - How to Run .py file from Windows Command Prompt? 如何打开命令提示符以及使用python脚本运行的命令? - How to open a command prompt along with a command to run using a python script? 无法在 anaconda 提示符或命令提示符下运行需要 gdal 模块的 a.py - Cannot run a .py which requires a gdal module in anaconda prompt nor in command prompt 无法在脚本中导入python模块,但可以在命令提示符下导入 - Unable to import python module in script, but can in command prompt 如何在命令提示符Windows 10中运行python脚本 - How to run python script in Command Prompt Windows 10
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM