PythonStyleGuide

From Wikitech
Jump to: navigation, search

In general we follow the official style guide for Python code PEP8. The intent of this page is to specify a common style guide for those aspects that are not unambiguously defined in the PEP8 style guide.

Imports

Regarding imports, the PEP8 import section gives only some indication of good and bad practices, not a detailed guide. This schema aim to give a more complete style guide for imports, where each block is separated from the others by an empty line (the comments are just for explanation purposes).

# Full import of stdlib modules, in alphabetic order
import a_stdlib_module
import b_stdlib_module

# Submodules imports of stdlib modules, in alphabetic order both vertically and horizontally
from another_stdlib_module import a_stdlib_submodule, b_stdlib_submodule
from c_stdlib_module import another_stdlib_submodule, last_stdlib_submodule

# Full import of 3rd party modules, in alphabetic order
import a_third_party_module
import b_third_party_module

# Submodules imports of 3rd party modules, in alphabetic order both vertically and horizontally
from another_third_party_module import a_third_submodule, b_third_submodule
from c_third_party_module import another_third_submodule, last_third_submodule

# Full import of current application modules, in alphabetic order and with absolute imports
import myapp.a_module
import myapp.b_module

# Submodules imports of current application modules, in alphabetic order both vertically and horizontally
from my_app.another_module import a_submodule, b_submodule
from my_app.c_module import another_submodule, last_submodule

See also