没有名为osgeo.ogr的模块

kar*_*njo 5 python virtualenv gdal

我需要将osgeo.ogr模块导入virtualenvpython程序。

全局python具有以下模块:

user@ubuntu:~/$ python
Python 2.7.8 (default, Oct 20 2014, 15:05:19)
[GCC 4.9.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import osgeo
>>>
Run Code Online (Sandbox Code Playgroud)

但是进入virtualenv(没有--no-site-packages):

user@ubuntu:~$ temp/bin/python
Python 2.7.8 (default, Oct 20 2014, 15:05:19) 
[GCC 4.9.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import osgeo
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named osgeo
Run Code Online (Sandbox Code Playgroud)

这种行为的原因是什么?

Sar*_*Rai 5

可以用这个解决

pip install --global-option=build_ext --global-option="-I/usr/include/gdal" GDAL==`gdal-config --version`    
Run Code Online (Sandbox Code Playgroud)


kar*_*njo 4

问题是这样解决的:

(VIRTUAL_ENV)user@ubuntu:~$ pip install --no-install GDAL==1.11.2
Run Code Online (Sandbox Code Playgroud)

1.11.2,因为我的GDAL版本是1.11.2:

(VIRTUAL_ENV)user@ubuntu:~$ gdal-config --version
1.11.2
Run Code Online (Sandbox Code Playgroud)

下一个:

(VIRTUAL_ENV)user@ubuntu:~$ cd ~/.virtualenvs/VIRTUAL_ENV/build/GDAL/
(VIRTUAL_ENV)user@ubuntu:~/.virtualenvs/VIRTUAL_ENV/build/GDAL$ python setup.py build_ext --include-dirs=/usr/include/gdal/
(VIRTUAL_ENV)user@ubuntu:~/.virtualenvs/bamap/build/GDAL$ pip install --no-download GDAL
Run Code Online (Sandbox Code Playgroud)

--include-dirs=/usr/include/gdal/对我来说是正确的。GDAL 与 Quantum GIS 一起安装在我的系统中。如果您手动构建 GDAL,路径可能是其他路径。完整版本:

python setup.py build_ext --gdal-config=/YOUR_PATH/bin/gdal-config --library-dirs=/YOUR_PATH/gdal/VERSION_GDAL/lib --libraries=gdal --include-dirs=/YOUR_PATH/gdal/VERSION_GDAL/include
Run Code Online (Sandbox Code Playgroud)

详细信息可以在这里找到。