I came across another way to solve this problem, so I decided to share it (this answer is more like an alternative to other answers).
It is worth mentioning here that this solution “attacks” the problem by executing only a specific Python script (in the pycham IDE) in root mode, and not the entire pycharm application.
1) Disable the password to start Python:
This will be achieved by editing the /etc/sudoers.d/python file. What we need to do is add an entry to this 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: hostname
2) Create a "sudo script": the purpose of this script is to give the python user privilege to run as root.
Create a script called python-sudo.sh and add the following to it:
!#/bin/bash sudo /usr/bin/python " $@ "
Note, again, that the path is the path to your Python, as in the previous step.
Remember to give permission to execute this script with the command: chmod , i.e.
chmod +x python-sudo.sh
3) Use the python-sudo.sh script as the Pycharm interpreter:
Within Pycharm, go to: File --> Settings --> Project interpreter
At the top right, click the Settings icon and click Add Local.
In the browser option, select the python-sudo.sh script that we created earlier. This will give Pycharm the privilege of running the Python script as root.
4) Debugging the test: all that remains to be done is debugging a specific Python script in the pycharm IDE. This can be easily done by right-clicking the script for debugging → by clicking "Debug sample_script_to_debug.py"
Hope this was helpful and let me know if there are any errors in this approach.
Cheers
Guy.