Batch file open->closes nothing happens - python

I wrote this bat file to run a program i wrote in python
#echo off
python C:\Project\myProg\main.py
pause
but nothing happens, when i click it twice just open closes nothing runs.
If I open a cmd and type just python C:\Project\myProg\main.py it works and runs.
I tried to Run as Administrator, but I get the same result.
Any ideas?

Related

I can't get pyw file to run for some reason

I created a python file where an event loop is running.
During this event loop, pressing certain hotkeys causes interaction with another app.
If I open the file and run it through PyCharm, it works.
If I run the file in a terminal or as a .py file, the code exits prematurely for some reason so I have to run it with the python -i command or add input() at the end of the code so that the event loop keeps running for the hotkeys to work. Is there a reason why that happens?
Now I would like to autostart this file every time Windows runs. In theory it should be easy:
Change it to .pyw and run it with pythonw.
However, if I do that and click on the file to execute, it doesn't work. No idea what is happening in the background.
If I run the file in a terminal like this: python main.pyw
the script is executed in the terminal, I can see it and the hotkeys work.
Why is .pyw not working for me here? What other alternative do I have to run the file in the background so that I can always use the hotkeys whenever I boot Windows?

Pythonw batch file still shows console

I have created a Python script for which I don't want to see the console pop-up every time it's being ran. I tried the pythonw.exe + .pyw approach, and while the script works just fine, the console still pops-up regardless.
.bat file content:
"C:\Users\mariu\anaconda3\pythonw.exe" "C:\Users\mariu\test.pyw" pause
And this is the inevitable console:
Is there something am I missing?
I had the same issue and moved to powershell script, since my tasks needed to start in parallel, and batch script seems not to behave like that, even with pythonw.exe
A .ps1 with the lines below should do the work
& "C:\Users\mariu\anaconda3\pythonw.exe" "C:\Users\mariu\test.pyw"
exit

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, i.e. 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

python script exceutes bat file: commands not recogned, identical bat file executed by user runs without issue

I am trying to get python27 to execute a very simple bat file (this code is simplified/striped down to a single line). The bat file contains:
C:/Users/dave/Desktop/vlc-2.2.6/vlc.exe
the python code to create and run the bat file is
import subprocess
f = open('myfile.bat', 'w')
f.write('C:/Users/dave/Desktop/vlc-2.2.6/vlc.exe')
f.close()
subprocess.Popen('C:/Users/dave/Desktop/myfile.bat', stdout=subprocess.PIPE)
Running the python script using idle, vlc opens.
Double clicking the bat file, vlc opens.
Double clicking the python script the cmd window opens and closes instantly, vlc command is not executed
I have caught the error once by pressing the pause break button and it stated vlc is not a recognised internal or external command, obviously it is when the bat file is called via idle or by myself.
I am perplexed and new to this style of scripting.
Any adivce would be greatly appreciated.
Many thanks
Dave
SOLVED well sort of
I decided to uninstall all python versions and reinstall python27 64bit.
Without restart;
The commands are being executed I only get 5 identical errors stating:
The process tried to write to a nonexistent pipe
My code is obviously wrong!
But, vlc opens!

The output of a python script

So I am trying to debug some Python code that I am running, because for some reason when I run the code in a command line it works perfectly, but when I make it a .py file and run it that way it fails.
The output is in a Windows Commander window, and it disappears before I can read it.
Is there a way to see what this output is?
Open a windows commander, and then go to the dir where a.py is, run "python a.py", it will not disappear!

Categories

Resources