Solving Basemap PyCharm Error — KeyError: ‘PROJ_LIB’

Basemap PyCharm Error — KeyError: ‘PROJ_LIB’ is an error which occurs in Python when you try to use Basemap to plot a map by PyCharm.

In this article we are going to explain why the error is popping up and show you how to solve the error and get rid of it for good.

Explaining Basemap PyCharm Error — KeyError: ‘PROJ_LIB’

This error is an error which occurs in Python when you try to use Basemap to plot a map by PyCharm.

The error message should look like the error in the example bellow, make sure you have the same error message in order to avoid confusion.

                                                                       #
...
    exec(code_obj, self.user_global_ns, self.user_ns)
    from mpl_toolkits.basemap import Basemap
    module = self._system_import(name, *args, **kwargs)
    pyproj_datadir = os.environ['PROJ_LIB']
    raise KeyError(key) from None
    KeyError: 'PROJ_LIB'
                                                                       #

Bellow I make my best attempt at solving the error and present multiple possible solutions.

Solution 1 : edit the .bashrc file.

The easiest solution to edit the .bashrc file.

You should add the line bellow to your .bashrc file

                                                                       #
export PROJ_LIB=/path/to/your/instalation/of/anaconda/share/proj/
                                                                       #

After this, the package Basemap should work perfectly and Pycharm will print no errors.

Sometimes, you need to downgrade pyproj for this solution to actually work.

Congratulations, in case this fix solved your issue. The following solution is great if this one did not do the job.

Solution 2 : Correctly import Basemap and install proj4

The solution is simple, before importing Basemap Use the following code

                                                                       #
import os
os.environ["PROJ_LIB"] = "C:\\Utilities\\Python\\Anaconda\\Library\\share"; #fixr
from mpl_toolkits.basemap import Basemap
                                                                       #

PROJ_LIB should be set to the location of epsg.

To find where epsg is, open the Anaconda Prompt and run the command bellow

                                                                       #
conda install -c conda-forge proj4
                                                                       #

This solution should be enough to solve the issue, I hope one of the fixes above actually solved your error.

If this article has been useful for your particular case, consider donating to our Kofi account, there is a red button at the top of this page.

Summing-up

This is the end of our article, this is a confusing error but with a small investigation the error could be solved, thank you for reading.

To support us consider donating to our Kofi account above. Keep coding, keep learning and cheers.

If you want to learn more about Python, please check out the Python Documentation : https://docs.python.org/3/