0

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.

Aaron Lind
  • 9
  • 1
  • 3
  • I think your question is asking multiple things. For the executable, look at: https://stackoverflow.com/questions/27784271/how-can-i-use-setuptools-to-generate-a-console-scripts-entry-point-which-calls, for the background issue have a look at: https://superuser.com/questions/1069972/windows-run-process-on-background-after-closing-cmd – NOhs Jul 22 '19 at 13:17
  • Unfortunately I don't believe either of these solve my issues, the first deals with packaging .py files using setuptools which isn't necessary as I only have one file which needs to be an .exe rather than and .exe that refers to a package. The second link explains how to hide the cmd window but requires significantly more effort than preferred (ideally I'd like to simply call "mypycode.exe args1 args2" from its location). WScript also appears to not want to work on my system and it's probably best I don't open a further question to solve that issue. – Aaron Lind Jul 22 '19 at 13:39
  • Show us what you tried so far. – Masoud Rahimi Jul 22 '19 at 15:38
  • Currently I am using pythonw.exe and leaving my script as .py however this doesn't allow for my Error messages to be printed if someone inputs invalid arguments. – Aaron Lind Jul 24 '19 at 11:59

1 Answers1

0

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]
Michael Larsson
  • 187
  • 2
  • 11