简体   繁体   English

如何将.py文件与以`py`然后文件名打开CMD关联?

[英]How to associate .py files to open CMD with `py` then the filename?

How to associate .py files to open CMD with py then the filename? 如何关联.py文件以使用py然后使用文件名来打开CMD? Maybe in a .bat file? 也许在.bat文件中?

sorry about my poor English, and if I insist on subjects you already master, it's my first constructive answer here ;p 对我的英语不好感到抱歉,如果我坚持你已经精通的科目,这是我在这里的第一个建设性答案; p

I'm not sure about what you want to achieve but from your question and its tags I assume tha you want to : 我不确定您要实现什么目标,但是根据您的问题及其标签,我认为您想要实现以下目标:

  • run ".py" file containing a python script from the file explorer by double clicking it 通过双击从文件资源管理器运行包含Python脚本的“ .py”文件

  • have a cmd.exe window open after this action with your python script interpreted 在执行此操作后打开cmd.exe窗口,并解释您的python脚本

  • have a way to review this scipt output without relying on superman eyes able to gasp 65536 characters per millisecond 有一种方法可以查看此scipt输出,而无需依靠超人的眼睛能够每毫秒吸收65536个字符

So basically, if you have a script printing "Hello World !", you want to click on it, and see in a cmd.exe window the text "Hello World !" 因此,基本上,如果您有打印“ Hello World!”的脚本,则要单击它,然后在cmd.exe窗口中看到文本“ Hello World!”。 displayed to validate that your script is working properly ? 显示以验证您的脚本是否正常工作? To make it short you are RIGHT, a .bat file will be enough to do the trick, even if there is a whole bunch of alternatives including executable generation to embed a full python interpreter (see http://www.py2exe.org/ ), or simply adding a wait loop at the end of your script, but having a batch script associated is probably the lightest and easiest solution in your case. 简而言之,即使有很多替代方法,包括可执行文件生成来嵌入完整的python解释器,也可以使用.bat文件完成所有操作(请参见http://www.py2exe.org/ ),或者只是在脚本的末尾添加一个等待循环,但是关联批处理脚本可能是您所用的最轻便,最简单的解决方案。

As you figured out, associating .py files with the python interpreter will run your scripts but the console window will dissapear immediatly on completion without letting you the time to consider the output. 如您所知,将.py文件与python解释器相关联将运行您的脚本,但控制台窗口将在完成后立即消失,而不会让您有时间考虑输出。 You just need to associate .py files (right click -> open with, if you want to do it programatically it's possible to set this in the windows registry) with a .bat script that will do the job, that is, run the script and wait until you are ready to "leave". 您只需要将.py文件(右键单击->打开,如果要以编程方式进行设置,可以在Windows注册表中进行设置)与将执行此工作的.bat脚本关联,即运行该脚本然后等到您准备好“离开”。

This batch script will take the python script you clicked on as an argument, run it with your python interpreter and pause it's execution, waiting for your input before leaving. 此批处理脚本将您单击的python脚本作为参数,使用python解释器运行它并暂停执行,等待输入后再离开。 Since the default windows file association will execute your target program and pass it the file executed (should it be a click or a "start XXX" command) it's pretty straightforward, the bricks to do this in batch are : 由于默认的Windows文件关联将执行您的目标程序并将执行的文件传递给它(应该是单击还是“开始XXX”命令),因此非常简单,批量执行此操作的步骤如下:

  • program_name argument : to directly call an external command, so "python my_script.py" will run the python.exe program (no need to add the ".exe" or "'.com" part since it's an obvious case for windows) with my_script.py as argument, provided that your python executable directory is in your PATH environment variable, otherwise you will have to provide the full path, ie: "C:\\Python27\\python.exe my_script.py" . program_name参数:直接调用外部命令,因此“ python my_script.py”将运行python.exe程序(无需添加“ .exe”或“'.com”部分,因为Windows很明显)。如果您的python可执行文件目录位于PATH环境变量中,则将my_script.py作为参数,否则,您将必须提供完整路径,即:“ C:\\ Python27 \\ python.exe my_script.py”。

  • %X : to reference command line arguments sent to your script (%1 for the first one, then %2 etc.,) %X:引用发送到脚本的命令行参数(第一个为%1,然后为%2等)

  • pause : a command that will display the message "Press any key to continue ...", and obviously wait for any key before leaving your script 暂停:该命令将显示消息“按任意键继续...”,并且显然在退出脚本之前等待任何键

  • evantually, @echo off : to avoid printing each batch command before its execution 有利地,@echo off:避免在执行每个批处理命令之前打印每个批处理命令

So, assuming that your python interpreter is installed in C:\\Python27 (please replace with whatever version / location for your python.exe, or just "python" if it's in your PATH) your batch script could look like something like this : 因此,假设您的python解释器安装在C:\\ Python27中(请替换为python.exe的任何版本/位置,或者如果在PATH中,则替换为“ python”),则批处理脚本应类似于以下内容:

@echo off
C:\Python27\python.exe %1
pause

Save it somewhere, associate it with .py files, and you are done. 将其保存在某个位置,并将其与.py文件关联,即可完成操作。 HTH 高温超导

You can do it in two separate ways: 您可以通过两种不同的方式来做到这一点:

First, you can rename your .py file to .pyw and just open it and the script would be executed immediately (with pythonw.exe ) but this is not showing you a console output. 首先,您可以将.py文件重命名为.pyw ,然后将其打开即可立即执行脚本(使用pythonw.exe ),但这并未向您显示控制台输出。

Or you can simple associate your .py files with standard python.exe which will show the console output. 或者,您可以将.py文件与标准python.exe简单关联,这将显示控制台输出。

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

相关问题 如何将python与bash中的py文件相关联 - How do I associate python with py files in bash 如何使用“ python”命令代替py -3在cmd中打开python - How to open python in cmd with the 'python' command in stead of py -3 如何使.py文件直接打开到空闲 - How to make .py files open directly into Idle 如何在 Windows 10 上打开和查看 Python PY 文件? - How do I open and view Python PY files on Windows 10? 如何在不同的行中制作 Vim open.py 文件? - How can I make Vim open .py files in different lines? 如何打开具有两个py文件的api资源管理器绑定? - how to open api explorer bind with two py files? 如何仅使用python脚本在CMD上显示.py? - How to display a .py on CMD with only python script? 如何使 Discord.py ping cmd - How to make a Discord.py ping cmd 安装了多个 Python 版本:如何为 CMD 和“打开方式”设置 py.exe(Windows 的 Python 启动器)的默认版本 - Multiple Python versions installed : how to set the default version for py.exe (Python Launcher for Windows) for CMD and for "Open with" 我有两个文件 LOGIN.py 和 LOGINCLICK.py。 如何通过单击按钮从 LOGINCLICK.py 打开 LOGIN.py 文件? - I have two files LOGIN.py and LOGINCLICK.py. How to open LOGIN.py file from LOGINCLICK.py by clicking a button?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM