Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

KeyError 'PROJ_LIB' #419

Closed
statiksof opened this issue Sep 3, 2018 · 32 comments
Closed

KeyError 'PROJ_LIB' #419

statiksof opened this issue Sep 3, 2018 · 32 comments

Comments

@statiksof
Copy link

I install basemap with the command
conda install basemap -c conda-forge

but

from mpl_toolkits.basemap import Basemap

returns:

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-23-212c45f90d40> in <module>()
----> 1 from mpl_toolkits.basemap import Basemap
      2 #import matplotlib.pyplot as plt
      3 import numpy as np

/srv/conda/lib/python3.6/site-packages/mpl_toolkits/basemap/__init__.py in <module>()
    144 
    145 # create dictionary that maps epsg codes to Basemap kwargs.
--> 146 pyproj_datadir = os.environ['PROJ_LIB']
    147 epsgf = open(os.path.join(pyproj_datadir,'epsg'))
    148 epsg_dict={}

/srv/conda/lib/python3.6/os.py in __getitem__(self, key)
    667         except KeyError:
    668             # raise KeyError with the original key value
--> 669             raise KeyError(key) from None
    670         return self.decodevalue(value)
    671 

KeyError: 'PROJ_LIB'
@ocefpaf
Copy link
Contributor

ocefpaf commented Sep 3, 2018

Are you installing it on your root environment? If so the env vars won't be activate, you must install it in an env:

conda create --name TEST python=3.6 basemap
conda activate TEST
python -c "from mpl_toolkits.basemap import Basemap"

PS: this is not an issue with basemap but actually how conda packages work.

@fonnesbeck
Copy link

fonnesbeck commented Sep 4, 2018

I can confirm this behavior with a clean install of basemap via conda into a new environment. You can see in the traceback that it refers to a conda environment:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/home/fonnesbeck/anaconda3/envs/dev/lib/python3.6/site-packages/mpl_toolkits/basemap/__init__.py", line 146, in <module>
    pyproj_datadir = os.environ['PROJ_LIB']
  File "/home/fonnesbeck/anaconda3/envs/dev/lib/python3.6/os.py", line 669, in __getitem__
    raise KeyError(key) from None
KeyError: 'PROJ_LIB'

@statiksof
Copy link
Author

Thanks @ocefpaf @fonnesbeck
Downgrading to an old version of Basemap (ex: 1.0.7) works perhaps.

@ocefpaf I am installing it in docker container, so myabe when I updated the image, the new version of Basemap is installed.

@ocefpaf
Copy link
Contributor

ocefpaf commented Sep 4, 2018

These are the versions I have in my env and they all work as expected.

> conda list | grep basemap
basemap                   1.1.0            py36h50ae964_6    conda-forge
proj4                     4.9.3                h470a237_8    conda-forge
pyproj                    1.9.5.1          py36h508ed2a_3    conda-forge

Please check the proj4 version and if the PROJ_LIB var is activated.

PS: again, this is not a basemap issue. Please open an issue at https://github.com/conda-forge/basemap-feedstock if you are still experiencing this.

@statiksof
Copy link
Author

I opened a new issue conda-forge/basemap-feedstock#30

Unfortunately, I have the same versions as @ocefpaf

@rstuckey
Copy link

rstuckey commented Sep 6, 2018

It seems the environment variable PROJ_LIB is not being set in the environment.

For python/ipython in a terminal, I managed to rectify by updating proj4:

$ conda create --name test --channel conda-forge python=3.6 basemap
$ conda activate test
(test) $ python -c "from mpl_toolkits.basemap import Basemap"

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/opt/anaconda3/envs/test/lib/python3.6/site-packages/mpl_toolkits/basemap/__init__.py", line 147, in <module>
    epsgf = open(os.path.join(pyproj_datadir,'epsg'))
FileNotFoundError: [Errno 2] No such file or directory: 'PROJ_LIB/epsg'

(test) $ conda list proj
# packages in environment at /opt/anaconda3/envs/test:
#
# Name                    Version                   Build  Channel
proj4                     4.9.3                h470a237_8    conda-forge
pyproj                    1.9.5.1          py36h508ed2a_3    conda-forge

(test) $ conda list basemap
# packages in environment at /opt/anaconda3/envs/test:
#
# Name                    Version                   Build  Channel
basemap                   1.1.0            py36h50ae964_6    conda-forge
basemap-data-hires        1.1.0                         0    conda-forge

(test) $ env | grep -i proj

(test) $ conda update --channel conda-forge proj4

(test) $ conda list proj
# packages in environment at /opt/anaconda3/envs/test:
#
# Name                    Version                   Build  Channel
proj4                     5.1.0                h470a237_2    conda-forge
pyproj                    1.9.5.1                  py36_0    conda-forge

(test) $ conda list basemap
# packages in environment at /opt/anaconda3/envs/test:
#
# Name                    Version                   Build  Channel
basemap                   1.1.0            py36h50ae964_6    conda-forge

(test) $ python -c "from mpl_toolkits.basemap import Basemap"

(test) $ env | grep -i proj
_CONDA_SET_PROJ_LIB=PROJ_LIB
PROJ_LIB=/opt/anaconda3/envs/test/share/proj

However, the problem persisted inside Jupyter. I think they may have fixed it recently. I haven't been able to test that pull yet.

In the meantime, a quick workaround (in your Jupyter kernel), for me at least, was to add the variable explicitly in my kernel.json file:

...
 "env": {
  "PROJ_LIB": "/opt/anaconda3/envs/test/share/proj",
...

Nonetheless, I'm not sure if the assumption of the presence of the environment variable in basemap/__init__.py:

pyproj_datadir = os.environ['PROJ_LIB']

should be there. It didn't appear to be in the previous version (1.0.7).

@QuLogic
Copy link
Member

QuLogic commented Sep 6, 2018

PROJ_LIB does not appear anywhere in Basemap. This is a downstream change in the conda-forge package that should be discussed there. So I'm closing this issue since it is not Basemap related.

@ocefpaf
Copy link
Contributor

ocefpaf commented Sep 6, 2018

This is not a basemap issue. It is a conda usage question, please read:

conda-forge/basemap-feedstock#30

@rstuckey
Copy link

rstuckey commented Sep 7, 2018

@QuLogic, @ocefpaf, thanks for clearing that up. I'll continue over there.

@MultEquilibria
Copy link

I installed basemap in a virtual env - from mpl_toolkits.basemap import Basemap doesn't give any errors but when I try to run m = Basemap(projection='cyl', llcrnrlat=south, urcrnrlat=north, llcrnrlon=west, urcrnrlon=east, lat_ts=south, resolution = "h") it gives the following error
screen shot 2018-09-12 at 19 12 31
screen shot 2018-09-12 at 19 12 40

@ocefpaf
Copy link
Contributor

ocefpaf commented Sep 12, 2018

@MultEquilibria please do not send images b/c we cannot search/copy/paste, please post text only tracebacks. With that said did you try doing what the error message tells you to do?

@MultEquilibria
Copy link

and that is what i get when I tried to install basemap-data-hires

(basemap_env) AMAC02TD1P7GTDX:~ petr.babin$ conda install -c conda-forge basemap-data-hires
Solving environment: done

Package Plan

environment location: /Users/petr.babin/anaconda/envs/basemap_env

added / updated specs:
- basemap-data-hires

The following NEW packages will be INSTALLED:

basemap-data-hires: 1.1.0-0 conda-forge

Proceed ([y]/n)? y

Preparing transaction: failed

>>>>>>>>>>>>>>>>>>>>>> ERROR REPORT <<<<<<<<<<<<<<<<<<<<<<

Traceback (most recent call last):
  File "/Users/petr.babin/anaconda/lib/python3.6/site-packages/conda/exceptions.py", line 819, in __call__
    return func(*args, **kwargs)
  File "/Users/petr.babin/anaconda/lib/python3.6/site-packages/conda/cli/main.py", line 78, in _main
    exit_code = do_call(args, p)
  File "/Users/petr.babin/anaconda/lib/python3.6/site-packages/conda/cli/conda_argparse.py", line 77, in do_call
    exit_code = getattr(module, func_name)(args, parser)
  File "/Users/petr.babin/anaconda/lib/python3.6/site-packages/conda/cli/main_install.py", line 11, in execute
    install(args, parser, 'install')
  File "/Users/petr.babin/anaconda/lib/python3.6/site-packages/conda/cli/install.py", line 253, in install
    handle_txn(unlink_link_transaction, prefix, args, newenv)
  File "/Users/petr.babin/anaconda/lib/python3.6/site-packages/conda/cli/install.py", line 282, in handle_txn
    unlink_link_transaction.execute()
  File "/Users/petr.babin/anaconda/lib/python3.6/site-packages/conda/core/link.py", line 223, in execute
    self.verify()
  File "/Users/petr.babin/anaconda/lib/python3.6/site-packages/conda/common/io.py", line 46, in decorated
    return f(*args, **kwds)
  File "/Users/petr.babin/anaconda/lib/python3.6/site-packages/conda/core/link.py", line 200, in verify
    self.prepare()
  File "/Users/petr.babin/anaconda/lib/python3.6/site-packages/conda/core/link.py", line 192, in prepare
    stp.remove_specs, stp.update_specs)
  File "/Users/petr.babin/anaconda/lib/python3.6/site-packages/conda/core/link.py", line 282, in _prepare
    mkdir_p(transaction_context['temp_dir'])
  File "/Users/petr.babin/anaconda/lib/python3.6/site-packages/conda/gateways/disk/__init__.py", line 60, in mkdir_p
    makedirs(path)
  File "/Users/petr.babin/anaconda/lib/python3.6/os.py", line 220, in makedirs
    mkdir(name, mode)
PermissionError: [Errno 13] Permission denied: '/Users/petr.babin/anaconda/envs/basemap_env/.condatmp'

$ /Users/petr.babin/anaconda/bin/conda install -c conda-forge basemap-data-hires

environment variables:
CIO_TEST=
CONDA_DEFAULT_ENV=basemap_env
CONDA_EXE=/Users/petr.babin/anaconda/bin/conda
CONDA_PREFIX=/Users/petr.babin/anaconda/envs/basemap_env
CONDA_PROMPT_MODIFIER=(basemap_env)
CONDA_PYTHON_EXE=/Users/petr.babin/anaconda/bin/python
CONDA_ROOT=/Users/petr.babin/anaconda
CONDA_SHLVL=1
PATH=/Users/petr.babin/anaconda/envs/basemap_env/bin:/Users/petr.babin/anac
onda/bin:/Users/petr.babin/anaconda/bin:/Users/petr.babin/anaconda/bin
:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
REQUESTS_CA_BUNDLE=
SSL_CERT_FILE=

 active environment : basemap_env
active env location : /Users/petr.babin/anaconda/envs/basemap_env
        shell level : 1
   user config file : /Users/petr.babin/.condarc

populated config files : /Users/petr.babin/.condarc
conda version : 4.5.11
conda-build version : not installed
python version : 3.6.6.final.0
base environment : /Users/petr.babin/anaconda (writable)
channel URLs : https://conda.anaconda.org/conda-forge/osx-64
https://conda.anaconda.org/conda-forge/noarch
https://repo.anaconda.com/pkgs/main/osx-64
https://repo.anaconda.com/pkgs/main/noarch
https://repo.anaconda.com/pkgs/free/osx-64
https://repo.anaconda.com/pkgs/free/noarch
https://repo.anaconda.com/pkgs/r/osx-64
https://repo.anaconda.com/pkgs/r/noarch
https://repo.anaconda.com/pkgs/pro/osx-64
https://repo.anaconda.com/pkgs/pro/noarch
package cache : /Users/petr.babin/anaconda/pkgs
/Users/petr.babin/.conda/pkgs
envs directories : /Users/petr.babin/anaconda/envs
/Users/petr.babin/.conda/envs
platform : osx-64
user-agent : conda/4.5.11 requests/2.19.1 CPython/3.6.6 Darwin/17.7.0 OSX/10.13.6
UID:GID : 1610892323:562225435
netrc file : None
offline mode : False

