What’s something you do all the time in Python? Import modules from packages. Not just that, you also fiddle with the formatting to make the style nannies happy. And remove unused imports. And bunches of other janitorial tasks.
This support is both super-helpful and underappreciated. Let’s solve that with a deep dive on PyCharm’s import support, including new import styling features in PyCharm 2018.3.
You’re coding away, in the zen flow, and then…you want to use the
requests library. It’s installed in your virtualenv, you just need to import it. So away you go, up to the top of the file, looking for the right place to manually import it.
Instead, let PyCharm do it for you. Start typing part of requests and type
Ctrl-Space-Space. PyCharm will offer a completion. When you accept it, PyCharm will complete the symbol and also generate the import.
Sometimes you cut-and-paste some code and already have
requests typed out completely. Or, perhaps you can type faster than doing autocomplete. Put your cursor somewhere in
requests and hit
Alt-Enter to bring up the Code Inspection. PyCharm has a choice to
Import this name. As with
Ctrl-Space, PyCharm generates the import:
If you already have an import from that package, PyCharm merges into the existing import line:
PyCharm calls this the Import Assistant. You stay where you are, it manages the import.
Generating the import is half the annoyance. Equally frustrating? Constant gardening of the long list of imports: re-sorting, pruning unused imports, joining or splitting. Janitorial work like that is what PyCharm lives for.
First, let’s put our imports into a messy state: bad sorting and unused imports.
Optimize Imports action to let PyCharm clean this up. You can trigger this action with
Ctrl-Alt-O but, if you haven’t memorized that, use
Find Action. The result: nice import lines:
PyCharm can optimize imports in a single file. But you can also optimize imports across the entire project. Select the folder at your project root, then trigger
Optimize Imports (from the menu, the shortcut, or Find Action.)
Working with Packages
That’s two drudgeries in the bag already — generating imports and gardening the import list. Here’s another: what if you don’t have the package installed yet?
We saw with imports the zen mode: you start typing and tell PyCharm to do the work. Same here: type your import, hit
Alt-Enter, and choose
Install and import package:
This is explained further in the tip on the help page.
Thus, in the middle of using the import, rather than interrupt your flow, tell PyCharm to go install the package into your project interpreter. As a bonus, if you have a
requirements.txt registered with your project, then PyCharm can record this package as an entry. That’s a nice flow.
PyCharm follows PEP 8’s guidance on import style. For example, PyCharm will warn you if you have an import mixed into your module code, instead of at the top. By default,
Optimize Imports will do the joining and sorting from PEP 8. And finally, if you generate another import from an already-imported package, PyCharm will add the new symbol to the existing import line.
What if you want some flexibility? PyCharm’s project preferences let you change sorting and the joining behavior:
I have a colleague who wants easier-to-track diffs by putting each import on its own line. PyCharm lets you toggle this setting, switching from joining imports to splitting them.
We often say that a big part of PyCharm’s value is how it does the janitorial work for you. Managing imports is a perfect example of that.