简体   繁体   中英

How to create a virtual environment in Python using an environment.yaml file?

I have a requirements.yaml file and I want to create a python virtual environment using it. The file looks like this

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

How can I use this file to create a new environment?

this looks like a conda environment (i could be wrong). In this case (if you have conda) you can do the following:

conda env create --name environment_name -f environment.yml

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

If you dont have or want Conda you would need to convert it to a requirements.txt

  - pip:
    - jupyter==1.0.0
    - pandas==1.0.0
    - scikit-learn==0.22.1

will look like that in the requirements.txt:

jupyter==1.0.0
pandas==1.0.0
scikit-learn==0.22.1

and then create and switch to your virtual environment

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

and then do

pip install -r requirements.txt

In case you are using conda your env.yml should look like

name: my_env
channels:
  - defaults
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

and in order to load it you need to run the following from the terminal.

conda env create -f env.yml

Few more suggestions.

  • If you already have your environment, and you are within it, you can export via
conda env export | grep -v "^prefix: " > env.yml
  • Please consider using a conda only environment and not a conda + pip one if it is possible. In your case all packages are available in conda.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM