
What I really needed was something that I could work on in my main dev environment but deploy and execute on the RPi. It also needed to be able to run as root because all RPi GPIO requires root privileges.
Most importantly, PyCharm has a remote debugging feature which coupled with automatic deployment makes everything super easy.
Setting Up Remote Debugging
Below is how I set up my environment. Much of this is found in the PyCharm help documentation. You can find how to set up remote debugging particularly the section on setting up a remote interpreter via SSH.
Automatic Deployment
First we need to setup automatic deployment of our files to the RPi. This part isn’t strictly required but if you don’t do it you’ll have to manage uploading your changes.
- Important: The login you use here is the credentials that the remote process will be run as. You can use the “pi” user, as we have another way of gaining root privileges to access GPIO detailed below.
Run Configuration
Still with me? Now that we’ve got deployment set up and an interpreter that will use our remote virtual environment, the final step is to create a run configuration to actually run a script.
- Create a simple python script and call it
hello_world.py. Give it the following contents.
- import RPi.GPIO
- print "hello world!"
- Click in the upper right of the main window and choose “Edit Configurations…”.

- Click the plus button and choose Python.

- Give the new configuration the name “hello_world (remote)”. For the script, choose the script we just created. For Python interpreter, choose the remote interpreter we created in the last section.

- Now add a path mapping to map from your local project path to the remote path. This lets the interpreter find the source file for what’s executing remotely.


- Save the new configuration and click the run button. You should see PyCharm connect and the hello world print on the debug console.

Running as Root
Rather than enabling logging in as root over SSH, there’s another approach that will work without opening that security hole. Using permissions, we can cause our python interpreter to simply run as root.
- Reconnect using PuTTY and navigate to the project root folder.
cd /home/pi/remote_debug/remote_debug_ex
- Change ownership of the python interpreter to root and cause it to be executed as its owner whenever it’s run.
- sudo chown -v root:root venv/bin/python
- sudo chmod -v u+s venv/bin/python
This will cause pip to act a bit funny when you want to install anything later on so just reverse the above changes from step 2 as needed. Just use the same commands but with pi instead of root and with
u-s.
I have a couple of scripts that I keep in the project root for just this purpose. You can use below:
python_as_pi.sh
[code language="bash"]
#!/usr/bin/env bash
DIR=$(dirname $(readlink -f "${BASH_SOURCE}"))
if [ ! -f $DIR/venv/bin/python ]; then
echo "This script should be located in the project root directory"
echo "and the virtual environment should be created."
else
# Change python back to run as pi
sudo chown -v pi:pi "$DIR/venv/bin/python"
sudo chmod -v u-s "$DIR/venv/bin/python"
fi
[/code]
python_as_root.sh
[code language="bash"]
#!/usr/bin/env bash
DIR=$(dirname $(readlink -f "${BASH_SOURCE}"))
if [ ! -f $DIR/venv/bin/python ]; then
echo "This script should be located in the project root directory"
echo "and the virtual environment should be created."
else
# Change python to run as root
sudo chown -v root:root "$DIR/venv/bin/python"
sudo chmod -v u+s "$DIR/venv/bin/python"
fi
[/code]
Conclusion
Now you have a way to remote debug your RPi with the interpreter running as root, allowing access to the Pi’s GPIO. You can do this with multiple projects and even have multiple projects or instances of projects open and debugging remotely.
PyCharm is a great IDE and I encourage you to look into using it to improve your development environment. For example, check out Zeal and the Dash plugin that will cause PyCharm to perform a documentation lookup when you press
Ctrl+
Shift+
D. There are also plugins to provide support for Markdown and bash scripts.