Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running Pycharm as root from launcher

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.

like image 488
Zorgmorduk Avatar asked Apr 10 '16 13:04

Zorgmorduk


People also ask

How do I run PyCharm as root user?

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.

How do I run a python script in PyCharm with sudo privileges?

Now go to PyCharm, go to File > Settings > Project Interpreter. Click on the settings icon and click on the green plus icon to add interpreter. Browse to the directory where you have saved the python-sudo.sh and select it. Save changes and exit.

How do I open PyCharm without terminal?

To run PyCharm, find it in the Windows Start menu or use the desktop shortcut. You can also run the launcher batch script or executable in the installation directory under bin. Run the PyCharm app from the Applications directory, Launchpad, or Spotlight.


1 Answers

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.

like image 146
Guy Avraham Avatar answered Oct 04 '22 21:10

Guy Avraham