Virtual Environment in Python
In this article, we will talk about the basic concept of virtual environments in Python and we will answer why we need it and how to use them.
What is Virtual Environment and Why We Need?
Basically, the virtual environment is a tool that helps to create an isolated environment for each project. Imagine you have three projects and each of them requires different packages. In this situation, a virtual environment can be very useful for the dependencies of each project and prevent conflicts among them.
We will discover conda virtual environment tool but here are common virtual environment tools in Python :
- venv
- virtualenv
- pipenv
- conda
Anaconda
If you want to work with conda you should download Anaconda Distribution and add your system path. Anaconda is an open-source distribution of Python and R programming languages for data science and comes with conda. Anaconda contains Jupyter, Qt Console, Spyder, etc.
Building Virtual Environment With Commands
Open the anaconda command prompt and let’s dive into creating a virtual environment.
1 — Check conda version
Type ‘conda -V’ in the terminal and press enter. If conda is successfully installed in your system you should see similar output.
conda -VOutput :
conda 4.13.0
2—Update conda version
Enter the following in the prompt and press enter.
conda update conda
The output should be something like this:
If you need any updates you can press ‘y’.
3— Check the environments in your system
To see a list of all your environments, type the following in the prompt.
conda env list
Output like this:
# conda environments:
#
base * C:\Users\User\anaconda3
myenv C:\Users\User\anaconda3\envs\myenv
new_env C:\Users\User\anaconda3\envs\new_env
4 — Create a new environment
conda create -n new_env
You can type what name you want after -n. In this example, I preferred ‘new_env’.
Proceed ([y]/n)?
To complete proceed press ‘y’ when conda asks you.
5— Activate and deactivate the new environment
To activate a new environment your command should be:
conda activate new_env
If you see ‘(new_env)’ on the left of the main directory path (C:\Users\User>) your activation is completed.
(new_env) C:\Users\User>
Type the following to deactivate your new environment:
conda deactivate
6 — List installed packages in a conda environment
You can list all of your packages installed in your environment with the below command.
conda list
The output will show you all packages with their versions :
# packages in environment at C:\Users\User\anaconda3\envs\new_env:
#
# Name Version Build Channel
blas 1.0 mkl
bzip2 1.0.8 he774522_0
ca-certificates 2022.07.19 haa95532_0
certifi 2022.9.14 py310haa95532_0
intel-openmp 2021.4.0 haa95532_3556
libffi 3.4.2 hd77b12b_4
mkl 2021.4.0 haa95532_640
mkl-service 2.4.0 py310h2bbff1b_0
mkl_fft 1.3.1 py310ha0764ea_0
mkl_random 1.2.2 py310h4ed8f06_0
numpy 1.23.1 py310h6d2d95c_0
numpy-base 1.23.1 py310h206c741_0
openssl 1.1.1q h2bbff1b_0
pip 22.2.2 py310haa95532_0
python 3.10.4 hbb2ffb3_0
setuptools 63.4.1 py310haa95532_0
six 1.16.0 pyhd3eb1b0_1
sqlite 3.39.3 h2bbff1b_0
tk 8.6.12 h2bbff1b_0
tzdata 2022c h04d1e81_0
vc 14.2 h21ff451_1
vs2015_runtime 14.27.29016 h5e58377_2
wheel 0.37.1 pyhd3eb1b0_0
wincertstore 0.2 py310haa95532_2
xz 5.2.6 h8cc25b3_0
zlib 1.2.12 h8cc25b3_3
7 —Installing packages
There are two options for installing a new package. One is to install the latest version and the other is to install a specific version. For instance, you need to install numpy in your environment.
To install numpy latest version type this command in the terminal:
conda install numpy
Install numpy specific version (1.22.0):
conda install numpy=1.22.0
After the installation, you command ‘conda list’ in the prompt and check which version you have in the environment.
conda listOutput:
# Name Version Build Channel
blas 1.0 mkl
bzip2 1.0.8 he774522_0
ca-certificates 2022.07.19 haa95532_0
certifi 2022.9.14 py310haa95532_0
intel-openmp 2021.4.0 haa95532_3556
libffi 3.4.2 hd77b12b_4
mkl 2021.4.0 haa95532_640
mkl-service 2.4.0 py310h2bbff1b_0
mkl_fft 1.3.1 py310ha0764ea_0
mkl_random 1.2.2 py310h4ed8f06_0
numpy 1.23.1 py310h6d2d95c_0
numpy-base 1.23.1 py310h206c741_0
openssl 1.1.1q h2bbff1b_0
pip 22.2.2 py310haa95532_0
python 3.10.4 hbb2ffb3_0
setuptools 63.4.1 py310haa95532_0
six 1.16.0 pyhd3eb1b0_1
sqlite 3.39.3 h2bbff1b_0
tk 8.6.12 h2bbff1b_0
tzdata 2022c h04d1e81_0
vc 14.2 h21ff451_1
vs2015_runtime 14.27.29016 h5e58377_2
wheel 0.37.1 pyhd3eb1b0_0
wincertstore 0.2 py310haa95532_2
xz 5.2.6 h8cc25b3_0
zlib 1.2.12 h8cc25b3_3
If you want to install more than one package you should type all packages with space like this:
conda install numpy pandas matplotlib
Also, you can upgrade old packages and install the latest version with this command:
conda upgrade numpy
8 — Export and Import Environment
You may want to run your environment on another computer or share it with someone at this point you should export your environment.
Export your active environment to a new file:
conda env export > environment.yaml
You will create a file name ‘environment.yaml’. This is a copy of your environment that contains all of your packages and version.
To import or run ‘environment.yaml’ file on the other computers :
conda env create -f environment.yaml
9 — Remove Environment
Today's last command is removing the environment but first, the environment you want to delete should be deactivated. Otherwise, you can not delete it.
To remove environment:
conda env remove -n new_env