There are many great IDEs available for Python, but one of the most powerful has to be Eclipse (or Aptana Studio, an Eclipse derivative geared towards web development) with the PyDev plugin installed.
Some of Eclipse/PyDev’s features include:
- Integrated Python console and debugger
- PyLint integration
The list goes on. One drawback to all of the functionality these tools provide, however, is that figuring out how to use them can be a bit overwhelming for the uninitiated. Indeed, there are a number of books just on using Eclipse! Fortunately though, the most useful features of Eclipse are not hard to get the hang of, so don’t feel like you shouldn’t use Eclipse just because you don’t know what every single feature it has is.
The first thing you need to do is to get started is to install Eclipse and PyDev. The section below describes how to do that. However, an alternative option is to install Aptana Studio 3 which comes pre-installed with PyDev. Aptana Studio 3 includes some additional features as well that are mostly geared towards web development, and also includes a very nice theme system that is not present in the base version of Eclipse. Either solution will work. If you choose to use Aptana Studio skip the first section below and continue at PyLint installation instructions.
Installing Eclipse and PyDev
To begin, download the latest version of Eclipse “Classic” (3.6.2 at the time of writing), and extract the archive to your home directory or some other suitable location. Eclipse is now installed!
Next, open Eclipse and click Help -> Install New Software…
Once the source has been added, check the box next to “PyDev for Eclipse”. Hit “Next” and accept the agreement to add the plugin to Eclipse. It will take a couple minutes now to download and install PyDev. When the installation is finished you will be prompted to restart Eclipse: go ahead and restart it. When Eclipse is first loaded you will be greeted with a Welcome screen with links to tutorials and other information to get you started. For now let’s skip over that and go straight to editing mode. Eclipse allows you to switch between different modes or “perspectives” depending on what you want to do. Each different perspective has its own layout and behavior which are optimized for performing that particular task. For instance there are difference perspectives for working on Java code, for debugging Java, for working on Python, etc. Let’s switch to the Python perspective.
Click Window -> Open Perspective -> Other , and select “PyDev”.
Now we are ready to create a new PyDev project and configure PyDev, but first let’s install a helpful tool that PyDev can make use of: PyLint.
PyLint is a helpful tool that scans Python code and finds lines that may either be problematic or break convention (PEP 8). Usually, PyLint is run on the command-line and displays it output there. When configured in PyDev, however, problematic lines are highlighted in your editor making it easy to quickly spot and correct mistakes. If you haven’t already installed PyLint on your computer, now is a good time to do so.
PyLint should now be installed. To make sure it was installed correctly you can try running “pylint -h” from the command-line to show the help documentation.
Creating a new project
Now that PyDev and PyLint have been installed we are ready to create our first project. In Eclipse/Aptana Studio, click File -> New -> Project and in the PyDev section choose “PyDev Project.” Hit Next. Now, choose a name for the project (e.g. “SunPy”), and under “project contents” uncheck the option “Use default” and in the “Directory” field enter the root folder for the code you want to import, e.g. “/home/user/sunpy-dev.” For Grammar choose either 2.7 or 3.0. Uncheck the “Create default ‘src’ folder…”
Before you can finish the new project creation you will need to configure and select an interpreter that PyDev can use when evaluating your code. Click on the link that says “Please configure an interpreter…” to open the relevant PyDev configuration panel.
The Python interpreter can either be configured automatically or manually. In most cases the Auto Config option will work fine and should be used. The only case where you will need to manually add the interpreter is if you have multiple versions of Python installed on your system and Auto Config finds the wrong one. In that case you will need to click “new” and then specify the location of your Python interpreter (e.g. /usr/bin/python2.7). In either case use the default values suggested when choosing which additional packages to include. Once the interpreter has finished setting itself up, hit “Ok” to finish creating the project.
If all went well you should see your project listed on the left-hand panel. Collapse the folder to see the sub-directories and files within your project. The last thing you need to do here is to tell Eclipse to include your code directory in the Python library path. To do this, right-click on the project name in the left panel and go to “Properties.” In the “PyDev – PYTHONPATH” remove the default path that is added (e.g. “/SunPy/src”). Then click “Add source folder” and choose the “sunpy” directory, e.g. “/SunPy/sunpy.”
Hit “Ok” to accept the changes.
Configuring PyDev to use PyLint
The reason we installed PyLint before creating a new project was so that when we created setup the Python interpreter, the PyLint libraries would be included. If we had created the interpreter before creating a project we would have had to go back to the interpreter configuration and manually add PyLint.
To enable PyLint, open the Eclipse/Aptana preferences dialog by clicking Window -> Preferences and navigate to PyDev -> Pylint.
Check the box next to “Use Pylint?”, and enter the location of your “lint.py” which was installed as part of PyLint. The location will vary from system to system, so you may need to search your system for the file (on a Mac the file location might be something like
Finally, there are two useful arguments which we will tell PyDev to pass to PyLint each time it is executed: the first is “–reports=n” which disables the summary reports that are printed by default each time PyLint is run. The second is “–rcfile=…” Here you should specify the location of the SunPy project rcfile which is located in the the “tools/pylint” folder of the code branch. This will help to ensure that all of the developers working on the project share the same PyLint configuration. Hit “Ok” to accept the changes.
And that’s it! Eclipse and PyDev have a large number of useful features and a multitude of keyboard shortcuts. Take some time to browse through the Eclipse documentation and read other articles to become more familiar with PyDev. It may seem like a lot to take it all at once, but you can learn the features little by little. The first step is simply to get familiar with using Eclipse to edit your code.