2023-05-03 11:06:20 +02:00
== Why is this an issue?
2021-01-27 13:42:22 +01:00
Importing every public name from a module using a wildcard (``++from mymodule import *++``) is a bad idea because:
2020-06-30 14:49:38 +02:00
2020-06-30 12:48:07 +02:00
* It could lead to conflicts between names defined locally and the ones imported.
* It reduces code readability as developers will have a hard time knowing where names come from.
* It clutters the local namespace, which makes debugging more difficult.
2023-10-18 11:00:07 +02:00
Remember that imported names can change when you update your dependencies. A wildcard import that works today might be broken tomorrow.
2020-06-30 12:48:07 +02:00
2023-10-18 11:00:07 +02:00
=== Exceptions
No issue will be raised in ``++__init__.py++`` files. Wildcard imports are a common way of populating these modules.
No issue will be raised in modules doing only imports. Local modules are sometimes created as a proxy for third-party modules.
[source,python]
----
# file: mylibrary/pyplot.py
try:
from guiqwt.pyplot import * # Ok
except Exception:
from matplotlib.pyplot import * # Ok
----
Just keep in mind that wildcard imports might still create issues in these cases. It's always better to import only what you need.
== How to fix it
2021-02-02 15:02:10 +01:00
2020-06-30 12:48:07 +02:00
There are two ways to avoid a wildcard import:
2020-06-30 14:49:38 +02:00
2023-10-18 11:00:07 +02:00
* Replace it with `import mymodule` and access module members as `mymodule.myfunction`. If the module name is too long, alias it to a shorter name. Example: `import pandas as pd`
2020-06-30 12:48:07 +02:00
* List every imported name. If necessary import statements can be split on multiple lines using parentheses (preferred solution) or backslashes.
2023-10-18 11:00:07 +02:00
=== Code examples
==== Noncompliant code example
2020-06-30 12:48:07 +02:00
2022-02-04 17:28:24 +01:00
[source,python]
2020-06-30 12:48:07 +02:00
----
from math import * # Noncompliant
def exp(x):
pass
print(exp(0)) # "None" will be printed
----
2023-10-18 11:00:07 +02:00
==== Compliant solution
2020-06-30 12:48:07 +02:00
2022-02-04 17:28:24 +01:00
[source,python]
2020-06-30 12:48:07 +02:00
----
import math
def exp(x):
pass
print(math.exp(0)) # "1.0" will be printed
----
Or
2020-06-30 14:49:38 +02:00
2022-02-04 17:28:24 +01:00
[source,python]
2020-06-30 12:48:07 +02:00
----
from math import exp as m_exp
def exp(x):
pass
print(m_exp(0)) # "1.0" will be printed
----
2023-05-03 11:06:20 +02:00
== Resources
2020-06-30 12:48:07 +02:00
* https://docs.python.org/3.8/reference/simple_stmts.html#import[Python documentation - The import statement]
2021-06-02 20:44:38 +02:00
2021-06-03 09:05:38 +02:00
ifdef::env-github,rspecator-view[]
2021-09-20 15:38:42 +02:00
'''
== Implementation Specification
(visible only on this page)
2023-05-25 14:18:12 +02:00
=== Message
Import only needed names or import the module and then use its members.
=== Highlighting
* Primary: the whole import statement
2021-09-20 15:38:42 +02:00
2021-06-08 15:52:13 +02:00
'''
2021-06-02 20:44:38 +02:00
== Comments And Links
(visible only on this page)
2023-05-25 14:18:12 +02:00
=== on 23 Apr 2015, 09:36:39 Ann Campbell wrote:
\[~elena.vilchik] the docs.quantifiedcode.com link was 404. Would you supply the code samples, please?
=== on 23 Apr 2015, 10:16:47 Elena Vilchik wrote:
\[~ann.campbell.2] \http://webcache.googleusercontent.com/search?q=cache:JOuoj37QmGUJ:docs.quantifiedcode.com/python-code-patterns/maintainability/wildcard_import.html+&cd=3&hl=ru&ct=clnk&gl=fr here is saved copy. I don't know what happened :(
include::../comments-and-links.adoc[]
2021-06-03 09:05:38 +02:00
endif::env-github,rspecator-view[]