Now this question might seem like an questions like this one and this one.
However unfortunately the above solutions don't work for me. I need a way to execute a Python 3.4.3 script, leave it running when the terminal closes, and have it not hang in terminal and executed directly.
pi#raspberrypi:/var/www/html/mysite/scripts $ nohup python3 my.script.py &
The above runs the script, but it hangs in terminal, I cannot enter any other commands until I stop it, by pressing CTRL + C.
Is there a way to achieve something like this:
pi#raspberrypi:/var/www/html/mysite/scripts $ nohup python3 my.script.py &
pi#raspberrypi:/var/www/html/mysite/scripts $
(Now I enter more commands, despite the script still running)
I hope I have provided enough information, hopefully you can help me, thanks!
Based on a quick google search, I found
start /b python3 my.script.py
for windows or
python3 my.script.py &
on lunix (bash).
If the ampersand doesn't work, you can use a terminal multiplexer like GNU screen or tmux for your purpose.
Related
Morning guys,
I am a beginner programmer who was using PyCharm and recently moved to VsCode. Some days ago I started running scripts from Bash, but there is something that I do not understand why is not working properly.
Imagine I have a script called script.py and go to bash terminal and code:
$ python script.py
Ok, it runs correctly.
However, imagine that now I modify that script and i again I go to bash and code :
$ python script.py
And the result is that it runs the previous script and not the modified one!
I manually solve this problem by saving the script right clicking below OPEN EDITORS > script.py > Save
Thank you very much in advance.
Have a nice day!
I have tried to save it manually.
I do not understand why is not working properly.
It is working properly, just differently from what you're used to. You have to save the file before you can use it. I'm in the habit of pressing the key combination for Save All before I switch to the terminal window.
You can turn on auto-save by following the instructions here: https://code.visualstudio.com/docs/editor/codebasics#_save-auto-save
I've created an app that runs on cygwin, that will open some new shells and run python script on each of them. The problem started when I wanted to have control on the new shells and kill them at will. after a lot of digging I decided to use the following command:
subprocess.run('mintty.exe -t {} -h always -e {} &'.format(app_name, run_app_cmd), shell = True)
and later when I'll want to kill it use:
subprocess.run('kill -2 {}'.format(apps[app].shell_pid), shell = True).
it worked pretty well until I realized that A-L-O-T of times the new terminal gets stuck and doesn't respond, and I don't like it. I made some more digging and I found that while I thought that the python on the current mintty executes the command and opens the new terminal, what that actually happens is that the windows host opens the new mintty (the PPID of the new terminal is 1), and then probably the signal goes through some windows problems and etc.
the reason that I want each of the scripts in a separate terminal is that each of them have a lot of output, and I want them in different windows.
Now, after all this explanation, is there any way to prevent this? I don't want these stuck to become a part of my life...
Dear fellow developers,
I'm repeatedly using (and developing) a python script for calculations, by executing it through the windows command prompt in each test.
The script has some parsed options.
In order to make each of my calculations easily reproducible, I save the actual command I entered to execute each calculation. For the moment I simply copy by hand the command once I executed it and I put it in a file. But since I have to do it for each calculation, I wonder is there is any python script line that could take my command line input, like:
python script.py --option="foo"
into a file.
The form of the command could be:
%save file=_command_used.txt% python script.py --option=foo
which would create the file and save the actual command "python script.py --option=foo" into it.
Thanks in advance!
Best regards!
I would love to have solutions for both Windows command prompt and Linux shell command prompt.
On Linux there is the script command that will capture all entered commands in a file. Use it like that:
script -a _command_used.txt
python script.py --option=foo
python script.py --option=bar
The -a option stands for append so the _command_used.txt will not be overwritten.
On Windows you can achieve a similar thing using Start-Transcript and Stop-Transcript cmdlet. See this related post.
Since you are using Python, I recommend you investigate the Xonsh shell as one way to solve this. It is cross platform and is scripted with python.
I've programmed a bit in Python for the last few days and everything has worked fine up until yesterday. Whenever I try to run a script now the shell only responds with the message RESTART and a path to where my script is stored. Here's an image:
I don't know how to fix this and can't find any solutions to it. When I'm writing the code directly into the shell everything works fine but this error only appears when I try to run a script from another file.
Thanks in advance
Except for the RESTART line, the behavior you see has nothing to do with IDLE. Run any program (without IDLE) that does not print anything with the '-i' option and you will see the same thing -- a '>>> ' prompt.
C:\Users\Terry>type testprog.py
5+5
C:\Users\Terry>python -i testprog.py
>>> quit()
C:\Users\Terry>
IDLE runs a editor window code the same as if it were run in a console (Command Prompt on Windows) with 'python -i'.
That's because your testprog.py doesn't actually output anything. Try changing it to print(5 + 5) or something similar, currently it only computes 5+5 and then it exits.
I'm wanting to open a terminal from a Python script (not one marked as executable, but actually doing python3 myscript.py to run it), have the terminal run commands, and then keep the terminal open and let the user type commands into it.
EDIT (as suggested): I am primarily needing this for Linux (I'm using Xubuntu, Ubuntu and stuff like that). It would be really nice to know Windows 7/8 and Mac methods, too, since I'd like a cross-platform solution in the long-run. Input for any system would be appreciated, however.
Just so people know some useful stuff pertaining to this, here's some code that may be difficult to come up with without some research. This doesn't allow user-input, but it does keep the window open. The code is specifically for Linux:
import subprocess, shlex;
myFilePathString="/home/asdf asdf/file.py";
params=shlex.split('x-terminal-emulator -e bash -c "python3 \''+myFilePathString+'\'; echo \'(Press any key to exit the terminal emulator.)\'; read -n 1 -s"');
subprocess.call(params);
To open it with the Python interpreter running afterward, which is about as good, if not better than what I'm looking for, try this:
import subprocess, shlex;
myFilePathString="/home/asdf asdf/file.py";
params=shlex.split('x-terminal-emulator -e bash -c "python3 -i \''+myFilePathString+'\'"');
subprocess.call(params);
I say these examples may take some time to come up with because passing parameters to bash, which is being opened within another command can be problematic without taking a few steps. Plus, you need to know to use to quotes in the right places, or else, for example, if there's a space in your file path, then you'll have problems and might not know why.
EDIT: For clarity (and part of the answer), I found out that there's a standard way to do this in Windows:
cmd /K [whatever your commands are]
So, if you don't know what I mean try that and see what happens. Here's the URL where I found the information: http://ss64.com/nt/cmd.html