Configure PyCharm's standard library list?

Answered

When ordering/optimizing imports, PyCharm treats builtins as a third party import versus standard library.

PyCharm's docs say it formats the existing import statements according to the Style Guide for Python Code,

which states

Imports should be grouped in the following order:

  1. Standard library imports.
  2. Related third party imports.
  3. Local application/library specific imports.

For example, PyCharm organizes

import logging
from builtins import object

import six

as

import logging

import six
from builtins import object

Is there a way to configure PyCharm's standard library list to work around this?

5 comments
Comment actions Permalink

Are you running 2018.3.3 version of PyCharm?

If not, please update and try again.

0
Comment actions Permalink

Yes, I am running 2018.3.3 and this is the behavior I am seeing. Do you see different behavior with that snippet above?

 

My version info:

PyCharm 2018.3.3 (Community Edition)
Build #PC-183.5153.39, built on January 9, 2019
JRE: 1.8.0_152-release-1343-b26 x86_64
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
macOS 10.14.2

0
Comment actions Permalink

Yes, when I hit Optimize Imports is stays

import logging
from builtins import object

import six

Please try starting with defaults and without custom plugins by renaming/removing configuration and plugins folders https://intellij-support.jetbrains.com/hc/en-us/articles/206544519.

If doesn't help, please upload your logs folder zipped (Help | Compress Logs and Show in...) to https://uploads.services.jetbrains.com/ and let me know the name of that zip file.

0
Comment actions Permalink

I think I see the problem. My project needs to support both python 2.7 and 3.7. When I have the project interpreter set for 2.7, the imports are mis-ordered as described above. When I set it to 3.7, they get optimized/ordered correctly.

Before I tried changing the interpreter, I did remove all configs/plugins per those instructions but the behavior remained for python 2.7.

I guess I can just leave the project interpreter as 3.7 since our CI runs tests in both 2.7 and 3.7, but it would be nice if I could get Optimize Imports to work the same in both Pythons.

 

0
Comment actions Permalink

Oh, I see, thank you for clarification!

Then it seems to be related to https://youtrack.jetbrains.com/issue/PY-26512, please vote for it and follow for updates.

0

Please sign in to leave a comment.