PyCharm is a full-featured Python editor that includes super helpful things like code completion and error highlighting. It's available for free in a Community Edition.
These steps will help you make the most out of the PyCharm IDE for working with CircuitPython:
By default, PyCharm will auto-save the code file(s) that you are working on very frequently. This is normally great, but with CIRCUITPY drives, it is can be undesirable because it will cause the device to reset and re-run the code more frequently than you might want. When you're using PyCharm to edit files directly on CIRCUITPY drives, it's best to disable the auto-save features and save manually with CTRL+S whenever you're ready to run the new version of code.py.
To disable auto-save, follow these steps:
- Open settings with CTRL+ALT+S or click File -> Settings
- Open up Appearance & Behavior -> System Settings
- Uncheck the box for "Save files if the IDE is idle for X seconds"
- Uncheck the box for "Save files when switching to a different application"
PyCharm likes to create a folder named .idea which holds configuration settings and meta information about your project. This folder gets created in the root directory of the project that you are working in. Ideally, we don't want this to end up on the CIRCUITPY drive, because it will eat up precious storage space and cause the device to reset when files inside of it are altered. The way we avoid this is by creating our project on the computer's hard drive and then adding CIRCUITPY or the directory that contains it as a "content root" so that PyCharm will show the files on the CIRCUITPY drive as part of the project.
- Click File -> New Project
- Enter a name for the project. I like to use DEVICE_WORKSPACE but you can choose any name you like
- Enter or browse to a location on your computer's hard drive to store the project directory in
- Click the Create button
- With your new project open, click File -> Settings or press CTRL+ALT+S to open the settings window
- Open "Project [YOUR_PROJECT_NAME]" then click "Project Structure"
- Click the "+ Add Content Root" button on the right side of the window
The next step depends on your host OS.
If you are on Linux, type or navigate to /media/[username]/ where [username] is your actual username on the computer. Click "Okay" to add this directory as the content root.
If you are on Mac, type or navigate to /Volumes/ then click "Okay" to add this directory as the content root.
If you are on Windows, type or navigate to the drive letter that matches your CIRCUITPY drive, e.g. D:\, then click "Okay" to add this directory as the content root.
After completing these steps, you'll have a project on your computer's hard drive which will get the .idea directory stored inside of it. And you'll also have access to the CIRCUITPY drive(s) that are connected to your computer.
circuitpython-stubs will let PyCharm know more information about the built-in core modules in CircuitPython so that it can offer relevant code hints and typing information. The stubs are published on PyPi, and installing them can be done in the PyCharm settings window.
- Click File -> Settings or press CTRL+ALT+S to open the Settings window
- Open "Project: [YOUR_PROJECT_NAME]" then click Python Interpreter
- Click the + install button on the right side of the window
- Type "circuitpython-stubs" into the search bar
- Select the
circuitpython-stubspackage then press the Install Package button at the bottom left
If you project uses libraries from the CircuitPython Library Bundle, you can install them on your computer if they are published to PyPi. Doing so will give PyCharm information about the library in order to offer more relevant code hints and type information.
Use the above instructions to open the Available Packages window, then enter "adafruit-circuitpython-" into the search box to see all of the libraries deployed to PyPi.
If you use the instructions from Advanced Serial Console on Mac or Advanced Serial Console on Linux, you can connect to the CircuitPython device's serial console inside the Terminal pane within PyCharm.
To open the Terminal pane, click View -> Tool Windows -> Terminal.
Connect to your device using
tio. Then, you can see output and interact with the device and REPL.