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

Basemap can't import when used with a virtual environment #72

Closed
TD22057 opened this issue Dec 21, 2020 · 4 comments
Closed

Basemap can't import when used with a virtual environment #72

TD22057 opened this issue Dec 21, 2020 · 4 comments

Comments

@TD22057
Copy link

TD22057 commented Dec 21, 2020

Original issue reported here: matplotlib/basemap#489 (comment) w/ link to the patch that this repo is applying. Original text reproduced here:

I have a conda environment with MPL + basemap installed in it. I'm using a virtual environment built from that conda environment to install other software. When python is run from the venv, basemap fails to import because it uses sys.prefix to find the share directory which isn't correct. In this case, sys.prefix is the venv, not the base system where basemap is installed.

The problem is this line in init.py:
basemap_datadir = os.path.join(sys.prefix, 'share', 'basemap')

IMO, the correct way to do this is to find the site packages directories and search for the share dir relative to both of them. /opt/base is a conda env w/ python and mpl+basemap in it. /home/user/venv is the virtual env.

>>> site.getsitepackages()
['/home/user/venv/lib/python3.7/site-packages', 
  '/opt/base/lib/python3.7/site-packages']
@TD22057
Copy link
Author

TD22057 commented Dec 21, 2020

In case that wasn't clear enough, here is basically what I'm doing. Start with a conda env that has MPL and basemap installed in /opt/base. Then create a venv.

/opt/base/bin/python -m venv --system-site-packages venv_local
./venv_local/bin/python
[GCC 7.3.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import mpl_toolkits.basemap
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/opt/base/lib/python3.7/site-packages/mpl_toolkits/basemap/__init__.py", line 161, in <module>
    epsgf = open(os.path.join(basemap_datadir, 'epsg'))
FileNotFoundError: [Errno 2] No such file or directory: '/home/user/venv_local/share/basemap/epsg'

@ocefpaf
Copy link
Member

ocefpaf commented Dec 22, 2020

I don't think conda supports/activates for venvs. Why not a conda env?

Closing this b/c I'm pretty sure that use case is not supported and the alternative is to just use a conda environment instead.

@ocefpaf ocefpaf closed this as completed Dec 22, 2020
@TD22057
Copy link
Author

TD22057 commented Dec 22, 2020

I can't disagree more strongly. venv's are an integral part of python and using conda to provide different versions of python is very standard practice. I have hundreds of users that work this way during development, testing, and deployment. The base conda env we use is over 2.5 GB and cloning that a) takes for ever, and b) uses tons and tons of disk space (hard linking doesn't work - usage is across multiple NFS mounted drives). That's the whole point of a venv - light weight and fast.

The fix for this is not that complicated and won't break anything with regard to conda. If I you don't mind, I can make PR for you to review.

@ocefpaf
Copy link
Member

ocefpaf commented Dec 22, 2020

Then please open an issue in conda and ask for venv support there, there is nothing we can do here for you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants