A proof-of-concept for PyCharm projects that must run with elevated permissions on Linux.
python-sudo.sh
sources the virtual environment and then executes the project with elevated permissions.
setup.sh
places the appropriate entry in /etc/sudoers.d/python
.
The user must enter their sudo
credentials during setup.sh
.
-
Ensure
setup.sh
is executable (ls -l setup.sh
).-rwxr--r-- 1 user user 535 Mmm DD HH:MM python-sudo.sh
-
Run
./setup.sh
from project root. Example output:[user@host sudome]$ /bin/bash /home/user/PycharmProjects/sudome/setup.sh Configuration Line: [sudo] password for user: user host = (root) NOPASSWD: /usr/bin/python File contents: /etc/sudoers.d/python user host = (root) NOPASSWD: /usr/bin/python
-
Add
python
as a venv project interpreter in PyCharm. -
Add
python-sudo.sh
as a project interpreter in PyCharm. This will time out or throw an error, but it will be okay. -
Observe the available run configs from
.idea/runConfigurations/
. -
Choose any config through PyCharm and run it (
Shift + F10
).
ProjectRoot/
|-.idea/
| \- runConfigurations/
| \- [XML files]
|- src/
|- test/
|- python-sudo.sh
|- requirements.txt
\- setup.sh
Inspired by Eric Smith.