I\'ve tested code that requires root access in pyCharm
by running sudo pycharm.sh
but this is not the way I would recommend of doing so.
I
For what it's worth, I've managed run a python script with sudo priviledges (on Ubuntu 16.04) like this:
In the very first line in the script, define the interpreter like this:
#!/usr/bin/sudo python
Make the script executable:
chmod +x myscript.py
Run the script directly, without specifying the python
interpreter yourself:
./myscript.py
I have encountered the same problem trying to debug Bluetooth related code on a Raspberry Pi. I suppose, since you're doing remote debug on the device, that the device is for development use only. In such a case, in my humble option, you should permit ssh root login, so you can configure PyCharm to use the root user and you don't need to sudo. That's the solution I have chosen.
The following instructions are for a Raspberry Pi, but the procedure is the same for any Linux distribution:
First of all, add your public key to the authorized_keys
:
cat ~/.ssh/id_rsa.pub | ssh pi@raspberrypi "mkdir -p ~/.ssh && cat >>
~/.ssh/authorized_keys"
Then login into the Raspberry Pi:
ssh pi@raspberrypi
Once you have a console copy your key into the root
directory:
sudo mkdir /root/.ssh
sudo cp authorized_keys /root/.ssh/
Finally edit sshd_config
adding PermitRootLogin without-password
:
sudo vim /etc/ssh/sshd_config
Use your preferred editor.
Now you are able to ssh inside the Raspberry Pi as root:
ssh root@raspberrypi
Using root
instead or pi
user, give you the ability to run your code, even remotely, with root privileges, as
required by BlueZ.
For those looking for a cleaner solution and don't mind entering a password each time.
Go to your Run Configuration > Edit Configurations
Under 'Execution', check the Emulate terminal in output console option.
This will allow you to debug a Python script while maintaining your current user and giving elevated sudo privileges to the script when it's needed. It also makes it easier to maintain different virtual environments if you work across multiple projects.
In PyCharm new version, it has a configure to run Python interpreter in root, no need workaround. See picture below. Check to checkbox: Execute code using this interpreter with root privileges via sudo
I have encounter 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 here that this solution "attacks" the problem by running only a certain Python script (within the pycham IDE) in root mode , and not the entire pycharm application.
1) Disable requiring password for running Python:
This will be achived by editing the /etc/sudoers.d/python file. What we need to do is to 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
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 helpfull and let me know if there are any mistakes in this approach.
Cheers,
Guy.
Terminal:
sudo ./Pycharm
this way you can start PyCharm as SuperUser