Let PyCharm Do Your Import Janitorial Work
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.
Subscribe to Blog updates
Thanks, we've got you!
Five Things To Love About the New UI
Are you using the New UI yet? Not yet? Let me tell you why it's the best thing since sliced bread! Let’s get it enabled and take a look around. The easiest way to do that is from the Settings cog at the top-right of the UI: PyCharm will need to restart, but that’s it; you’re done! In t…
Tutorial: Visual testing with pytest
If you are like many Python developers out there you LOVE writing code! Tests? Not so much. Whether it’s the concept of testing or the interface of testing tools, testing is a chasm not all developers cross. The Visual Testing with pytest tutorial aims to bring Python testing to the masses, tak…
Django Custom Tags in PyCharm Professional 2020.1
PyCharm started its life with Django support and our new release adds some useful improvements for Django coding. Let's take a look at support for custom Django tags in PyCharm Professional 2020.1. But First, One More Thing Maybe it's bragging, but we need to show one existing feature related to thi…
Python 3.8 support in PyCharm
The release of Python 3.8 brought new features to the Python coding realm. The language is evolving according to its community's needs by addressing cases where new syntax or logic become necessary. From new ways of assigning expressions to restriction of usage of function declarations, calls, and v…