
- Python 3 Basic Tutorial
- Python 3 - Home
- What is New in Python 3
- Python 3 - Overview
- Python 3 - Environment Setup
- Python 3 - Basic Syntax
- Python 3 - Variable Types
- Python 3 - Basic Operators
- Python 3 - Decision Making
- Python 3 - Loops
- Python 3 - Numbers
- Python 3 - Strings
- Python 3 - Lists
- Python 3 - Tuples
- Python 3 - Dictionary
- Python 3 - Date & Time
- Python 3 - Functions
- Python 3 - Modules
- Python 3 - Files I/O
- Python 3 - Exceptions
What are the âbest practicesâ for using import in a Python module?
The import statement, just like any other statement or keyword in Python should be used and added to the code properly following the best practices. Letâs see them one by on â
Multiple Imports
Multiple Imports should usually be on separate lines. For example â
import numpy import pandas import matplotlib
Always on the Top
Imports are always put at the top of the file i.e.
- After any module comments and docstrings
- Before module globals and constants.
For example â
# import the numpy module import numpy
Import Modules in an Order
A good practice is toimport modules in the following order â
- Standard library modules â e.g. sys, os, getopt, re
- Third-party library modules â e.g. ZODB, PIL.Image, etc.
- Locally developed modules
Absolute Imports
Absolute imports are recommended, as they are usually more readable and tend to be better performed if the import system is incorrectly configured. For example â
import mypkg.sibling from mypkg import sibling from mypkg.sibling import example
Wildcard imports (from import *) should be avoided
Avoid the Wildcard imports since they make it unclear which names are present in the namespace, confusing both readers and many automated tools.
Circular Import
To avoid problems with circular imports, it is sometimes necessary to move imports to a function or class. Gordon McMillan says â
âCircular imports are fine where both modules use the âimport <module>â form of import. They fail when the 2nd module wants to grab a name out of the first (âfrom module import nameâ) and the import is at the top level. Thatâs because names in the 1st are not yet available, because the first module is busy importing the 2nd.â
The import can easily be moved into that function, if the second module is only used in one function. By the time the import is called, the first module will have finished initializing, and the second module can do its import.
If some of the modules are platform-specific, it may also be necessary to move imports out of the top level of code. In that case, it may not even be possible to import all of the modules at the top of the file. Import the exact modules in the corresponding platform-specific code is a good option.
- Related Articles
- What are the best practices for using loops in Python?
- What are the best practices for using if statements in Python?
- What are the best practices for exception handling in Python?
- What are the best practices for committing in Git?
- What are Python coding standards/best practices?
- Import a module in Python
- What are the best practices for function overloading in JavaScript?
- What are the best practices to organize Python modules?
- Import module in Python
- Best practices for using MySQL indexes?
- What are the Best Practices in Visual Marketing?
- What are the best practices to be followed while using JavaScript?
- Best practices for writing a Dockerfile
- What are the best practices to keep in mind while using packages in Java?
- Best practices for Java comments.
