PyCharm Setup for ROS
Table of Contents
1 Introduction
PyCharm is a Python IDE that many previous students have found useful for learning and developing Python code in the past. It features an editor, a debugger, code completion, and more. I personally don't use any IDE when developing Python (I just use a text editor and command-line debuggers), but you may find this to be a very useful tool.
2 PyCharm for ROS:
The first step is to Download PyCharm. By default, you are only granted a 30 day trial (at the time of this writing), but you can apply for a free professional license as a student. Once downloaded, follow the installation instructions listed on their site to install. I'm reproducing the installation instructions below for convenience:
- Copy the
pycharm-2017.2.3.tar.gz
to the desired installation location (make sure you have rw permissions for that directory) - Unpack the
pycharm-2017.2.3.tar.gz
using the following command:tar -xzf pycharm-2017.2.3.tar.gz
- Remove the
pycharm-2017.2.3.tar.gz
to save disk space (optional) - Run
pycharm.sh
from the bin subdirectory
Note that PyCharm has tools for creating both a launcher for the command line
(so you don't always have to cd
to the bin/
directory to run PyCharm), and a
launcher for the Desktop (this will allow PyCharm to be executed from your
system menus or from icons on your desktop). Both of these options are available
under the Tools
menu within PyCharm.
If you launch PyCharm from the command line, it will automatically inherit the
environment from your current terminal (including your ROS environment
variables). However, if you are setting the environment variables in your
.bashrc
, then PyCharm won't have any ROS environment variables, and it may
have difficulty finding ROS Python libraries and message definitions. So if you
want to launch PyCharm from a desktop icon or from a system menu, you may want
to edit the file that PyCharm created when you ran Tools->Create Desktop
Entry...
. Running that tool should have created a file in
~/.local/share/applications/jetbrains-pycharm.desktop
If you open that file, you could change the following line:
Exec = "<PATH-TO-PYCHARM-INSTALL>/bin/pycharm.sh"% f
to be
Exec = bash -i -c "<PATH-TO-PYCHARM-INSTALL>/bin/pycharm.sh"% f
the bash -i -c
. This opens a new bash interactive session (which runs your
.bashrc
, and then it executes the PyCharm shell script in that environment.
3 Adding Projects
Assuming you already have your workspace setup, you can simply import a package
into PyCharm using Open
and selecting the project you want. In the picture
below, I selected the basics project
If you don't have any projects setup yet, you can create a new project and assign an appropriate name. However, if you want to use PyCharm with ROS, it is easier to setup packages in a terminal then import them into PyCharm.
Next you want to make sure you're using Python 2.7 as your interpreter. Go to
Settings->Project->Project Interpreter
.
Run roscore
in your terminal and you're all set. You can now edit and run
nodes from PyCharm
If you run the same node from the command line you get the same output
4 Debugging
A cool feature of PyCharm is it's debugging feature. You can set breakpoints by simply clicking besides the line number. debugging allows you step in and out of functions and see how exactly your code works. It also lets you look behind the curtain and see the underlying files that makes your code run