Currently I've been creating just a small program. And one of the options is to start a counter.
Code for counter
from time import sleep
number = 0
while 1 == 1:
sleep(1)
print number
number += 1
I want to be able to open this program in a new terminal window (Ubuntu) so I can keep the existing terminal open. Anyway to do this in python?
Thanks
Paul
EDIT
I think I figured out one way to run it in the same terminal so it works!
os.system('python counter.py')
I simply used that, I thought if I were to do that before, when I used CTRL + C it would close down both programs but this seems to of worked. Thanks for the answers!
You could create a wrapper that launches your terminal and tells it to run your script. If you know which terminal you're using, this isn't too hard. For example, if you use multi-gnome-terminal:
#!/bin/sh
multi-gnome-terminal --use-factory --command /usr/bin/python /path/to/my/script.py
Now, every time you run that wrapper (with sh ./wrapper, or /usr/local/bin/wrapper if you install -m755 it, or by double-clicking it, or whatever), it will open a new window (launching a new terminal process only if necessary) and run your script there.
Related
Consider the following scenario: I have two Python scripts, the first a long-running process and the second a short-running process. I heavily use the shift + enter shortcut to run code directly from my .py files.
Currently in VS Code, I can only manage to have the interactive environment with one script. That is, if I run the first, long-running script and then open a new Python terminal window, running a line such as print('hello world') will run in the first terminal. I want to learn how to change the shift + enter so that it executes in the second, newly-opened terminal window.
I've looked at a few SO questions, namely this one, but the solutions either don't work or are not applicable to my use case.
I managed to execute in the newly-opened terminal window typing a command there. Not as convenient as shift + enter but does the job.
I used to run Python scripts from my Windows command line, and all the prints were printed in the same console. Now something happened on my machine (Windows 10), and when I launch a Python script from the command line (i.e. open a Command Prompt and run python <my_script.py>), Windows opens a new window (titled with the absolute path of python.exe). This windows closes automatically at the end of the execution, so that I can't see the output.
How do I go back to printing output in the same command prompt window from which I run the script?
Not sure how useful this will be but I had this same problem, found this thread, and realized that the new console window was opening up when I omitted 'python' from the command.
>python myscript.py
shows the output right in the terminal where I typed the command, but
>myscript.py
opens the new console window and closes it immediately after the script runs.
It's odd but it very likely a windows setup issue as python is an exe. If memory serves windows will spawn on a > run command so checking the way python is booting will help.
Unfortunately it could be a range of issues, so some steps towards victory:
What happen when you just type python into the cmd? If it simply starts the input >>> - it means your python setup is fine. If a cmd window spawns and disappears it may be a windows permissions issue.
Try running your script with -i flag: python -i script.py. This drops you into the repl when the app completes - displaying your output.
Ensure you're using the native flavour of the cmd to test. Ensuring any command app or IDE isn't injecting a start command or weird /K (spawn new window) flag.
Hope it helps.
In my computer this was caused by Windows not knowing what program a .py file was associated with. I solved this by going to:
Control Panel -> Programs -> Default Programs -> Associate a file type or protocol with a program (Scroll down) and choose "Choose default apps by file type" Scroll down until you see ".py" and choose the correct
Python interpreter.
Simply: last row on the end of your program maybe this:
input("\nIf you whish end the program press any key ...")
...and your program wait for the key and you see your outcome
I use a RaspberryPi 3 with UbuntuMate 16.04. On it, I want to start a little Python (3.5) program every midnight. For that I call a little shell script, so that I can change into the wanted directory comfortably.
crontab:
5 0 * * * /path/to/script/start.sh
start.sh (yes, it's executable):
#!/bin/bash
cd /path/to/wanted/workingDir/
python3.5 ControllerQueue.py
#also tried: python3.5 ControllerQueue.py &
Now if I execute the programm or script from the terminal, everything runs fine. But if I use the crontab it starts the script and stops right after. I also tried running the programm directly but get the same outcome. The paths are correct as I copied the workingDir path from the crontab-file and started it via the terminal.
Is there something I overlook?
As stofvl suggested, I saved the error output of my shell script. It turns out that I needed to add a display. My programm is divided into two scripts. One which provides a GUI and the other main application. The script only starts the main application, without the GUI, but it seems that this didn't matter.
This discussion helped me solve the problem.
I am trying to run a python script via cmd prompt on my work PC (Windows 7, Python 2.7). The script requires filepaths from different drives on my PC. I am correctly pulling all necessary filepaths and I press Enter to run the script but the script just hangs. The only thing that shows is a blinking underscore. I try to click the X to close the prompt but nothing happens.
I am not able to Ctrl+C out of the program either. I open up Task Manager and I am not able to End Task (nothing happens) or End Process (cmd.exe doesn't even show up in this tab). I also tried Start-->Run-->taskkill /im cmd.exe but nothing happens. The rest of my team has no problem with Python 2.7. The only way to get out of the frozen cmd is to hold down the power button. I do not want to have to keep going through this process especially since this is during work. I'm hoping someone will be able to help me out:
Any idea what's wrong with the version of Python I am using?
How am I able to kill cmd.exe so that I can continue normal work functions without having to hold down the power button and waiting 5-10 minutes to reboot my PC?
The filepaths I was pulling files from were through ClearCase directories. It turned out that I simply had to reinstall ClearCase. There must have been some configuration issue that was causing the cmd to hang and thus forcing a hard reboot. It's good to point out that I am no longer experiencing this problem and that nothing was wrong with the python scripts.
Im using a shortcut in Notepad++ to Run my Python-files directly in Python IDLE.
From the shorcut.xml, you can see how I'm using it:
<Command name="runinpython" Ctrl="no" Alt="yes" Shift="yes" Key="81">"C:\Python27\Lib\idlelib\idle.pyw" -r "$(FULL_CURRENT_PATH)"</Command>
The problem is: Every time I press Shift+Alt+Q, it opens another IDLE-Window and I'd like to only open it once and then run my .py-files in it, otherwise I have to close the IDLE windows each time after every execution.
Do you know a way to do this?
Thanks
http://mpcabd.xyz/notepad-plugin-to-run-python-scripts/ supports it
-> when running py-scripts from within Notepad++, there won't be new console-windows opened all the time, but instead the script always runs in the open cmd-window :-)