简体   繁体   English

无法在 cmd 中执行带有 .py 扩展名的 python 脚本

[英]Not able to execute python script with .py extension in cmd

I have changed the directory to the relevant place where my file is stored, but still I'm not able to execute the script.我已将目录更改为存储文件的相关位置,但仍然无法执行脚本。

When I use "py Hell.py.txt" instead of "py Hell.py" it works.当我使用“py Hell.py.txt”而不是“py Hell.py”时,它可以工作。

Whats the issue here.这里有什么问题。 I saved it with .py extension as well.我也用 .py 扩展名保存了它。 Please check the link below to see the screenshot python script请检查下面的链接以查看屏幕截图python脚本

It's possible that you are using Windows and file extensions are hidden.您可能使用的是 Windows 并且隐藏了文件扩展名。 You might want to go to folder options and disable "hide file extensions" and see that the file you saved is indeed a text (.txt) file and not a Python (.py) file.您可能想要转到文件夹选项并禁用“隐藏文件扩展名”,并查看您保存的文件确实是文本 (.txt) 文件而不是 Python (.py) 文件。

It seems that your file name is actually "Hell.py.txt" not "Hell.py".看来您的文件名实际上是“Hell.py.txt”而不是“Hell.py”。 That's why the interpreter can't run "Hell.py", please check this and try to run it again.这就是解释器不能运行“Hell.py”的原因,请检查此并尝试再次运行它。

Just checking a few things只检查几件事

  1. What text editor are you using at the moment?你现在使用什么文本编辑器? As others have pointed out, if you're using notepad, it might have saved the python file as a .txt file instead.正如其他人指出的那样,如果您使用的是记事本,它可能会将 python 文件保存为 .txt 文件。

  2. What code did you input to get your output?你输入了什么代码来得到你的输出?

I've tried it on my own console using the following code with a file saved as hello.py.txt我已经在我自己的控制台上使用以下代码进行了尝试,文件保存为 hello.py.txt

print("hello hello")

I then got the following output after running the file然后我在运行文件后得到以下输出

hello hello

In the long run, it definitely pays to use a text editor like Atom or Sublime.从长远来看,使用 Atom 或 Sublime 等文本编辑器绝对值得。 I personally use atom for python and the packages and other support help me a lot in my coding :)我个人使用 atom for python 和包和其他支持在我的编码中帮了我很多:)

Hope this answered your question!希望这回答了您的问题!

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM