I am unable to import geocube at all in my virtual environment, although I have installed it and my venv is activated.
>>> from geocube.api.core import make_geocube
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\andrei.nita\Documents\PG\batch\pgtopy\geocube.py", line 10, in <module>
from geocube.api.core import make_geocube
ModuleNotFoundError: No module named 'geocube.api'; 'geocube' is not a package
What is strange is that if I start a python console from my env\Scripts and import geocube from that folder…it works. Is there a way to fix the path to the module it self? Is it something related to the Python interpreter? I get this error in the conda environments as well. I am using Python 3.10.9.
My venv has the following packages:
(env) C:\Users\MyName\Documents\MyFolder\pgtopy>pip list
Package Version
----------------- ------------
affine 2.4.0
appdirs 1.4.4
attrs 23.1.0
cachetools 5.3.2
certifi 2023.7.22
click 8.1.7
click-plugins 1.1.1
cligj 0.7.2
colorama 0.4.6
fiona 1.9.5
GDAL 3.4.3
geocube 0.4.2
geopandas 0.14.0
numpy 1.26.1
odc-geo 0.4.1
packaging 23.2
pandas 2.1.2
pip 22.3.1
psutil 5.9.6
psycopg 3.1.12
pyparsing 3.1.1
pyproj 3.6.1
python-dateutil 2.8.2
pytz 2023.3.post1
rasterio 1.3.9
rioxarray 0.15.0
scipy 1.11.3
setuptools 65.5.0
shapely 2.0.2
six 1.16.0
snuggs 1.4.7
typing_extensions 4.8.0
tzdata 2023.3
xarray 2023.10.1
>Solution :
Your script file name is geocube.py, you’re most likely importing from your own file, not the package geocube. Check out The Module Search Path. Here is an excerpt:
When a module named
spamis imported, the interpreter first searches
for a built-in module with that name. These module names are listed in
sys.builtin_module_names. If not found, it then searches for a file
namedspam.pyin a list of directories given by the variablesys.path.
sys.pathis initialized from these locations:
The directory containing the input script (or the current directory when no file is specified).
PYTHONPATH(a list of directory names, with the same syntax as the shell variablePATH).The installation-dependent default (by convention including a site-packages directory, handled by the site module).