In Python 3.7 I would like to import few methos and use them later in the same script. Before that I would like to check if they are correctly imported.
Based on this answer I can check if a module is fully imported into the script but how if I imported only one method with the form from X import Y
?
What I have done
I have done the following snippet:
from shapely.geometry import asShape
from shapely.geometry import Point
import sys
if 'shapely.geometry.asshape' in sys.modules:
print('Shapely Geometry asshape ok')
else:
print('Shapely Geometry asshape NOT loaded')
if 'shapely.geometry.point' in sys.modules:
print('Shapely Geometry point ok')
else:
print('Shapely Geometry point NOT loaded')
which gives me the following results:
Shapely Geometry asshape NOT loaded
Shapely Geometry point ok
Duplicate question
I don't this this is a duplicate question as all other questions here on SO are about check if a module is imported (with the form import Z
) and not only some methods.
It doesn't matter whether you import just one function from a module, or import the whole module itself, whole module is always imported into sys.modules
. So, in your case, you'll have to check for imported module instead of the function:
'shapely.geometry' in sys.modules
Check out this qustion 'import module' vs. 'from module import function'.