Using PyCharm

A few years ago I switched from Sublime to PyCharm for writing Python code. I have tried a number of editors including Atom, Sublime, Visual Studio Code and I like them all but I have settled on PyCharm as it is a better fit for me. This post is going to outline a couple of key points in setting up a project. I plan to rely on and fine tune this post as a reference when trying to discuss some more specific topics.

PyCharm can seem a bit daunting, especially if you are wanting to edit a single file, but for project work it makes perfect sense. I want an editor that can handle everything for me. For this post I'm going to go over setting up a project with a virtual environment and set some environment variables so we don't have to put secret passwords in the main body of the code.

Create a Project

At this point I see no reason if starting a project to not be using Python 3.7 or higher. So be sure to have Python installed so PyCharm can use a valid interpreter, then create a new project (File -> New Project).

Virtual Environment

In the dialog that opens, click on the little triangle to the left of "Project Interpreter" to expand that section. Select the option to create a new virtual environment using Virtualenv and enter a location for your project. I typically set aside a folder in my home directory for pycharm_projects and place the projects under that. The base interpreter should point to your python.exe or python bin file (in my setup it's at /usr/bin/python3.7). After all that click the "Create" button.

Using a virtual environment allows you set up an environment specially for this project. This allows you to install other versions of python on your computer, but this environment can be fixed to a particular version. This is important when you have to match a production server somewhere or when you want to try out a new version without breaking all of your old code.

Edit Configurations

There is a little drop down in the tool bar called 'Edit Configurations'. I recommend using the configuration to set up an easy way to run a script. Within that configuration you can set environment variables for things like your database username and password. When set as environment variables, you can use the environment variable name in your code instead of the actual username and password. Environment variables can also greatly assist when you have multiple developers, staging servers, and production servers. Each instance can have it's own set of file paths, database credentials and other variables.

Once the Edit Configuration dialog opens, click the green plus sign, pick Python from the list and fill out the form. Be sure to name it appropriately (by default it is 'Unnamed'). The script path will be the path to the file you want to run (we haven't covered that yet, but it's typically a file you create in your project). The three little dots to the right of Environment Variables is where you'll be able to enter and save your environment variables.

For a much better explanation of why to setup configurations like this, read the 12 Factor App with particular attention to the configuration section.

Other Stuff

PyCharm offers all sorts of great other features. Of particular interest is the Visual Debugger. Once you get some familarity with PyCharm and if it suits your style, mastering the editor will make you a far more efficient coder. To assist in that, JetBrains has a bunch of videos available to get you started and Michael Kennedy of the Talk Python to Me podcast has a Mastering PyCharm course available.