10 steps to using Virtual Environments with pip
Creating, Activating and De-activating virtual environments
Virtual environments were always the bane of my existence (since I use pip instead of conda). After a lot of research (erm…googling), I finally found a series of processes that worked for me - and I documented it to help others and remind myself.
Why do you need virtual environments?
For instance, say you are writing a program that needs various packages and their sometimes numerous dependencies. To make sure that you do not run into versioning issues, it might be cleaner to create a virtual environment where all the packages required to run the program or project are static and do not change. I had to do this when I started running Deep Learning projects since TensorFlow for instance would only work on specific versions of Python.
I found the links below very useful in understanding virtual environments:
Installing virtual environments using pip
Virtual Environments Tutorial
The following steps below are what I use to create virtual environments using pip.
Step 0: To create a virtual environment, type this in your Windows Command line:
pip3 install virtualenv
Note that the command above will create the virtual environment with the Python version on your local computer.
To install multiple versions of Python on your computer, you need pyenv. I found a really helpful blog and video by k0nze that gives a clear step-by-step of the entire process.
- Navigate to the project folder where you want the virtual environment to live using
cd
in the Command line. For example, to create a folder called New Folder:
cd C:\Users\ibobu\OneDrive\New Folder
- Create the virtual environment:
py -m venv env
Note that env is the name of the new environment and can be named anything — for example:py -m venv deeplearning
. - Activate the environment:
.\env\Scripts\activate
Again, note that env is the name of the new environment. - Do some basic checks:
- There should be a folder in your project folder called env
- Go to the new environment:
cd env
- Check for Python install:
where python
- Go to the new environment:
- There should be a folder in your project folder called env
- Install the required packages for your project using the following command:
pip install -r requirements.txt
Using a requirements file is the most efficient way of installing packages. See an example of a requirements file
Since Python is already installed, your requirements file should not contain Python - that gave me a lot of errors at first.
I also got an error when pip tried to install TensorFlow (I was doing a Deep Learning project when I wrote this post). The error was: Could not find a version that satisfies the requirement tensorflow==2.3.0
To fix the error, I upgraded pip by running:pip install --upgrade pip
and thenpip install tensorflow
. That seemed to fix things. Also, check the TensorFlow official website to find what versions of Python are compatible with the latest release of TensorFlow. At this time, TensorFlow was not compatible with Python 3.11 (so I had to downgrade to 3.7). - To show all packages installed in your virtual environment:
pip list
orpip freeze
- Create a requirements file using:
pip freeze > requirements.txt
- To leave the virtual environment:
deactivate
- To use an existing virtual environment:
- Navigate to the folder where the virtual environment was created in your Windows terminal:
cd path-to-directory
- Activate the environment using:
.\env_name\Scripts\activate
- Navigate to the folder where the virtual environment was created in your Windows terminal:
- You are now a virtual environment expert :) Well, at the very least, you are as familiar with the basics as I am!
Good luck!