如何使用environment.yaml文件在Python中创建虚拟环境?

Bha*_*ula 6 python python-venv

我有一个requirements.yaml 文件,我想使用它创建一个python 虚拟环境。该文件看起来像这样

dependencies:
  - python=3.7.5
  - pip=19.3.1
  - pip:
    - jupyter==1.0.0
    - pandas==1.0.0
    - scikit-learn==0.22.1
    - numpy==1.18.1
    - matplotlib==3.1.3
    - seaborn==0.10.0
    - black==19.10b0
    - haversine==2.2.0
    - toml==0.10.0
    - nose==1.3.7

Run Code Online (Sandbox Code Playgroud)

如何使用该文件创建新环境?

小智 6

这看起来像一个 conda 环境(我可能是错的)。在这种情况下(如果您有 conda)您可以执行以下操作:

conda env create --name environment_name -f environment.yml
Run Code Online (Sandbox Code Playgroud)

https://docs.conda.io/projects/conda/en/4.6.0/_downloads/52a95608c49671267e40c689e0bc00ca/conda-cheatsheet.pdf

如果您没有或想要 Conda,您需要将其转换为requirements.txt

  - pip:
    - jupyter==1.0.0
    - pandas==1.0.0
    - scikit-learn==0.22.1
Run Code Online (Sandbox Code Playgroud)

看起来像在requirements.txt中:

jupyter==1.0.0
pandas==1.0.0
scikit-learn==0.22.1
Run Code Online (Sandbox Code Playgroud)

然后创建并切换到您的虚拟环境

https://uoa-eresearch.github.io/eresearch-cookbook/recipe/2014/11/26/python-virtual-env/

然后做

pip install -r requirements.txt
Run Code Online (Sandbox Code Playgroud)

  • @mas 你似乎在想我在想什么 - 使用 yaml 文件可能与“venv”和“pip”一起使用。但是阅读 [`virtualenv`](https://virtualenv.pypa.io/en/latest/cli_interface.html#creator) 的文档并且没有找到任何规范/要求文件的提及,我意识到 *virtual 的 `venv`环境*和用于*包安装*的`pip`与官方python的基础设施PoV是两个不同的东西 - 只是`conda`或[`micromamba`](https://mamba.readthedocs.io/en/latest/user_guide/ micromamba.html#specation-files) 方便地同时提供这两种功能。 (2认同)