使 Python 脚本在 Windows 上运行而不指定“.py";延期

2022-01-18 00:00:00 python windows windows-7

问题描述

我希望能够在包含 python 脚本的文件夹中打开命令提示符,只需键入不带 .py 的脚本名称并观察它运行.

I want to able to open a command prompt at the folder which contains a python script and just type in the script name without the .py and watch it run.

按照谷歌上的各种提示来做这件事,我这样做了:

Following the various tips on google to do this, I do:

  1. 将 python.exe 添加到我的路径
  2. 将 .py 添加到 PATHEXT
  3. 尝试在浏览的窗口中打开 .py 文件.当提示:你想打开什么程序?我导航到我的 python.exe

应该发生的情况是 python.exe 应该添加到打开方式"弹出窗口中,但不是吗?我重新尝试使用 python.exe 离开我的路径.同样的问题.

What should happen is the python.exe should be added to the 'Open With' pop up, but it is not? I re try with the python.exe off my path. Same problem.

请注意,每次我在控制面板中设置路径.python的版本是2.7.为了让事情变得更奇怪,我可以将 .py 程序与 pythonw 相关联——但这没有用,因为我想要控制台.

Note every time I set a path it is in the control panel. Version of python is 2.7. And to make things stranger, I can associate .py programs with pythonw - but this is no use, as I want the console.

任何帮助表示赞赏.


解决方案

如前所述将 .PY 添加到 PATHEXT

Add .PY to PATHEXT as noted before

然后做:

assoc .py=Python.File
ftype Python.File=c:Python27python.exe "%1" %*

在命令提示符下执行脚本或在资源管理器中双击不需要将 python 添加到路径中,仅当您想启动交互式 python 会话或使用 python yourscript.py 运行脚本时代码>

Adding python to the path isn't necessary to execute the script in a command prompt or double clicking in Explorer, only if you want to start an interactive python session or running the script with python yourscript.py

参见 http://docs.python.org/2/using/windows.html 了解更多详情.

See http://docs.python.org/2/using/windows.html for more details.

相关文章