0

I created a new conda environment with python version 3.7

Then installed

conda install -c conda-forge gdal=2.4

Then installed gdal2tiles

pip install gdal2tiles

and then python test.py

#test.py
import gdal2tiles
gdal2tiles.generate_tiles('xyz.tif', 'output_dir')

but I am getting an error

AttributeError: module 'gdal2tiles' has no attribute 'generate_tiles'

It's the same method that I copied from the documentation but doesn't seem to work. https://pypi.org/project/gdal2tiles/

2 Answers 2

1

Unable to reproduce the issue with the following recipe

conda create --name gdaldemo python=3.7 -y
conda activate gdaldemo
conda install -c conda-forge gdal=2.4 -y
python -m pip install gdal2tiles
# Last command to check the issue
python -c "import gdal2tiles;print(gdal2tiles.generate_tiles)"

It seems you may have a conflict between GDAL native gdal2tiles.py and the package gdal2tiles.

If it echoes something when executing the following, it means your import gdal2tiles is using the default provided by GDAL and not the one by your 3rd party library.

python -c "import gdal2tiles;print(gdal2tiles.GDAL2Tiles)"
8
  • I still have the same error. Would that be a problem on windows machine? Which os it is that you are using? Commented May 19, 2021 at 10:20
  • Ubuntu 18.04 64 bits
    – ThomasG77
    Commented May 19, 2021 at 10:24
  • Yes it echos <class 'gdal2tiles.GDAL2Tiles'> Commented May 19, 2021 at 11:47
  • What python -c "import sys;print(sys.path);" returns?
    – ThomasG77
    Commented May 19, 2021 at 11:58
  • 1
    At the beginning of the Python script, do import sys;sys.path.insert(0, "C:\Users\usr\AppData\Local\Continuum\anaconda3\Lib\site-packages") After, you should be able to run your script. Clearly, not the best practice as normally it's related to Python messed environment(s) but too difficult to solve without this hackish approach...
    – ThomasG77
    Commented May 19, 2021 at 15:52
0

As mentioned in answer from ThomasG77, the problem is that your python interpreter is trying to use the gdal2tiles.py script from gdal. It is not possible to download the gdal2tiles package from pip under a different name see this answer here.

A quick and dirty solution would be to download the the repo from gitlab and add it to your project. Then it is possible to import the function via a relative import:

from .gdal2tiles.gdal2tiles import generate_tiles

It would be great if the owner of the package could rename it to a different name, so people would not have this problem any more.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.