如何使用 conda 或 virtualenv 在虚拟环境之间共享包? [英] How to share packages between virtual environments using conda or virtualenv?

查看:87
本文介绍了如何使用 conda 或 virtualenv 在虚拟环境之间共享包?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有多个 python 项目,每个项目都有自己的虚拟环境.另外我有一个全局python环境.我想在虚拟/全局环境之间共享相同的包文件,而不需要多次复制它们(如果可能).

<预><代码>环境包---------------------------全球:A B C DVENV_1:A B C EVENV_2:C D F

所以,在这个例子中:

  • 所有环境都将共享 C相同文件.
  • GLOBALVENV_1 还将共享 AB 的文件.
  • GLOBALVENV_2 将为 D 共享文件.
  • 但是由于 EF 不是全局安装的,所以虚拟环境将单独安装这些包.

我尝试将 virtualenv--system-site-packages 标志一起使用,但这只会使 all 全局包在虚拟环境中可访问环境.因此,当我使用 pip freeze 导出我的环境时,它会包含不必要的包.

如何使用选定的软件包创建虚拟环境,以便如果全局安装了软件包,则不会在本地制作该软件包的新文件副本?这是否可以使用 condavirtualenv 或任何其他工具?

我使用的是带有 python 3.6 的 Windows 10.

解决方案

  • 在 pip 中,您可以在 ~/中缓存下载.pip/cache 以便下次不需要再次下载它们,只需在 $HOME/.pip/pip.conf 中添加以下内容:

[全局]download_cache = ~/.pip/cache

  • 在 conda 中,您可以使用通过在共享用户具有读写访问权限的系统上创建目录来共享包缓存.

然后,对于每个具有访问权限的用户,使用以下条目编辑在其主目录中找到的 .condarc 文件,指定该共享目录的完整路径:

pkgs_dirs:-/path/to/shared_directory

<块引用>

Windows - C:\Users\username.condarc

macOS 和 Linux -/home/username/.condarc

通过运行 conda info 验证包缓存.

I have multiple python projects, each with its own virtual environment. Additionally I have a global python environment. I want to share the same package files between virtual/global environments without copying them multiple times (where possible).


Environment        Packages
----------------------------------------
GLOBAL:      A    B    C    D    
VENV_1:      A    B    C         E
VENV_2:                C    D         F

So, in this example:

  • all environments will share the same files for C.
  • GLOBAL and VENV_1 will also share files for A and B.
  • And GLOBAL and VENV_2 will share files for D.
  • But since E and F are not globally installed, then the virtual environments will install those packages individually.

I tried using virtualenv with the --system-site-packages flag, but that just makes all global packages accessible in the virtual environment. So when I export my environment using pip freeze it will contain unnecessary packages.

How can I create a virtual environment with select packages, such that if a package is globally installed, a new copy of files for that package won't be made locally? Is this possible using conda or virtualenv or any other tool?

I am using Windows 10 with python 3.6.

解决方案

  • In pip you can cache downloads in ~/.pip/cache so it won't need to download them again next time by adding the following to $HOME/.pip/pip.conf:

[global]
download_cache = ~/.pip/cache

  • In conda, you can use a shared package cache by creating a directory on your system where the shared users have read and write access.

Then, for each user who will have access, edit the .condarc file found in their home directory with the following entry, specifying the full path to that shared directory:

pkgs_dirs:
    - /path/to/shared_directory

Windows - C:\Users\username.condarc

macOS and Linux - /home/username/.condarc

Verify the package cache by running conda info.

这篇关于如何使用 conda 或 virtualenv 在虚拟环境之间共享包?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