An unexpected error has occurred. Conda has prepared the above report.
If submitted, this report will be used by core maintainers to improve
future releases of conda.
Would you like conda to send this report to the core maintainers?
[y/N]: y
Upload successful.

Thank you for helping to improve conda.
Opt-in to always sending reports (and not see this message again)
by running

$ conda config --set report_errors true

@WeatherGod
Copy link
Member

WeatherGod commented Sep 12, 2018 via email

@ocefpaf
Copy link
Contributor

ocefpaf commented Sep 12, 2018

Your issue in #419 (comment) looks like a common conda issue on Windows. I've seen other users reporting similar problems but that has nothing to do with basemap or the conda-forge package. Please ask at the conda mailing list or post an issue in their issue tracker.

@WeatherGod
Copy link
Member

WeatherGod commented Sep 12, 2018 via email

@WeatherGod
Copy link
Member

WeatherGod commented Sep 12, 2018 via email

@ocefpaf
Copy link
Contributor

ocefpaf commented Sep 12, 2018

The filename paths lead me to believe this is a Mac system.

Interesting, I've only saw issues like that on Windows.

@MultEquilibria I recommend you to open an issue with AnacondaInc.

@MultEquilibria
Copy link

Yes, it's Mac Sierra 10.13.6

I used
conda create --name basemap_env basemap

but i tried other ways - installing from conda-forge didn't work for me because of

PermissionError(13, 'Permission denied')
so I tried alternatively

sudo conda create --name New --channel conda-forge basemap basemap-data-hires
the installation went well but the same error as before when I try to set the resolution as high

@MultEquilibria
Copy link

MultEquilibria commented Sep 12, 2018

The error is persistent in the alternative case of New environment set up with sudo conda create --name New --channel conda-forge basemap basemap-data-hires

when I try to add basemap-data-hires package it says that all the packages are already installed..

AMAC02TD1P7GTDX:~ petr.babin$ source activate New
(New) AMAC02TD1P7GTDX:~ petr.babin$ conda install -c conda-forge basemap-data-hires
Solving environment: done

All requested packages already installed.

(New) AMAC02TD1P7GTDX:~ petr.babin$

@ocefpaf
Copy link
Contributor

ocefpaf commented Sep 12, 2018

@MultEquilibria sudo is highly discourage when using conda. Its main advantage is to install package in the user space. I recommend you to use miniconda and start over without ever using sudo, installing everything in the user space.

@MultEquilibria
Copy link

@MultEquilibria sudo is highly discourage when using conda. Its main advantage is to install package in the user space. I recommend you to use miniconda and start over without ever using sudo, installing everything in the user space.

I realized it already but I'm so reluctant to reinstall Conda in the user space because I would like to avoid reinstalling all the necessary packages again. This issue killed my day today - default basemap doesn't work properly, when fetched from conda-forge doesn't work in a root environment, could not set it up in a virtual environment because of restrictions, when using sudo it either installs it back into a root environment or messes it up [and on top of that somewhere along the way the issues with channels - not finding conda-forge when installing into virtual environment]

@MultEquilibria
Copy link

@MultEquilibria sudo is highly discourage when using conda. Its main advantage is to install package in the user space. I recommend you to use miniconda and start over without ever using sudo, installing everything in the user space.

Thank you for the advice, may be it's what I should do - to reinstall Anaconda properly

@WeatherGod
Copy link
Member

WeatherGod commented Sep 12, 2018 via email

@MultEquilibria
Copy link

just a quick semantic point. You say "virtual environment", but I think you mean "conda environment". These are actually two different things, as "virtual environment" predates "conda environments", and are created by an entirely different tool (pyvenv). These two terms are not interchangable.

thank you, you are right of cause

@jaecheverrip
Copy link

this was the only way to resolve the KeyError 'PROJ_LIB', using this : https://gist.github.com/junzis/36fee36acd7cadfdf1c01c06d97f4947

