My ultimate goal is to run a Python script (the code need to call some subprocess) without terminal.
I tried with python launcher on Mac. The code works correctly when the "Run in a Terminal window" is checked.
(Notice that the shell=False is default here.) However, it won't run the subprocess correctly when I uncheck that option.
I also tried to run in with Mac OS automator (Run Shell Script) — negative too.
Here is a small sample code.
import subprocess
with open("record","w") as f:
subprocess.call(["which","ipython"], stdout = f) # this line fails
subprocess.call(["open","."]) # this line always works
If I run from terminal, it creates a record file, which shows the ipython path. However when I run it from automator or python launcher without terminal window, it creates an empty file.
Related
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 did a application with Python 3 using Tkinter gui, on Raspbian with the RPi. I have three questions:
1) I've turned the python script into executable using:
chmod -x path/myfile.py
I've declared on the first line of script
#!/usr/bin/env python3
When I double click onthe script it pops up a windows asking if I want to execute normally or on the terminal. If i chose Execute, the script runs normally.
Is there any way to execute normally directly, without asking?
2) How do i create a shortcut on my Desktop to execute my python script?
I've created a file on the desktop and I edited the following:
[Desktop Entry]
Type=Application
Encoding=UTF-8
Name=Name of the shortcut
Comment=Comment
Icon=path/pic.gif
Exec=python3 path/myfile.py
Terminal=false
The picture if the shortcut works, but the script it's not launched. It appears the hourglass and nothing happens.
3) How do I make a python script launch when at the startup of Raspbian?
I've already edited the
/etc/xdg/lxsession/LXDE/autostart
and also the
/etc/xdg/lxsession/LXDE-pi/autostart
with:
#python3 path/myfile.py
on the last line of the file. Rebooting the system, nothing happens.
Thank you for your help.
I have extended the file context menu in the Windows registry and added a shortcut to a Python script (which is a Tkinter window). It runs fine, but obviously with the Python console in the background. I tried to change the shell command from python to pythonw (and renaming the file to .pyw) but then Windows always simply opens the "open with..." dialog and not the script. If I copy-paste the shell command and run it from cmd, it works. Running the .pyw from the explorer also works fine, without console.
Why is the behavior different when running it in the shell command?
This is in my .reg setup file under the command key:
#="pythonw \"C:\\test_script.pyw\" \"%1\""
I use the Windows version of Python. I have a Python script using Pyside (nothing complicated, a kind of "hello world").
When I click on my script file or if I launch it from a command line, it executes perfectly and I have a GUI appearing.
However, I would like to avoid having a GUI if the script is launched from a textual terminal (cmd.exe, cygwin, ...). A kind of script which automatically knows if it should have a GUI output or a textual output.
Is there an easy and simple way to do that? I want to be able to do that with the Windows version of Python (not the one coming with Cygwin packages).
An obvious way would be to add a kind of "--no-gui" parameter when I launch the script from a textual terminal, but I wonder if Python (or some Python libraries) already provide tools for that.
Moreover I have an SSH server (Cygwin-base) on this computer, I can execute the script at distance but no GUI appear (of course) and I have no error message. It is a case where it is very interesting to know if the script failed because of the lack of Windows graphical support or if the script should adapt its output for a textual terminal.
I know that you can run file as .py file or as .pyw file. The second option is used for GUI applications and it does not open the console window. To distinguish these to two cases you can check isatty method of sys.stdout.
import sys
if sys.stdout.isatty():
# .py file is running, with console window
pass
else:
# .pyw file is running, no console
pass
EDIT:
I tried to run that with putty+ssh on linux box - it returns True.
I tried to use msys bash shell on windows box - it returns True (.py file)
I tried to use cygwin bash shell with cygwin python - it returns True (.py file)
Unfortunately, I have no possibility to try putty + windows cygwin ssh server.
A Python application (having a permission to be executed by the user) doesn't start with double click on it. If I run it (directly, by ./theapp.pyw) in terminal I get ": No such file or directory" error (while there is no such a string in the application code and the file surely exists (renaming it and using a different name results in the same)).
But If I explicitly open it with Python (by issuing python ./theapp.pyw or right-clicking and choosing to open with Python 2.7) it works just fine.
The first line of the file says
#!/usr/bin/env python
Issuing "/usr/bin/env python" command launches Python 2.7 console.
The OS is XUbuntu Linux 11.10. The only Python installed is Ubuntu's default 2.7.
Any ideas?
pyw files are for windows
http://filext.com/file-extension/PYW
And in windows the shebang line is ignored.
Just work with the .py script.
You can also try creating a bash script that launches your python scripts with the specific flags that you want.