6

Here is the situation: I've installed QGIS 3.10 with the downloadable .exe file and accepting all as predefined. The Python version used with it is 3.7.0 like here:

import sys
sys.version
'3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:59:51) [MSC v.1914 64 bit (AMD64)]'

The thing now is that I'm working on a script that need GeoPandas and I'm not able to install or import it:

import geopandas

The above command prompts this ModuleNotFoundError error:

Traceback (most recent call last):
  File "C:\PROGRA~1\QGIS3~1.10\apps\Python37\lib\code.py", line 90, in runcode
    exec(code, self.locals)
  File "<input>", line 1, in <module>
  File "C:/PROGRA~1/QGIS3~1.10/apps/qgis/./python\qgis\utils.py", line 737, in _import
    mod = _builtin_import(name, globals, locals, fromlist, level)
ModuleNotFoundError: No module named 'geopandas'

I tried to install Anaconda and within it I'm able to install pip and GeoPandas without any issues but it comes with Python 3.7.4. I have no deep understanding about how several Python versions work together within the same PC.

So, my issue here is easy: I need to be able to import GeoPandas in the Python console regarding that I use QGIS 3.10 and it works with Python 3.7.0. I use Windows 10.

2
  • 2
    Have you tried installing QGIS in a conda environment? Here is a great tutorial on the subject: gisunchained.wordpress.com/2019/05/29/using-qgis-from-conda
    – Aaron
    Commented Jan 8, 2020 at 18:33
  • 1
    I'm aware of it, thanks. The goal is to create a python script that works in several machines and with users that doesn't have any knowledge about python. I hope there is a simple solution on this just to import and execute geopandas tools from the python console in QGIS 3.10 Commented Jan 9, 2020 at 7:17

4 Answers 4

3

In order to install the module with the right Python, it is better to use python -m pip install geopandas instead of pip install geopandas. You can follow the following steps:

  1. Find python.exe which is in your QGIS folder. (Example: C:\OSGEO4~1\apps\Python37)
  2. Opens a command prompt
  3. Type the following command: cd C:\OSGEO4~1\apps\Python37 & python -m pip install geopandas

Replace the OSGEO path with your QGIS path.

3
  • 1
    Thanks. Now I get the following error when installing geopandas: "Collection fiona [...] ERROR: Command errored out with exit status 1: [...] Complete output (1 lines): A GDAL API version must be specified. Provide a path to gdal-config using a GDAL_CONFIG environment variable or use a GDAL_VERSION environment variable. Commented Jan 9, 2020 at 8:18
  • The installation of the geopandas module requires numerous dependencies on other modules and frequently returns numerous error messages. A similar question was asked in the following post: stackoverflow.com/questions/54734667/… Commented Jan 9, 2020 at 8:25
  • I though this would be much easier, like a pair of code lines or something... When I try that fiona manual instalation a new problem comes to light: error: Microsoft Visual C++ 14.0 is required. Get it with "Build Tools for Visual Studio": visualstudio.microsoft.com/downloads. This just never ends... Commented Jan 9, 2020 at 9:28
3

Try the following procedure. This worked for me with QGIS 3.10.1 and Windows 10. The only difference is that I have an OSGeo4W install, so hopefully it will work for you with a standalone install.

Firstly, go to https://www.lfd.uci.edu/~gohlke/pythonlibs/#fiona and download the file:

Fiona‑1.8.13‑cp37‑cp37m‑win_amd64.whl

Next create a batch file with the following contents (this should work for your standalone installation):

@echo off
rem Root OSGEO4W home dir to the same directory this script exists in
SET OSGEO4W_ROOT=C:\PROGRA~1\QGIS3~1.10
call "%OSGEO4W_ROOT%"\bin\python-qgis.bat"

rem List available o4w programs
rem but only if osgeo4w called without parameters
@echo on
@if [%1]==[] (echo run o-help for a list of available commands & cmd.exe /k) else (cmd /c "%*")

For an OSGeo4W64 installation, the batch file looks like this:

@echo off
rem Root OSGEO4W home dir to the same directory this script exists in
SET OSGEO4W_ROOT=C:\OSGeo4W64
call "%OSGEO4W_ROOT%"\bin\python-qgis.bat"

rem List available o4w programs
rem but only if osgeo4w called without parameters
@echo on
@if [%1]==[] (echo run o-help for a list of available commands & cmd.exe /k) else (cmd /c "%*")

Just make sure that OSGEO4W_ROOT is set to the directory which contains your installation folders (apps, bin etc). Save this with a .bat extension- preferably in the same OSGEO4W_ROOT directory.

Now, when you double click this batch file it should open you up to a Python prompt in a command window. Type quit() and hit enter. This should return you to a command prompt with the qgis python environment correctly set.

The following steps are more or less based on Underdark (Anita Graser)'s instructions here.

Type pip install geopandas and hit enter. The installation will error at Fiona.

Now type 'pip install C:\Full\Path\To\Fiona‑1.8.13‑cp37‑cp37m‑win_amd64.whl' and hit enter. Note - you need to type the full path to the location where you saved the wheel (.whl) file including the file name. This should successfully install Fiona.

Now type pip install geopandas again and hit enter. If all goes well, this will successfully install geopandas.

Now you can just type exit() and hit enter to exit the command window. Job done!

1
  • For a stand alone QGIS I get this error, any idea ? ERROR: Command errored out with exit status 1: 'C:\PROGRA~1\QGIS3~1.10\bin\python3.exe' -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'C:\\Users\\brichard\\AppData\\Local\\Temp\\pip-install-335sm5vb\\gdal_a7341d33252a43858398c96c78e3ffa9\\setup.py'"'"'; __file__='"'"'C:\\Users\\brichard\\AppData\\Local\\Temp\\pip-install-335sm5vb\\gdal_a7341d33252a43858398c96c78e3ffa9\\setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file ...
    – Basile
    Commented Apr 7, 2021 at 14:32
3

I came across this question in my own quest to install geopandas for qgis and I figured this might be useful for future reference: I am using an osgeo4w installation of qgis 3.16 (it may be available in previous versions but I haven't checked) and geopandas 0.7.0 is now available through the OSGeo4w setup. This probably won't be the solution for everyone but I figured I would add it as an option, especially since using wheels isn't recommended for use with an OSGeo4w installation.

1

If you are getting the SSL-Error, go to OSGeo-Shell, type in py3_env and check whether C:\OSGeo4W64\apps\Python37\lib is in your path. If not then use the set command as in the result of py3_env to add it. For me it wouldn't work otherwise.

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.