@e-velin
Copy link

e-velin commented Nov 7, 2018

what helped me was to uninstall anaconda and to install basemap with pip from here: https://www.lfd.uci.edu/~gohlke/pythonlibs/#basemap
I know it's far from an optimal solution but it worked for me

@rakibulhassan08
Copy link

Still have the problem with the "PROJ_LIB"

File "C:\Users\ITS\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 108, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)

File "D:/Rakibul/Dropbox_Rakibul personal/Dropbox/Codes_Rakibul/Python codes/Basemapplot_lat_long/basemap.py", line 1, in
from mpl_toolkits.basemap import Basemap

File "C:\Users\ITS\Anaconda3\lib\site-packages\mpl_toolkits\basemap_init_.py", line 155, in
pyproj_datadir = os.environ['PROJ_LIB']

File "C:\Users\ITS\Anaconda3\lib\os.py", line 678, in getitem
raise KeyError(key) from None

KeyError: 'PROJ_LIB'

@ghost
Copy link

ghost commented Dec 30, 2018

Hi there,
I also have this issue.
Traceback (most recent call last): File "C:/Users/KW/PycharmProjects/Durchschnittsabweichung/simple map.py", line 1, in <module> from mpl_toolkits.basemap import Basemap File "C:\Program Files\Anaconda3\lib\site-packages\mpl_toolkits\basemap\__init__.py", line 155, in <module> pyproj_datadir = os.environ['PROJ_LIB'] File "C:\Program Files\Anaconda3\lib\os.py", line 678, in __getitem__ raise KeyError(key) from None KeyError: 'PROJ_LIB'

I already tried updating conda, using
conda update --prefix C:\Programme\Anaconda3 anaconda

I was just working on the tutorial on how to use basemap for plotting a simple map in PyCharm Community Edition.
interpreter is set to Program Files\Anaconda3\python.exe and basmap is installed in Version 1.2.
Almost forgot: I am using windows 10
conda info:
active environment : None user config file : C:\Users\KW\.condarc populated config files : C:\Users\KW\.condarc conda version : 4.5.12 conda-build version : 3.15.1 python version : 3.7.0.final.0 base environment : C:\Program Files\Anaconda3 (read only) channel URLs : https://conda.anaconda.org/conda-forge/win-64 https://conda.anaconda.org/conda-forge/noarch https://repo.anaconda.com/pkgs/main/win-64 https://repo.anaconda.com/pkgs/main/noarch https://repo.anaconda.com/pkgs/free/win-64 https://repo.anaconda.com/pkgs/free/noarch https://repo.anaconda.com/pkgs/r/win-64 https://repo.anaconda.com/pkgs/r/noarch https://repo.anaconda.com/pkgs/pro/win-64 https://repo.anaconda.com/pkgs/pro/noarch https://repo.anaconda.com/pkgs/msys2/win-64 https://repo.anaconda.com/pkgs/msys2/noarch package cache : C:\Program Files\Anaconda3\pkgs C:\Users\KW\AppData\Local\conda\conda\pkgs envs directories : C:\Users\KW\AppData\Local\conda\conda\envs C:\Program Files\Anaconda3\envs C:\Users\KW\.conda\envs platform : win-64 user-agent : conda/4.5.12 requests/2.19.1 CPython/3.7.0 Windows/10 Windows/10.0.17134 administrator : True netrc file : None offline mode : False

I hope for help!
thanks in advance

@shalevale
Copy link

shalevale commented Jan 28, 2019

Ok. I was getting the same problem.
Finally, I resolved.

You must insert the path where you have "EPSG" in this code:
import os
os.environ['PROJ_LIB'] = 'C:/Users/shale/Miniconda3/Library/share'

I really hope that all you can resolve this problem.
Good luck.

Others accounts reported that the file was in this route:
os.environ['PROJ_LIB'] = r'c:\Users\username\AppData\Local\conda\conda\envs\envname\Library\share'

os.environ['PROJ_LIB'] = '<path_to_anaconda>/share/proj'

