CS 440/ECE 448
Fall 2024
Margaret Fleck
Using a Virtual Environment
What is a virtual environment? Python virtual environment is an isolated environment for Python projects.
Imagine you have two projects ProjectA and ProjectB. For some legacy issues, ProjectA requires Python2 while ProjectB only works in Python3.
In such case, ideally you need separate environments to run these two projects. Python virtual environment makes your switch between these projects smoothly.
Meanwhile, even without conflict between projects, you may not want to contaminate system-level Python environment with your own packages. Since some package version mismatch may crash some of your system processes.
Therefore, it may be a good habit to create a new virtual environment when you start some new research/course projects since you never know what you will need in the future.
Is it difficult to use virtual environment? No, not at all! conda, an OS-agnostic tool, takes care of all cumbersome things for you.
How to use virutal environment? Download and install corresponding miniconda for your OS.
Do not worry about the Python version shown on the webpage if it does not match your needs. That version only demonstrates the Python version miniconda installs as default.
You have all freedom to install any Python version later with conda, which is the reason why we need virtual environment.
I know something called Anaconda, is it the same as miniconda? Anaconda is essentially the same as miniconda. However, Anaconda is much larger (~3GB) than miniconda (~50MB) since Anaconda pre-installs many packages, which you may not need.
Actually, all packages installed in Anaconda could be installed later based on your needs. We recommned using miniconda. More comparison could be found here.
OK, I have installed miniconda, what's next? In this project, we use Python 3.8 or 3.8. No matter whether you
one of these versions installed already, we recommend getting yourself familar with conda.
Create a virtual environment for this MP with the following command:
conda create --name cs440-mp3 python=3.9
Then everytime you want to use the virtual environment, activate it with conda activate cs440-mp3
You can install package with conda install XXX or pip install XXX Either way will install packages only in this cs440-mp3 environment.
When you do not want to use environment anymore, deactivate it with conda deactivate More information could be found here.