Running Pycharm as root from launcher
Asked Answered
L

7

19

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.

Licence answered 10/4, 2016 at 13:0 Comment(1)
I'm not familiar with pycharm but this article may be what you're looking for: esmithy.net/2015/05/05/rundebug-as-root-in-pycharmSyrinx
G
17

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"

Gibson answered 20/6, 2017 at 7:57 Comment(2)
This did not work for me, it corrupted my sudoers file, and that was a whole adventure... Along with PyCharm (and IntelliJ) spitting out invalid interpreter & so I tried it anyway, just doesn't work. -1 from me (this is on ubuntu btw)Delrio
This work for me, in the project folder: sudo chmod -R 777 ./Recollection
M
10

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.
Mini answered 10/4, 2016 at 13:5 Comment(4)
Thanks, but i'd like to avoid going into the terminal window, and run as root directly from the launcher's icon if possible.Licence
yes, it launches the app as my current user, which is not root.Licence
Thanks, this was exactly I was looking for!Licence
How about for a flatpak Pycharm install?Flowerdeluce
G
2

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

Garlicky answered 28/6, 2018 at 7:34 Comment(1)
this means that every python script will have root permissions. While it is a correct answer to the question I wouldn't implement this solution because it is really dangerous.Outland
F
2

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.

Flowerdeluce answered 21/9, 2019 at 8:5 Comment(0)
G
1

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.

Galluses answered 26/1, 2021 at 4:23 Comment(0)
B
0

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
Buerger answered 20/12, 2020 at 13:12 Comment(0)
R
0

Easy solution: In the project folder:

sudo chmod -R 777 ./ 

This just give write permission.

Recollection answered 18/5, 2023 at 9:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.