os.environ['PROJ_LIB'] = r'C:\Users(xxxx)\AppData\Local\Continuum\anaconda3\pkgs\proj4-5.1.0-hfa6e2cd_1\Library\share'

@shalevale
Copy link

Hi there,
I also have this issue.
Traceback (most recent call last): File "C:/Users/KW/PycharmProjects/Durchschnittsabweichung/simple map.py", line 1, in <module> from mpl_toolkits.basemap import Basemap File "C:\Program Files\Anaconda3\lib\site-packages\mpl_toolkits\basemap\__init__.py", line 155, in <module> pyproj_datadir = os.environ['PROJ_LIB'] File "C:\Program Files\Anaconda3\lib\os.py", line 678, in __getitem__ raise KeyError(key) from None KeyError: 'PROJ_LIB'

I already tried updating conda, using
conda update --prefix C:\Programme\Anaconda3 anaconda

I was just working on the tutorial on how to use basemap for plotting a simple map in PyCharm Community Edition.
interpreter is set to Program Files\Anaconda3\python.exe and basmap is installed in Version 1.2.
Almost forgot: I am using windows 10
conda info:
active environment : None user config file : C:\Users\KW\.condarc populated config files : C:\Users\KW\.condarc conda version : 4.5.12 conda-build version : 3.15.1 python version : 3.7.0.final.0 base environment : C:\Program Files\Anaconda3 (read only) channel URLs : https://conda.anaconda.org/conda-forge/win-64 https://conda.anaconda.org/conda-forge/noarch https://repo.anaconda.com/pkgs/main/win-64 https://repo.anaconda.com/pkgs/main/noarch https://repo.anaconda.com/pkgs/free/win-64 https://repo.anaconda.com/pkgs/free/noarch https://repo.anaconda.com/pkgs/r/win-64 https://repo.anaconda.com/pkgs/r/noarch https://repo.anaconda.com/pkgs/pro/win-64 https://repo.anaconda.com/pkgs/pro/noarch https://repo.anaconda.com/pkgs/msys2/win-64 https://repo.anaconda.com/pkgs/msys2/noarch package cache : C:\Program Files\Anaconda3\pkgs C:\Users\KW\AppData\Local\conda\conda\pkgs envs directories : C:\Users\KW\AppData\Local\conda\conda\envs C:\Program Files\Anaconda3\envs C:\Users\KW\.conda\envs platform : win-64 user-agent : conda/4.5.12 requests/2.19.1 CPython/3.7.0 Windows/10 Windows/10.0.17134 administrator : True netrc file : None offline mode : False

I hope for help!
thanks in advance

You must insert the path where you have "EPSG" in this code:
import os
os.environ['PROJ_LIB'] = 'C:/Users/shale/Miniconda3/Library/share'

@AashutoshTrivedi
Copy link

"PROJ_LIB" is an environment variable which keeps the path of directory of proj4. So if you are using anaconda then do open

sudo -H gedit /etc/environment

and write in there

PROJ_LIB="/home//anaconda3/pkgs/proj4-5.2.0-he6710b0_1/share/proj"

proj4-5.2.0-he6710b0_1 - This depends on version of proj4.

To locate above path you can use command
locate proj4
and copy the one which indicates the directory.

@QuLogic
Copy link
Member

QuLogic commented Mar 6, 2019

Please don't do that. You should not be editing global environments to fix user-specific issues. If you want to edit your environment, use a user-specific file, like ~/.bashrc or ~/.bash_profile. Also, you should not run graphical applications as root (sudo gedit).

In any case, this is not a Basemap issue. It is an issue with the conda-forge package. One that is fixed if a) you install into a separate environment instead of the root environment, or b) update to a newer version of conda. If you still have this issue while using conda-forge in an up-to-date environment, then please report it to them here.

I am locking this issue now because it keeps getting more comments with half-solutions, and once again, this is not the correct place for issues with conda-forge packages.

@matplotlib matplotlib locked as resolved and limited conversation to collaborators Mar 6, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests