How to share a virtual environment with pipenv?

耗尽温柔 提交于 2019-12-11 08:53:40

问题


Pipenv virtual environnements (venv) will be shared with children folders. So for example if you have installed an venv in ~/foo/, it will be accessible in ~/foo/baz/

But what if you want to share the same venv between ~/foo/bob/ and ~/baz/alice/ ?

The following kind of worked for me. I hope it can help.


回答1:


To share virutal env with pipenv Create a directory ~/foo/bob/

mkdir -p ~/foo/bob/ ; cd ~/foo/bob/        

create a virtual env in ~/foo/bob/

pipenv --three

This will create ~/.local/share/virtualenvs/bob-signature/

Install whatever packages you need. For example

pipenv install jupyter

This will create a Pipfile.lock in ~/foo/bob/

Create another directory, say ~/baz/alice/ and create a venv there

mkdir -p ~/baz/alice ; cd ~/baz/alice/ ;  pipenv --three 

As before pipenv will have created alice-signature/ in ~/.local/share/virtualenvs/. Remove that folder and replace it by a link to bob-signature

cd ~/.local/share/virtualenvs/
rm -r alice-signature/
ln -s bob-signature/ alice-signature

In ~/baz/alice/, link Pipfile and Pipfile.lock to the ones in ~/baz/bob/

cd ~/baz/alice/ ;
rm Pipfile ; rm Pipfile.lock
ln -s ~/foo/bob/Pipfile . ;  ln -s ~/foo/bob/Pipfile.lock  .  

Now, You should have a venv accessible from alice/ or bob/, and packages installed from any of those directories will be shared.




回答2:


There's a undocumented feature in pipenv: if you create a file named .venv in the project root with a path to a virtualenv, pipenv will use that instead of an autogenerated path.

(You'll still need to keep Pipfile's and Pipfile.locks synchonized. Making them symlinks as @MalikKoné suggests may do, but not if Pipfiles are under version control as they are supposed to.)

This, however, is more fit for cases when you already have an established set of environments that you wish to reuse. Otherwise, placing environments in arbitrary places is prone to create a mess eventually.



来源:https://stackoverflow.com/questions/48772978/how-to-share-a-virtual-environment-with-pipenv

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!