This is the pseudo-solution I have come up with for this problem.
#pylint: disable=no-name-in-module
from numpy import array as np_array, transpose as np_transpose, \
linspace as np_linspace, zeros as np_zeros
from numpy.random import uniform as random_uniform
#pylint: enable=no-name-in-module
Then, in your code, instead of calling NumPy functions as np.array
and np.zeros
and so on, you would write np_array
, np_zeros
, etc.
Advantages of this approach vs. other approaches suggested in other answers:
- The Pylint disable/enable is restricted to a small region of your code
- That means that you don't have to surround every single line that has an invocation of a NumPy function with a Pylint directive.
- You are not doing Pylint disable of the error for your whole file, which might mask other issues with your code.
The clear disadvantage is that you have to explicitly import every NumPy function you use.
The approach could be elaborated on further.
You could define your own module, call it say, numpy_importer
as follows
""" module: numpy_importer.py
explicitely import numpy functions while avoiding Pylint errors
"""
#pylint: disable=unused-import
#pylint: disable=no-name-in-module
from numpy import array, transpose, zeros #add all things you need
from numpy.random import uniform as random_uniform
#pylint: enable=no-name-in-module
Then, your application code could import this module only (instead of NumPy) as
import numpy_importer as np
and use the names as usual: np.zeros
, np.array
etc.
The advantage of this is that you will have a single module in which all NumPy related imports are done once and for all, and then you import it with that single line, wherever you want. Still you have to be careful that numpy_importer
does not import names that don’t exist in NumPy as those errors won't be caught by Pylint.