Recently I’ve been getting into embedded Linux, particularly the Raspberry Pi and have consequently been learning Python. I really don’t like programming directly on these small devices since the environment is typically spare and slow.
At first I used Cyberduck to open and update files along with a text editor. That became exhausting particularly as my project began to grow and span multiple directories. It was also a lot of Alt+Tabbing back and forth between Cyberduck, the text editor, and PuTTY.
What I really needed was something that I could work on in my main dev environment but deploy and execute on the RPi. This is particularly important to me since my RPi is way out in the garage. It also needed to be able to run as root because all RPi GPIO requires root privileges.
I decided on PyCharm, since I have a JetBrains Toolbox subscription and I’m familiar with CLion which uses the same base IDE framework. 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.
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.
- Create a new project called RemoteDebugEx.
- Click on File -> Settings. Then go to Editor -> Code Style and change line endings to “Unix and OS X (\n)”. You want this since your target environment is Linux and different line endings may cause issues.
- Click on Tools -> Deployment -> Configuration.
- Add a new SFTP connection called “My RPi”. This will use SSH to copy files to and from the Pi.
- Provide connection settings. You can use a server name or IP address.
- Press the “Test SFTP connection…” button to make sure everything is correct. You may be prompted to accept the new fingerprint. Click Yes.
- Set paths to say where we want to keep this on our remote. You can choose temporary or permanent locations. (I use temporary, but PyCharm’s sync features allow you to make changes on the RPi and download them back to your computer.)
- Now click Tools -> Deployment -> Automatic Upload. This makes it so files will automatically upload whenever you save.
The next task is to set up a remote interpreter. In this section I’ll also show you how to download changes you make on the Pi.
- Now we need a virtual environment on the remote. Create an empty
requirements.txtfile. Save and it should be uploaded automatically. This also creates the remote debug folder.
- Log into the Pi using SSH (I like PuTTY but there are other options). We should be able to see our newly uploaded file.
- Create the virtual environment. First make sure virtualenv is installed.
[shell]sudo pip install virtualenv[/shell]
Then navigate to your project root folder on the remove and create the virtual environment.
pip install RPi.GPIO
pip freeze > requirements.txt
- Now you want to get the updated
requirements.txtfile back to your computer. Right click the file and choose “Sync with Deployed to My RPi…”. You’ll see a compare dialog that allows you to copy the file back to your computer.
- The next step is to add the virtual environment as a project interpreter. Click on File -> Settings.
- Then go to Project: RemoteDebugEx -> Project Interpreter. Click the gear icon and choose Add Remote.
- Choose to use SSH credentials and enter your host and login information. For “Python interpreter path”, choose the “python” file within your virtual environment folder.
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.
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.
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.
- 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
I have a couple of scripts that I keep in the project root for just this purpose. You can download them here.
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.