简体   繁体   中英

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? Maybe in a .bat file?

sorry about my poor English, and if I insist on subjects you already master, it's my first constructive answer here ;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

  • have a cmd.exe window open after this action with your python script interpreted

  • have a way to review this scipt output without relying on superman eyes able to gasp 65536 characters per millisecond

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 !" 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.

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. 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".

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. 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 :

  • 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" .

  • %X : to reference command line arguments sent to your script (%1 for the first one, then %2 etc.,)

  • 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

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 :

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

Save it somewhere, associate it with .py files, and you are done. 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.

Or you can simple associate your .py files with standard python.exe which will show the console output.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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