I’m trying to run a Python script via a desktop icon/shortcut/launcher on a Raspberry Pi using Raspbian and LXDE. I have to use a desktop launcher since the script has to be started via a tiny touchscreen only accepting left-clicks and without a keyboard.
I think that I have a permission issue since the script fails at a point where it should execute some system commands which require root.
The script works if I do the following:
Open a terminal
Enter sudo python3 program.py
I tried to replicate this behavior with a .desktop file using the following config:
[Desktop Entry]
Encoding=UTF-8
Version=1.0
Type=Application
Exec=sudo python3 program.py
Terminal=true
Icon=path/to/icon.png
Name=Program Launcher
I suppose I missed something obvious, but since I don’t work with Linux usually I’m a bit lost here.
Edit: The problem was not a missing permission but an incorrectly assumed working directory and is now solved. Sorry for my confusion.
In detail: during development the script was started from its own folder while the desktop launcher used /home/pi as working directory. In general that's not a problem, but in the script a hardcoded path was used and a required file not found when using the launcher. The real problem was sloppy swallowed and the status code of a permission issue returned. So it was simply a bad code issue.
As docs for desktop entries says:
The Exec key must contain a command line. A command line consists of
an executable program optionally followed by one or more arguments.
The executable program can either be specified with its full path or
with the name of the executable only. If no full path is provided the
executable is looked up in the $PATH environment variable used by the
desktop environment. The name or path of the executable program may
not contain the equal sign ("="). Arguments are separated by a space.
It leads to conclusion, that maybe python3 is not within $PATH used by the desktop environment. Try to check full path of your python3 and sudo with:
whereis python3
whereis sudo
on my Archlinux it gives me /usr/bin/python3 and /usr/bin/sudo. Try to modify your Dekstop entry to something like:
[Desktop Entry]
Encoding=UTF-8
Version=1.0
Type=Application
Exec=/usr/bin/sudo /usr/bin/python3 program.py
Terminal=true
Icon=path/to/icon.png
Name=Program Launcher
Let me know if this helps.
Related
I hope that I'm tagging/asking on the correct page. I'm Using Linux Mint 6.0, but it could be OS independent.
So the used command for installing Firefox was
nix-env -iA nixpkgs.firefox-esr
When I type which firefox, I get:
/home/foo/.nix-profile/bin/firefox
So Linux Mint comes with Chrome preinstalled, which has a launcher, e.g. also in the start menu. How do I get that for firefox as well? I didn't find a tool to create such a launcher in Mint and I actually think, that nix should do that for me.
EDIT: I also found this page which seemed helpful and advertised e.g. the KDE Kickoff, but I wasn't able to get that one to run.
I can only speak for Ubuntu launchers, but other distros will have launcher files that will have a similar setup
TLDR, add ~/.nix-profile/share to XDG_DATA_DIRS env variable on login. Add the following to ~/.profile after nix loading commands
export XDG_DATA_DIRS=$HOME/.nix-profile/share:$XDG_DATA_DIRS
Explanation:
Installed packages via nix will have an immutable path in nix/store. ~/.nix-profile/bin/firefox is the derivation your current nix environment is linked to (if you update the firefox package, it'll point to the new one)
This means you can create a launcher file for that executable. Lets see if the firefox-esr derivation comes with a desktop launcher or not:
$ nix-build '<nixpkgs>' -A firefox-esr
This will build the package and give you a derivation path. For my current channel it is /nix/store/3iipcmiykgr4p34fg3rkicdz1bw584gm-firefox-102.2.0esr
If I check inside it, there is a .desktop file which defines Ubuntu launchers:
$ ls /nix/store/3iipcmiykgr4p34fg3rkicdz1bw584gm-firefox-102.2.0esr/share/applications
firefox.desktop
These files will also be available under ~/.nix-profile/share/applications so you can simply add that to XDG_DATA_DIRS env variable before boot
If an application did not have one, you can manually make one and add it under ~/.local/share/applications, then set the executable path to the nix one
So SuperSandro2000 explained in the comments, that firefox from nix ships with a .desktop file already. This can be easily added to the start menu and lies in
/nix/store/...-firefox-XXX.X/share/applications/firefox.desktop
If there is no such file included, the most direct way could be (imho) to just create a simple bash script:
#!/bin/bash
./home/foo/.nix-profile/bin/firefox & #Run Firefox
echo Firefox was started with PID $!
In order to make it runnable, enter chmod +x your_skript_name.sh. Afterwards, ./firefox 2> /dev/null & can be used instead to run it silently in the background.
You can also consider the developer/command line options for firefox (Archive) or this blog article here.
Maybe /usr/bin/menulibre is also the right application, it allows you to create .desktop files. This app can also be found by right-clicking on the start "menu".
I'm trying to get python to run my scripts via CMD line.
note: idr if the book said where (a specific place to store my files for) python to access them but scanning back over the beginning I didn't find any relation to it.
According to this book https://automatetheboringstuff.com/2e/appendixb/ I am supposed to be able to type python (script.py) in command line just like this and it should run the script:
Here's the error I am getting upon execution, compared to the example from the book below it to show that this is supposed to work.
CMD LINE OUTPUT :
Microsoft Windows [Version 10.0.18362.1016]
(c) 2019 Microsoft Corporation. All rights reserved.
C:\Users\Armagon>python conway.py
python: can't open file 'conway.py': [Errno 2] No such file or directory
C:\Users\Armagon>
As you can see I get a python Error and researching this has given me nothing I found useful.
Here is the exact sample quoted from the book:
Microsoft Windows [Version 10.0.17134.648]
(c) 2018 Microsoft Corporation. All rights reserved.
C:\Users\Al>python hello.py
Hello, world!
C:\Users\Al>
First off, doing some research I have found a way to make it work, by cd(change directory) to the direct path of the folder containing the scripts C:\Users\Armagon\Desktop\mystuff. This helps to verify that the script I am trying to run is in fact located there and I've spelled it correctly.
But, according to everything I've followed up to this point I shouldn't have to do it this way.
The scripts are on my desktop all in the same folder called mystuff.
Following this link as well as a video Add a directory to Python sys.path so that it's included each time I use Python
I added the mystuff folder to PYTHONPATH in environment variables exactly as suggested. Here is a partial output of the sys.path (ran from IDLE) that shows mystuff filepath is added, so I'm pretty sure I did that part right.
['', 'C:\\Program Files\\Python38\\Lib\\idlelib', 'C:\\Users\\Armagon\\Desktop\\mystuff']
Maybe I've confused what is supposed to be happening here
I am operating under the assumption (based off what I've learned so far) that when CMD executes the line: python (script).py
It's supposed to run python.
Which in turn python is supposed to find my scripts on desktop (by the PYTHONPATH environment variable I created) and execute the script typed in CMD.
I've tried a lot of things in the process of getting just this far. I'd really appreciate if someone could point out what it is I'm overlooking to run python scripts from my desktop just like the book. I'm not very skilled at present and will gladly add any information as needed if I have excluded pertinent info I apologize in advance.
You should be able to do this as long as the script is in the module search path, for example, the PYTHONPATH environment variable. You should then be able to run the script like this:
python -m script
Well, the cd way is the way to go to run a python file via the command prompt. There is no other way I can think of.
Follow these steps or this link -> https://www.wikihow.com/Use-Windows-Command-Prompt-to-Run-a-Python-File to do it:
1)Open command prompt. type cmd or command prompt in search bar or run and press enter key.
2)In command prompt, given that the folder you are trying to access is in desktop, type-> cd desktop\myStuff.. Press enter.
3)Just write- python filename.py and enter.
4)It should run automatically.
How is it possible to run Pycharm from the launcher with root privileges?
I can do that from the terminal window, with sudo ./pycharm.sh, but I'd like to do the same directly from the launcher.
I have encountered another way to solve this issue so I thought to share it (this answer is more like an alternative for the other answers).
It is worth to mention that this solution "attacks" the problem by running only a certain Python script (within the PyCharm IDE) in root mode , and not the entire PyCharm application.
1) Disable requiring password for running Python:
Recommend using the visudo program to edit the file to ensure syntax is correct. Otherwise you may be locked out of using sudo.
visudo /etc/sudoers.d/python
What we need to do is add an entry in that file as follows:
user host = (root) NOPASSWD: full_path_to_python , for example:
guya ubuntu = (root) NOPASSWD: /usr/bin/python
NOTES:
user can be detected by the command: whoami
host can be detected by the command: hostname
2) Create a "sudo script": The purpose of this script is to give python privilege to run as root user.
Create a script called python-sudo.sh , and add the following into it:
#!/bin/bash
sudo /usr/bin/python "$#"
Note, again, that the path is the path to your Python as the previous phase.
Don't forget to give execution permissions to this script using the command: chmod, i.e.-
chmod +x python-sudo.sh
3) Use the python-sudo.sh script as your PyCharm interpreter:
Within PyCharm go to: File --> Settings --> Project interpreter
At the right top hand side click the "setting" icon, and click "Add local".
In the browser option choose the python-sudo.sh script we have created previously. This will give PyCharm the privilege to run a python script as root.
4) Debug the test: All there is left to do is actually debug the specific Python script in the PyCharm IDE. This can be done easily via Right-click on the script to debug --> hit "Debug sample_script_to_debug.py"
Try: gksudo ./path/to/pycharm/executable
More about gksudo
If you're on ubuntu and don't have gksudo install it using:
apt-get install gksu
Here is an example launcher configuration (under: ~/.local/share/applications/jetbrains-pycharm-ce.desktop):
[Desktop Entry]
Version=1.0
Type=Application
Name=PyCharm Community Edition
Icon=/home/YOUR_USER/pycharm/bin/pycharm.png
Exec=gksudo -k -u root "/home/YOUR_USER/pycharm/bin/pycharm.sh" %f
Comment=Develop with pleasure!
Categories=Development;IDE;
Terminal=false
StartupWMClass=jetbrains-pycharm-ce
ce indicates community edition, yours may differ.
As of this post (June 28 2018) I am running Pycharm-2018.1.4 on Ubuntu 18.04 Bionic Beaver. The solution that worked for me was to simply edit the sudoers.d file and add the following to the last line:
user host = (root) NOPASSWD: full_path_to_python
for example:
guya surface-pro = (root) NOPASSWD /usr/bin/python3.6
I had the need to run a script from PyCharm as root like the OP, but the accepted answer didn't work for me because 1.) I had installed PyCharm via flatpak and 2.) The gksu command isn't available on newer versions of Ubuntu and Mint.
I couldn't find a way to make things work consistently with the flatpak install so I uninstalled the PyCharm flatpak and then reinstalled PyCharm the "normal" way. The accepted answer relies on the gksu command which is not available on my OS (Mint 19.2). Thankfully pkexec is a suitable alternative and was already available on my system. I then updated my launcher file (~/.local/share/applications/PyCharm.desktop) as follows. The important bit is the Exec line:
[Desktop Entry]
Name=PyCharm
Exec=pkexec env DISPLAY=$DISPLAY XAUTHORITY=$XAUTHORITY /opt/pycharm-community-2019.2.2/bin/pycharm.sh
Comment=PyCharm
Terminal=false
Icon=/opt/pycharm-community-2019.2.2/bin/pycharm.png
Type=Application
The pkexec command will cause a popup to appear prompting you for your password each time you launch PyCharm via the .desktop file.
Here's a sample git repo that is a proof-of-concept for how to do this. Should be as simple as following the instructions in the README.md. My team needed to do this for one of our products and we used that as the workflow.
Hope this helps! https://github.com/samayer12/sudome
This answer builds upon the description here that was first pointed out by Ed Jones.
I have Pycharm-community 2020.
I edited the
/var/lib/snapd/desktop/applications/pycharm-community_pycharm-community.desktop
file with nano
just modified the Desktop Entry as follow:
[Desktop Entry]
X-SnapInstanceName=pycharm-community
Version=1.0
Type=Application
Name=root PyCharm Community Edition
Icon=/snap/pycharm-community/222/bin/pycharm.png
Exec=sudo env BAMF_DESKTOP_FILE_HINT=/var/lib/snapd/desktop/applications/pycharm-community_pycharm-community.desktop /snap/bin/pycharm-community %f
Comment=root Python IDE for Professional Developers
Categories=Development;IDE;
Terminal=false
StartupWMClass=jetbrains-pycharm-ce
StartupNotify=true
that is: added the sudo command in front of the EXEC item, put "root" in front of couple of fields (see above) and saved with a different name:
/var/lib/snapd/desktop/applications/root_pycharm-community_pycharm-community.desktop
now I have two icons in the launcher, one that is the standard and the other one with "root" as leading word in the description and runs as root.
Note:
the original desktop launcher (which is still there):
[Desktop Entry]
X-SnapInstanceName=pycharm-community
Version=1.0
Type=Application
Name=PyCharm Community Edition
Icon=/snap/pycharm-community/222/bin/pycharm.png
Exec=env BAMF_DESKTOP_FILE_HINT=/var/lib/snapd/desktop/applications/pycharm-community_pycharm-community.desktop /snap/bin/pycharm-community %f
Comment=Python IDE for Professional Developers
Categories=Development;IDE;
Terminal=false
StartupWMClass=jetbrains-pycharm-ce
StartupNotify=true
I have RedHat linux. Intention is to start the java based GUI app after linux system finishes boot up. I have edited /etc/rc.local and kept
/usr/java/jre1.7.0_60/bin/java -jar /home/rfgwtest/Desktop/CEM/CEM_v2.0_Beta08/CEM_v2.0_Beta08/PME_CEM.jar &
at the end of the file. I am able to run this file for testing by executing the file on prompt. no errors of path or so.
what is missing as part of this? i have found this tip from the RedHat website
https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/5/html/Installation_Guide/s1-boot-init-shutdown-run-boot.html
is there a common way to do this across the different kind of distribution ?
Maybe, creating a desktop entry will do it.
To do that,
$ cd
$ nano .config/autostart/javaApp.desktop
now, write this inside it,
[Desktop Entry]
Type=Application
Exec=usr/java/jre1.7.0_60/bin/java -jar /home/rfgwtest/Desktop/CEM/CEM_v2.0_Beta08/CEM_v2.0_Beta08/PME_CEM.jar
Hidden=false
NoDisplay=false
Name[en_IN]=StartJAR
Name=StartJAR
Comment[en_IN]=Jar comment
Comment=Jar comment
save the file and then, restart or re-login to the system.
I'm trying to run jedit from the terminal on fedora. its installed to the path /usr/local/share....
How do I get it to run "jedit" from the terminal rather than switching to the directory and typing java -jar jedit.jar to execute the program.
I created a sh file in that same direectory with "java -jar jedit.jar" and added that directory to ~/.bashrc. I'm aware that I need to type "sh file.sh" to run that file but I know there is a gap in my knowledge somewhere.
i want to use jedit as a commit editor for git and be able to change core.editor='jedit -w' in git config
someone please help
I assume you used the Java installer?
If this is the case, the installer should also have placed a jedit launch script in /usr/local/bin which should be in your PATH by default, so calling jedit from a terminal should instantly work after installation.
If it does not work, then please check whether the file jedit is available in /usr/local/bin and whether /usr/local/bin is in your PATH environment variable.