在不指定“.py"的情况下使 Python 脚本在 Windows 上运行扩大 [英] Making Python scripts run on Windows without specifying ".py" extension

查看:48
本文介绍了在不指定“.py"的情况下使 Python 脚本在 Windows 上运行扩大的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够在包含 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:\Python27\python.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.

这篇关于在不指定“.py"的情况下使 Python 脚本在 Windows 上运行扩大的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