anaconda / anaconda-project

Tool for encapsulating, running, and reproducing data science projects
https://anaconda-project.readthedocs.io/en/latest/
Other
217 stars 88 forks source link

[BUG] GDAL and PROJ env vars not updated when set in parent conda env #349

Open kyleredilla opened 2 years ago

kyleredilla commented 2 years ago

Version info

anaconda-project: 0.10.1 conda: 4.10.3

I'm dealing with two variables which are set in the parent conda env which are not updated by anaconda-project run that I've noticed so far, and I wonder if perhaps they should be? They are PROJ_LIB, and GDAL_DATA.

This can cause issues with packages that depend on this happening, but setting them manually based on the anaconda-project env seems to fix the issues. Here is an example with PROJ, where the PROJ_LIB var is not updated:

# empty folder, activate conda env that has GDAL and PROJ installed
$ pwd
/Users/me/scratch/test-ap
$ conda activate
(base) $ echo $PROJ_LIB
/Users/me/opt/anaconda3/share/proj
(base) $ anaconda-project init
Project configuration is in /Users/me/scratch/test-ap/anaconda-project.yml
(base) $ anaconda-project add-packages pyproj
(base) $ anaconda-project run python

>>> import pyproj
/Users/me/scratch/test-ap/envs/default/lib/python3.10/site-packages/pyproj/__init__.py:89: UserWarning: pyproj unable to set database path.
  _pyproj_global_context_initialize()
>>> pyproj.crs.CRS(3338)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/me/scratch/test-ap/envs/default/lib/python3.10/site-packages/pyproj/crs/crs.py", line 326, in __init__
    self._local.crs = _CRS(self.srs)
  File "pyproj/_crs.pyx", line 2352, in pyproj._crs._CRS.__init__
pyproj.exceptions.CRSError: Invalid projection: epsg:3338: (Internal Proj Error: proj_create: no database context specified)
>>> import os
>>> os.getenv("PROJ_LIB")
/Users/me/opt/anaconda3/share/proj
>>> exit()

# restart to set env var before import
(base) $ anaconda-project run python

>>> os.environ["PROJ_LIB"] = os.environ["CONDA_DEFAULT_ENV"] + "/share/proj"
>>> import pyproj
>>> pyproj.crs.CRS(3338)
<Derived Projected CRS: EPSG:3338>
Name: NAD83 / Alaska Albers
Axis Info [cartesian]:
- X[east]: Easting (metre)
- Y[north]: Northing (metre)
Area of Use:
- name: United States (USA) - Alaska.
- bounds: (172.42, 51.3, -129.99, 71.4)
Coordinate Operation:
- name: Alaska Albers (meters)
- method: Albers Equal Area
Datum: North American Datum 1983
- Ellipsoid: GRS 1980
- Prime Meridian: Greenwich

# ^^ no warnings or errors

Thanks! Loving the project so far! Happy to try contributing if this is indeed something to fix.

AlbertDeFusco commented 2 years ago

Hi! Thanks for the issue. At first glance it appears that we're hitting the case where the env vars are set during conda activate

https://github.com/conda-forge/proj.4-feedstock/blob/master/recipe/scripts/activate.sh

Unfortunately, anaconda-project does not call conda activate when running a command. See the end of PR #286

It seems like a good time to revisit the command run procedure.