Whenever you use autoimport provided by PyCharm it generates an absolute path import. i.e.
from my_package.my_subpackage import my_thing
instead of
from .my_subpackage import my_thing
Is there a setting to use relative imports instead of absolute ones when importing a python package?
Troubleshooting: Try installing/importing a package from the system terminal (outside of PyCharm) using the same interpreter/environment. In case you are using a virtualenv/conda environment as your Project Interpreter in PyCharm, it is enough to activate that environment in the system terminal and then do the test.
please use the default key combination below. CTRL + ALT + O ---> to remove the unused imports in windows. However, you can also change the keymap of "Optimize Imports" in settings.
PyCharm can do both. Type the name of the package and hit Alt-Enter , then choose Install and Import package . PyCharm will do both: you'll see a notification during the installation, then the import will be generated in the right way, according to your project styles.
PyCharm can clean all of that up using Optimize Imports , which cleans up your imports based on settings you can save. This action can be run on a single file or across all files in a project. Also, the Commit dialog box has a checkbox to run it in the Before Commit section.
It will be possible starting with 2019.3 https://youtrack.jetbrains.com/issue/PY-6054 Note, that it will not do the relative import automatically if there are no other relative imports in that file yet. There is also an action to convert the existing absolute import to relative:
It appears currently there is no way to change the import style for auto-import feature to use relative imports. The only style changes you can make to import statements are how the absolute imports are structured:
(The Jetbrains/PyCharm settings search functionality is excellent by the way).
The same thing happens when refactoring, it's definitely an option I'd like to see added.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With