简体   繁体   中英

Running python scripts from windows 10 execute line (WIN + R panel)

I am struggling with an apparently obvious issue. I've been practising python and I have a few scripts and I am trying to run them from windows 10 execute command ( WIN + R ).

I am doing this by creating a BATCH file of the script, and in theory the file should run with a simple "py script" in the WIN+R window. But it does not, it simply flashes the CMD window for a sec and then disappear.

Everything seems to be set up correctly, I can run the script from CMD, BATCH file are OK (they run with a double click, I included the @pause at the end), the folder where BATCH are is in the env. variables PATH.

So i really don't get what is wrong. Note that If I type the full path in the WIN+R window it works , but that takes quite some time..

Any hints?

A BIG thank you :)

EDIT: thanks for the replies, here is the batch:

@py script.py
@pause

It works on double click.

Ctrl + R => opens run window.

Type in "cmd"

If you have python installed with Path.

python <yourscript.py>

should run just fine.

Otherwise, Run as below.

path/to/python/python.exe <yourscript.py>

Hope you did the same as above. If the bat runs and closes in a flash. That is not an issue. It happens.

Have a sleep in your bat as well. If you're running on bat.

Add below after your python script execution.

It will make your bat script wait for a thirty seconds before closing and you can verify your stdout.

timeout /t 30

On Windows, executing Python scripts directly, ie without calling python before, can be tricky. Often Windows will just open your default Python editor to show you the file, because the .py extension is registered to this editor.

see this post for more info: How to execute Python scripts in Windows?

You should create a batch file as follows...

@py -3 E:\\FOLDER\\SUBFOLDER\\FILE.py %* @pause

I have both python 2 and python 3 so I use py -3 as for me py will run Python 2 The ending %* is important

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