In application development, each project requires a different environment (Python version, required libraries, etc.). With Anaconda, a package management system, you can build a virtual environment for each project and easily move back and forth between multiple independent environments on a single local machine. In this article, I will provide how to build a virtual environment (hereinafter referred to as “conda environment”) with Anaconda.
Prerequisite environment
- windows 10 (64bit)
- anaconda 4.8.2
Create a virtual environment
On the Terminal, create a conda environment named myenv using the following command.
conda create -n myenv
When you create the conda environment, you can install python and specify the version along with it. Libraries such as pandas can also be installed together, and if the version is not specified, the latest version will be installed as far as the dependencies allow.
conda create -n myenv python=3.6 pandas
You can also create a conda environment by importing a yaml file that contains information about the environment you want to build. On the Terminal, output the above myenv information to a yaml file named myenv.yml using the following command.
conda env export > myenv.yml
And then you will see that it contains information about the installation of python3.6, pandas and its dependencies.
name: myenv
channels:
- defaults
dependencies:
- blas=1.0=mkl
- certifi=2021.5.30=py36haa95532_0
- intel-openmp=2021.3.0=haa95532_3372
- mkl=2020.2=256
- mkl-service=2.3.0=py36h196d8e1_0
- mkl_fft=1.3.0=py36h46781fe_0
- mkl_random=1.1.1=py36h47e9c7a_0
- numpy=1.19.2=py36hadc3359_0
- numpy-base=1.19.2=py36ha3acd2a_0
- pandas=1.1.5=py36hd77b12b_0
- pip=21.2.2=py36haa95532_0
- python=3.6.13=h3758d61_0
- python-dateutil=2.8.2=pyhd3eb1b0_0
- pytz=2021.1=pyhd3eb1b0_0
- setuptools=52.0.0=py36haa95532_0
- six=1.16.0=pyhd3eb1b0_0
- sqlite=3.36.0=h2bbff1b_0
- vc=14.2=h21ff451_1
- vs2015_runtime=14.27.29016=h5e58377_2
- wheel=0.36.2=pyhd3eb1b0_0
- wincertstore=0.2=py36h7fe50ca_0
prefix: C:Users*****Anaconda3envsmyenv
Create another conda environment with the name myenv2 using this yml file.
conda env create -n myenv2 -f myenv.yml
If you want your project members to build the same environment, you can have them duplicate the virtual environment by distributing the yaml file in this way.
Confirm the built environment
You can use the following command to check the list of conda environments created so far.
conda info -e
The output looks like this, and the environment you are currently using will be indicated by *.
# conda environments:
#
base * C:Users*****Anaconda3
myenv C:Users*****Anaconda3envsmyenv
myenv2 C:Users*****Anaconda3envsmyenv2
Move to the virtual environment you built with the following command.
conda activate myenv
If you run “conda info -e” again, you will see that * is attached to myenv.
# conda environments:
#
base C:Users*****Anaconda3
myenv * C:Users*****Anaconda3envsmyenv
myenv2 C:Users*****Anaconda3envsmyenv2
After moving to the conda environment you want to check, you can run the following command to check the installed package and version.
conda list
Remove unnecessary virtual environments
You can delete the conda environment that is no longer needed. Since it is not possible to delete the environment while you are still in the environment to be deleted, use the following command to return to the base environment.
conda deactivate
Next, delete the myenv environment using the following command.
conda env remove -n myenv
“conda info -e” command confirms that myenv has been removed.
# conda environments:
#
base * C:Users*****Anaconda3
myenv2 C:Users*****Anaconda3envsmyenv2
If you want to know how to use the conda environment in JupyterLab, check out this article.
コメント