Running Pycharm as root from launcher
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.
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:
This will be achieved by editing the /etc/sudoers.d/python
file. 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"
Hope it was helpful and let me know if there are any mistakes in this approach.
Guy.
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.