简体   繁体   中英

Python won't import correct version of GDAL

I just used home-brew to update my version of GDAL. I was using GDAL version 1.11, which could not read .grd files.

import gdal
import matplotlib.pyplot as plt
from colormap import Colormap

quant = gdal.Open('.../pct1/pct1.grd')

ERROR 4: `/Users/Nate/Documents/CSU/DroughtNet/WebApps/WorldClim_ShinyApp/data/pct1/pct1.grd' not recognised as a supported file format.

I used Homebrew to update my GDAL version to 1.9 to see if that fixes the problem, and GDAL did install successfully. However, I can't get Python to import the appropriate GDAL version, it still imports version 1.11.

I've tried editing the sys.path file, putting the current GDAL framework up front, but that didn't do it. The Homebrew install said to run

mkdir -p /Users/Nate/Library/Python/2.7/lib/python/site-packages
echo 'import site; site.addsitedir("/usr/local/lib/python2.7/site-packages")' >> /Users/Nate/Library/Python/2.7/lib/python/site-packages/homebrew.pth

Which I did, but that didn't work either. Probably because my Python is not run from /Users/Nate/Library/Python/ but comes from a different place. Any suggestions on how to get Python to recognize the newest GDAL version?

UPDATE

Per Matthew's comment, I was able to get the correct version of GDAL (1.9) to load. However, it still won't read my .grd file. Is there an extension that I need to add in order to read these files?

This sounds like a job for a virtual environment. I would create an isolated environment (using virtualenv) from the one you mentioned and try to import GDAL using that Python environment. That way, it will have a fresh path and you can play around with importing it.

If you don't want to use a virtual environment, you should double-check to ensure that you are importing the correct GDAL version. Your script may be importing the old one if 1.9 was written in a separate place, rather than overwriting 1.11

As for debugging steps:

  1. Try printing out the Python path every time you run the script
  2. Print out the version of GDAL each run
  3. Try fiddling around with the inspect module ( documentation ) and see where exactly you are importing GDAL from and see if you can determine anything from there

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM