Using Virtualenv Environment and Conda Environment
The assignments of this course will require the students to use several libraries that have different APIs between versions. A managed Python environment is a simple and effective way to keep the dependencies of different projects separately by creating environments for each of them.
There are different ways to create and manage virtual environments, we will introduce two main ways here:
Virtualenv
We can install virtualenv using
pip
, check
pip
version first by using
pip —version
to make sure it’s installed.
If no version version shows up, install
pip
by following the
link here.
With
pip
installed, we can now install virtualenv using commands below:
python3 -m pip install —user virtualenv #Linux, macOS
py -m pip install —user virtualenv #Windows
To create a virtual environment and name it envname (or whatever you prefer):
python3 -m venv envname #Linux, macOS
py -m venv envname #Windows
To use your environment, we need to activate it first:
source envname/bin/activate #Linux, macOS
.\envname\Scripts\activate #Windows
(Check the location of your python interpreter if you are not sure about the path, it should point to the
envname
directory.)
After it is activated, we can now install packages using
pip install some_package
within this environment.
To install entire environments with respective
requirements.txt
files, create a new virtualenv environment and run
pip install -r requirements.txt
.
After finish using the environment, call
deactivate
to leave it.
To learn more about the use of virtualenv, check
here.
Conda
Using conda to manage the environments requires us to install Miniconda (or Anaconda) first.
Anaconda comes with a lot of data science packages and is intended to use as a pre-installed environment, while Miniconda only comes with the minimal Python environment and the conda tool.
Installing Miniconda is simple, you can just download the right package for your OS and follow the instructions
here.
To create a virtual environment, use
conda create -n envname
# if you want to pass a specific python version i.e. 3.6
conda create -n envname python=3.6
To activate the environment, use
conda activate envname #Linux, macOS
activate envname #Windows
Then similarly you can use
pip install some_package
or
conda install some_package
to install your packages. However, we would recommend you to use the conda tool to install all of your packages.
To install entire environments with respective
environment.yml
files, run
conda env create -f environment.yml
. This will create a new conda environment with specifications of the
environment.yml
file. To change the name of the environment, modify the first line of the
environment.yml
file before you install it.
After finish using the environment, call
conda deactivate
to leave it.
For more information using conda, check conda's documentation
here.