PyCharm: Python Development in JetBrains' IDE

Russell Bateman
March 2015
last update:

After 3 months (November 2014-March 2015) of being a money-contributed PyDev user, frustrated with the instability and annoyances:

...I went for PyCharm at the suggestion of a stackoverflow.com respondant. Mind you, it was painful because I'm strictly religious when it comes to Eclipse. However, I have to get work done and I'm betting that PyCharm saves me literally an hour or more during a day of intensive coding and that's not even talking about the sheer frustration of using PyDev.

Words cannot express how embarrassed and heart-broken I am at this. At this point, I would probably not use IntelliJ for Java development, though it does do some things much better (debugger as noted above and also Maven integration), I am religiously devoted. However, for Python development, I'm afraid the goose is cooked.


Installing PyCharm

I'm resisting the call of the purchased package which gives more capabilities that I might need as things go forward. It would be a financial burden to me and the cost if my company were to pay is even higher.

Let's just say that after a bit more than a week of intensive PyCharm use, I'm happy with the community "free" edition and wondering where this will ultimately lead me.

  1. Download tarball from PyCharm website.
  2. Explode.
  3. Relocate where you wish.
  4. Create appropriate launcher for your platform.

PyCharm first run (quick-start)

Here's how to create a project with a small source file and run it. We're doing this in /tmp, but it could be anywhere, even a subdirectory full of crap (like /tmp). Here's our script:

fun.py:
import sys

def main( argv ):
    print 'This is a test'
    print argv

if __name__ == "__main__":
    sys.exit( main( sys.argv ) )

Unlike Eclipse and PyDev, PyCharm will make a project immediately out of any subdirectory, no need to fret over set-up. It will, however, create a subdirectory of its own, .idea, in whatever parent you choose. You can simply discard it once you've finished.

  1. Launch PyCharm.
  2. Choose File → Open...
  3. Navigate to and select the parent subdirectory of the script(s) you want to include in your project.
  4. Click OK, the project is created and you should see whatever happens to be inside already, including your script, fun.py, if you've already created it.

    If you haven't created it, just right-click on the subdirectory/project name at the top of the leftmost pane and choose New → Python file, it asks for a name, you type it in, then click OK, a new window will open and you can type it in.

  5. Right-click on the Python file and choose Run 'filename'. The Run pane opens and the output from running it appears. If you used the one above, you see this:
    /usr/bin/python2.7 /tmp/fun.py
    This is a test
    ['/tmp/fun.py']
    
    Process finished with exit code 0
    
  6. To endow your script with command-line arguments, click in the top menu bar, Run → Edit Configurations.... You'll notice that, under Python, it's created an entry, fun. Click on that and then, to the right, type some arguments into Script parameters:. Click OK.
  7. Run it again just as above to see the difference in output.

To debug your script, you need merely set a breakpoint on an executable line in it by clicking in the space between the displayed line numbers and the code area. A red dot will appear. Instead of choosing Run 'filename' as above, choose Debug 'filename'.


PyCharm keyboard mappings

This topic is identical to last year's tackling of IntelliJ vs. Eclipse habits I was forced to endure because I worked in Java somewhere that required me to use IntelliJ. Therefore, you'll find my comments of that nature at IntelliJ: Configuring keyboard shortcuts.

In particular, I like Eclipse's Ctrl+D and Alt+Left/Right mappings and have changed IntelliJ's accordingly. There's a lot of power in these for IntelliJ (and probably more in Eclipse I don't know about), but those three are the most frequently used. Also, I'd like to map IntelliJ's Find usages to Eclipse's Shift+Ctrl+G and a couple of others when I get around to them.

Show method (function) separators: Settings → Editor → General Appearance → Show method separators


PyCharm editor font

Source Code Pro seems nice. See here.


Set Python version

Now that python 3 has become common, perhaps even most common, setting to use Python 2 in the IDE is something to know how to do:

  1. Click Settings icon in toolbar (or File → Settings...).
  2. Go to Project: project-name.py → Project Interpreter.
  3. Click the Project Interpreter: list and choose Show All if Python 2.7 (for example) is not shown.
  4. Click the + sign to add.
  5. Navigate to the Python binary and select it,*, probably python2.7.

* If you don't know where this is, consider this exercise:

russ@nargothrond:~$ which python
/usr/bin/python
russ@nargothrond:~$ python --version
Python 2.7.15rc1

Fix missing third-party or system import
  1. Click Settings icon in toolbar (or File → Settings...).
  2. Go to Project: project-name.py → Project Interpreter.
  3. Look for the package, e.g.: docker, and select it. If your package doesn't appear, do you have the right interpreter? (E.g.: Python 3.6 when your project is Python 2.7.)
  4. Click OK.
  5. Wait a moment for the red underlining to begin to disappear.