0

I run Ubuntu 18.04.5 LTS and use a virtualenv for Python (3.6.9) and use Spyder (3.6) as an IDE. I've set my Python interpreter on Spyder to point to this virtualenv and I've successfully used geopandas before with this setup. But now I am getting an import error when using Spyder.

import geopandas as gpd
Traceback (most recent call last):

  File "<ipython-input-1-a62d01c1d62e>", line 1, in <module>
    import geopandas as gpd

  File "/home/theuser/.virtualenvs/mypython3/lib/python3.6/site-packages/geopandas/__init__.py", line 3, in <module>
    from geopandas.geoseries import GeoSeries  # noqa

  File "/home/theuser/.virtualenvs/mypython3/lib/python3.6/site-packages/geopandas/geoseries.py", line 9, in <module>
    from pyproj import CRS, Transformer

ImportError: cannot import name 'CRS'

If I do the same thing in a terminal (after activating the virtualenv) I get no error. I've double-checked to make sure Spyder is still set to use the virtualenv for the interpreter.

The only thing I can think of that has "changed" between the last time I imported geopandas successfully in Spyder and now is that I have installed qgis using the quickstart instructions found here.

Please let me know if I can clarify anything about my setup or problem.

UPDATE

So I think I determined what was happening. When I install qgis, it installs other packages like pyproj, which have versions that conflict with the ones in my virtualenv. For some reason, when I import geopandas from spyder, it doesn't use pyproj from my virtualenv, but the other one installed with qgis, causing the error. I'm not sure why Spyder and/or geopandas would be importing the pyproj package outside the virtualenv.

For now, I've tried uninstalling the pyproj package installed with qgis, though this may cause problems when I use qgis. If someone has a better suggestion, I'd be open to it and I'd also like to know why when importing geopandas in Spyder it doesn't use the pyproj package in the virtualenv even though it uses the geopandas package from my virtualenv.

UPDATE 2 So Spyder's sys.path is different from the one I get when I use the my virtualenv from terminal. It includes /use/lib/python3/dist-packages, which is where pyproj is installed when I install qgis.

So when I import geopandas, and it imports pyproj it grabs this one. How can I get Spyder to check my virtualenv packages first?

jtorca
  • 1,531
  • 2
  • 17
  • 31
  • I've confirmed it has something to do with `qgis` being installed. After uninstalling `qgis` I no longer get this import error. – jtorca Dec 03 '20 at 22:48
  • Voting to close this question as it fundamentally comes down to how to run spyder with a virtual environment and managing paths. – jtorca Dec 04 '20 at 03:28
  • 1
    (*Spyder maintainer here*) I think the problem is we don't activate virtualenvs (because there's no simple, standard way to do that for Windows, Linux and Mac) and that seems to be required to drop `/usr/lib/python3/dist-packages` from `sys.path`. However, we do activate conda envs, so my recommendation is to use Anaconda instead. – Carlos Cordoba Dec 05 '20 at 22:58

0 Answers0