What's the correct way to sort Python `import x` and `from x import y` statements?

后端 未结 6 1239
半阙折子戏
半阙折子戏 2020-12-04 05:32

The python style guide suggests to group imports like this:

Imports should be grouped in the following order:

  1. standard library imp
6条回答
  •  青春惊慌失措
    2020-12-04 06:09

    The PEP 8 says nothing about it indeed. There's no convention for this point, and it doesn't mean the Python community need to define one absolutely. A choice can be better for a project but the worst for another... It's a question of preferences for this, since each solutions has pro and cons. But if you want to follow conventions, you have to respect the principal order you quoted:

    1. standard library imports
    2. related third party imports
    3. local application/library specific imports

    For example, Google recommend in this page that import should be sorted lexicographically, in each categories (standard/third parties/yours). But at Facebook, Yahoo and whatever, it's maybe another convention...

提交回复
热议问题