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.
Related
I have a runGUI.sh script and I want to run a Python script through it. The Python Script is a GUI.py that I want to make it run as an App, by either double clicking the Shell script or the Python Script itself (ideally I would like to have both ways).
I tried everything that others recommend:
I changed permissions to both scripts and also made them executable.
I added the appropriate #!/bin/sh or #!/bin/bash in the Shell Script and #!/usr/bin/env python at the Python Script.
I tried all combinations in the Shell script for the Python executable command, like python GUI.py or ./GUI.py, and even with the full path of the Python Script.
The result is the same. If I double click the GUI.py or the runGUI.sh, and because they are executable, I get the options:
Options when double clicking
Regardless of "Run in Terminal" or "Run", nothing happens.
If I run either scripts through terminal, like ./GUI.py or runGUI.sh, the Python file works fine and the GUI is opening in both cases.
Is there a way to open this GUI by double clicking the Shell or Python scripts?
I am using Ubuntu 16.04, standard Gnome, have installed Zshell if this helps and I built the Python GUI with the Tkinter module.
Create a file name mygui.desktop in ~/.local/share/applications
[Desktop Entry]
Encoding=UTF-8
Version=1.0
Type=Application
Name=My GUI
Icon=
Exec=/path/to/GUI.py
and then mygui will be appearing as an application that you can launch clicking it.
Diego is mostly correct, but they left out one important detail that will cause it to fail. I am assuming you are using a python terminal application. It should look more like this:
[Desktop Entry]
Encoding=UTF-8
Version=1.0
Type=Application
Name=My GUI
Exec=python3 /path/to/GUI.py
Terminal=true
The Terminal=true makes it so it actually runs when you click on it. Also, put your python alias (python3,py,python,etc.) before it. Lastly, if you don't want an icon, just omit it rather than putting Icon=.
I know this is an old question, but I would have forgotten it too if I ran into this issue any less recently.
I've been spending the last week or so creating a simple touch friendly GUI in python3.4 (on the raspberry pi). Now I setup python to run my script on launch, but I ran into the problem, that I couldn't open other programs from within my program (such as the web browser or calculator). However if I use IDLE to execute the script and not the standard python program in the terminal, opening other programs from my scrip works! I already created a .sh file that runs when the Linux Gui starts, which opens up my script in IDLE, however it only opens the file and doesn't execute it.
So now here is my question: Can I create a .sh script, which opens IDLE and runs a python script in the IDLE console (I already tried the exec command when launching idle with no results)
Right now this is my command, which should execute the loaded file, but only loads it for some reaseon:
sudo idle3 -c exec(open('/path/to/my/file.py').read())
Any help is appreciated :)
Use Idle's cli options
You have a few options, of which the best one is to use the -r option. From man idle:
-r file
Run script from file.
This will however only open the interpreter window. Since you also want the editor, this will do pretty much exactly what you describe:
idle3 '/path.to/file.py' & idle3 -r '/path.to/file.py'
The startup command you need is then:
/bin/bash -c "idle3 '/path/to/file.py' & idle3 -r '/path/to/file.py'"
The command you tried will not work, since here, we can read:
Only process 0 may call idle(). Any user process, even a process with superuser permission, will receive EPERM.
Therefore, we depend on cli options of idle, which luckily provide an option :)
Alternatively
Another option would be to open the file with idle3 wait for the window to appear and simulate F5:
/bin/bash -c "idle3 '/path/to/file.py' & sleep 3 && xdotool key F5"
This would need xdotool to be installed on your system.
An advanced version of this wrapper would open the file with idle, subsequently check if the new window exists, is focussed and simulate F5 with xdotool.
These would however be the dirty options, which we luckily don't need :).
I wrote python script that uses subprocess.pOpen() module to run and manipulate with 2 GUI programs: Firefox and VLC player. I am using Ubuntu 14.04 LTS operating system in Desktop mode.
My problem is when I try to run that python script when system starts, script is running but Firefox or VLC don't start.
So far, I tried to make shell script to run my python script and then with crontab with #reboot /home/user/startup.sh to execute my python script. I set all permissions for every script that is using. I gave my user root permisions so everything is OK with that.
I also tried to run my script putting command "sudo python /path/to/my/script.py" in /etc/rc.local file but that also is not helping.
I googled and found out people using .desktop files that they put in ~/.config/autostart/ directory but that also failed. Example of what I wrote:
[Desktop Entry]
Type=Application
Exec="sudo python /home/user/path_to_my_script/my_script.py"
X-GNOME-Autostart-enabled=true
Name=screensplayer
Comment=screensplayer
And I saved this as program.desktop in ~/.config/autostart/ directory but it does not work. I am sure there is a way to fix this but don't know how. Any help will be appreciated!
Found solution to my problem. When you are running commands with pOpen in python like this:
FNULL = open(os.devnull, 'w')
_FIREFOX_START = ["sudo", "firefox", "test.com/index.html"]
subprocess.Popen(self._FIREFOX_START, stdout=self.FNULL, stderr=subprocess.STDOUT)
it won't run apps because of "sudo" word, when I removed it, it worked.
Also run gnome-session-properties in terminal and add new startup application, be aware that you have to execute python script without sudo, like this:
python /home/user/path_to_script/script.py
Also, I granted my user root privileges so kepp that in mind.
I have installed Python and written a program in Notepad++.
Now when I try to type the Python file name in the Run window, all that I see is a black window opening for a second and then closing.
I cant run the file at all, how can run this file?
Also I want to tell that I also tried to be in the same directory as a particular Python file but no success.
I assume you are running the script with command python file_name.py.
You can prevent closing of the cmd by getting a character from user.
use raw_input() function to get a character (which probably could be an enter).
It sounds like you are entering your script name directly into the Windows Run prompt (possibly Windows XP?). This will launch Python in a black command prompt window and run your script. As soon as the script finishes, the command prompt window will automatically close.
You have a number of alternatives:
First manually start a command prompt by just typing cmd in the Run window. From here you can change to the directory you want and run your Python script.
Create a Windows shortcut on the desktop. Right click on the desktop and select New > Shortcut. Here you can enter your script name as python -i script.py, and a name for the shortcut. After finishing, right click on your new shortcut on the desktop and select Properties, you can now specify the folder you want to run the script from. When the script completes, the Python shell will remain open until you exit it.
As you are using Notepad++, you could consider installing the Notepad++ NppExec plugin which would let you run your script inside Notepad++. The output would then be displayed in a console output window inside Notepad++.
As mentioned, you can add something to your script to stop it completing (and automatically closing the window), adding the line raw_input() to the last line in your script will cause the Window to stay open until Enter is pressed.
Try to open in Command Prompt instead of run window. The syntax is:
py filename.py
If it doesn't work, try to reconfigure Python. Did you set environment variables? If not, this could help you
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.