I'm fairly new to Python and I have a python script that I would like to ultimately convert to a Windows executable (which I already know how to do). Is there a way I can write something in the script that would make it run as a background process in Windows instead of being visible in the foreground?
You can always run a Windows program in the background using
START /B program
See this post for more information.
You can run the file using pythonw instead of python means run the command pythonw myscript.py instead of python myscript.py
Related
I'm writing some python to render stuff that I tweak and run a lot, and that runs inside a virtual env. I would like a keyboard command to run a bash script (that launches python) inside the known terminal and virtual env.
I played a bit with setting up a shell script and a custom task, but entering the virtual env is always a bit tricky.
I don't need a debugger or anything complicated, just a way to run the python code and attach a keystroke to it.
https://code.visualstudio.com/docs/editor/debugging#_launch-configurations
https://code.visualstudio.com/docs/python/jupyter-support-py
You can just run the python.exe/python binary in the virtualenv folder as an executable.
So to run a specific python file with a virtual environment:
For Windows it would be .\path_to_virtualenv\Scripts\python.exe yourfile.py, and for Unix system it would be ./path_to_virtualenv/bin/python yourfile.py
And instead of running a python file, you can probably pipe the input command into the python executable in the path above if you want to run a specific python command.
So something like COMMAND | ./path_to_virtualenv/bin/python yourfile.py
Could you please explain the difference between the way you want and the following operation:
Use "Ctrl+Shift+P" and type "Python: Select Interpreter" (the same as click the interpreter in the lower right corner which is on the right side of python).
Use "Run Python".
I think there is no difference because before your using a keyboard command, you still need to choose the interpreter.
By the way, use jupyter notebook or interactive window will be a good chioce as well.
I am using python.exe.
I tried:
C:/myfile.py
python C:/myfile.py
python "C:/myfile.py"
It always says "invalid syntax". The code is this one:
https://github.com/paulnasca/paulstretch_python/blob/master/paulstretch_stereo.py#L150
So not sure if the file has bugs or I am doing something wrong.
Your screenshot shows that you are already in the Python interpreter. Trying to run python again will result in an error. Exit the interpreter by hitting CtrlD. Make sure you have downloaded the complete paulstretch_stereo.py file. Put the file in the same directory as the files you want to process. Then, from the Windows command line, run python paulstretch_stereo.py --help and the program's options should print out.
By the way, make sure you have NumPy and SciPy installed, otherwise the program won't run.
What you get when you run python.exe directly is called the interactive interpreter.
The usual way to run a python module is simply providing it as a command-line option to the python process:
python C:/myfile.py
This command is provided from your command-line, not from the interactive interpreter.
I want to, from a python script, open a new terminal window.
Then, on that new window, run another python script, located on the same directory.
I needs to be on another window, because both scripts have while True loops, and I need them to run simultaneously.
How would I do that?
Thanks everyone!
You can just spawn an xterm with the python code used:
xterm -e "python /path/to/your/file.py"
This will close when the process ends.
If you need to, you can do this from inside a python script using the subprocess module (https://docs.python.org/2/library/subprocess.html).
I'd like to call a separate non-child python program from a python script and have it run externally in a new shell instance. The original python script doesn't need to be aware of the instance it launches, it shouldn't block when the launched process is running and shouldn't care if it dies. This is what I have tried which returns no error but seems to do nothing...
import subprocess
python_path = '/usr/bin/python'
args = [python_path, '&']
p = subprocess.Popen(args, shell=True)
What should I be doing differently
EDIT
The reason for doing this is I have an application with a built in version of python, I have written some python tools that should be run separately alongside this application but there is no assurance that the user will have python installed on their system outside the application with the builtin version I'm using. Because of this I can get the python binary path from the built in version programatically and I'd like to launch an external version of the built in python. This eliminates the need for the user to install python themselves. So in essence I need a simple way to call an external python script using my current running version of python programatically.
I don't need to catch any output into the original program, in fact once launched I'd like it to have nothing to do with the original program
EDIT 2
So it seems that my original question was very unclear so here are more details, I think I was trying to over simplify the question:
I'm running OSX but the code should also work on windows machines.
The main application that has a built in version of CPython is a compiled c++ application that ships with a python framework that it uses at runtime. You can launch the embedded version of this version of python by doing this in a Terminal window on OSX
/my_main_app/Contents/Frameworks/Python.framework/Versions/2.7/bin/python
From my main application I'd like to be able to run a command in the version of python embedded in the main app that launches an external copy of a python script using the above python version just like I would if I did the following command in a Terminal window. The new launched orphan process should have its own Terminal window so the user can interact with it.
/my_main_app/Contents/Frameworks/Python.framework/Versions/2.7/bin/python my_python_script
I would like the child python instance not to block the main application and I'd like it to have its own terminal window so the user can interact with it. The main application doesn't need to be aware of the child once its launched in any way. The only reason I would do this is to automate launching an external application using a Terminal for the user
If you're trying to launch a new terminal window to run a new Python in (which isn't what your question asks for, but from a comment it sounds like it's what you actually want):
You can't. At least not in a general-purpose, cross-platform way.
Python is just a command-line program that runs with whatever stdin/stdout/stderr it's given. If those happen to be from a terminal, then it's running in a terminal. It doesn't know anything about the terminal beyond that.
If you need to do this for some specific platform and some specific terminal program—e.g., Terminal.app on OS X, iTerm on OS X, the "DOS prompt" on Windows, gnome-terminal on any X11 system, etc.—that's generally doable, but the way to do it is by launching or scripting the terminal program and telling it to open a new window and run Python in that window. And, needless to say, they all have completely different ways of doing that.
And even then, it's not going to be possible in all cases. For example, if you ssh in to a remote machine and run Python on that machine, there is no way it can reach back to your machine and open a new terminal window.
On most platforms that have multiple possible terminals, you can write some heuristic code that figures out which terminal you're currently running under by just walking os.getppid() until you find something that looks like a terminal you know how to deal with (and if you get to init/launchd/etc. without finding one, then you weren't running in a terminal).
The problem is that you're running Python with the argument &. Python has no idea what to do with that. It's like typing this at the shell:
/usr/bin/python '&'
In fact, if you pay attention, you're almost certainly getting something like this through your stderr:
python: can't open file '&': [Errno 2] No such file or directory
… which is exactly what you'd get from doing the equivalent at the shell.
What you presumably wanted was the equivalent of this shell command:
/usr/bin/python &
But the & there isn't an argument at all, it's part of sh syntax. The subprocess module doesn't know anything about sh syntax, and you're telling it not to use a shell, so there's nobody to interpret that &.
You could tell subprocess to use a shell, so it can do this for you:
cmdline = '{} &'.format(python_path)
p = subprocess.Popen(cmdline, shell=True)
But really, there's no good reason to. Just opening a subprocess and not calling communicate or wait on it already effectively "puts it in the background", just like & does on the shell. So:
args = [python_path]
p = subprocess.Popen(args)
This will start a new Python interpreter that sits there running in the background, trying to use the same stdin/stdout/stderr as your parent. I'm not sure why you want that, but it's the same thing that using & in the shell would have done.
Actually I think there might be a solution to your problem, I found a useful solution at another question here.
This way subprocess.popen starts a new python shell instance and runs the second script from there. It worked perfectly for me on Windows 10.
You can try using screen command
with this command a new shell instance created and the current instance runs in the background.
# screen; python script1.py
After running above command, a new shell prompt will be seen where we can run another script and script1.py will be running in the background.
Hope it helps.
I have a python script which I can run from pythonwin on which I give the arguments.
Is it possible to automate this so that when I just click on the *.py file, I don't see the script and it asks for the path in a dos window?
You're running on Windows, so you need an association between .py files and some binary to run them. Have a look at this post.
When you run "assoc .py", do you get Python.File? When you run "ftype Python.File", what do you get? If "ftype Python.File" points at some python.exe, your python script should run without any prompting.
Rename it to *.pyw to hide the console on execution in Windows.
You can also wrap it in a batch file, containing:
c:\path to python.exe c:\path to file.py
You can then also easily set an icon, run in window/run hidden etc on the batch file.
how does your script ask for or get its parameters? If it expects them from the call to the script (i.e. in sys.argv) and Pythonwin just notices that and prompts you for them (I think Pyscripter does something similar) you can either run it from a CMD window (commandline) where you can give the arguments as in
python myscript.py argument-1 argument-2
or modify your script to ask for the arguments itself instead (using a gui like Tkinter if you don't want to run from commandline).