'How to convert .py to .exe and run as a background function on windows while retaining args?
I'm looking to run a python script that calls commands through subprocess depending on various arguments that are given to it. For ease of use it would be valuable to be able to use it as an executable that is run from cmd. I'm wondering if it's possible to convert from .py to .exe, run the program from cmd using "mypycode.exe args1 args2", and then to have that run in the background and be able to close cmd while it continues to run?
Using subprocess.DEVNULL I was able to stop the process from opening new cmd windows whenever the code looped however if I close out of the cmd window the program exits.
I've also looked at using Popen from subprocess however it seems that it might remove the ability to convert to .exe and possibly the ability to pass args unless I'm understanding it incorrectly.
I've also tried simply using pyinstaller using the -w flag to run the process without a cmd window popup however this crashes the program and gives an error "Failed to execute script mypycode".
I'm looking to be able to call the .exe from a cmd and essentially start the process and allow me to continue using cmd or close it and have the process continue in the background.
So far I either can't use cmd after calling mypycode.exe and also kill the process when cmd is closed, or I get the previously mentioned error that it failed to execute.
Solution 1:[1]
If it is of some help this is how I brought i an argument to my EXE-file whit Pyton. The program checks if there is a argument wich it is not when I'm in development mode, but when I run the EXE I can run it with an argument and the variable myPath is populated from the "outside":
import sys
if len(sys.argv) < 2:
myPath = ''
else:
myPath = sys.argv[1]
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Michael Larsson |