Creating Python Virtual Environment on Windows 11
Tags: python, django, backenddevelopment, virtualenv
Published: 2024-01-20 16:01:32

When you work on a Python project, the conventional approach involves globally installing a Python interpreter and creating a file named `filename.py`. Typically, as you advance into more complex Python tasks, you may use `pip` to install packages globally.
 
While this method might not pose a problem initially, it can become a concern as you move into specialized areas such as Data Science, Data Analytics, or Backend development.

Imagine a scenario: you have Project A where you have installed version 1.0 of  a package. Then you switch to Project B and install the same package but version 2.0. Upon returning to Project A, you encounter issues—this is what we call breaking changes.

In the following sections, I will walk you through:
- Creating, activating, and deactivating a virtual environment
- Reproducing a virtual environment
- Installing packages within the virtual environment
- Specifying the Python interpreter when creating the virtual environment 
- Configuring and using a virtual environment in VSCode

Now, let’s address the question.

## What is a virtual environment?
A virtual environment serves as a secluded space to store project dependencies. It is an isolated environment where project dependencies are stored.

Within each virtual environment, there is a specific Python version it runs on and folders of third-party libraries you have installed. Virtual environments make it easy to keep track of project dependencies, particularly useful when working on projects with dependencies.

![Diagramatic representation of a virtual environement](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tzqhnox4qq1soddjxxv5.png)

When you think of a virtual environment, picture the two key components shown in the figure above.


![An illustration of multiple virtual environments](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vsgzjul0d92bhjrrwsz3.png)

The figure above illustrates what you have on your system when you create multiple virtual environments.

## What is a dependency?
A dependency is a code written by other people that we can install and use on our project so we don’t have to rewrite it. However, this practice introduces potential compatibility issues, conflicts, or breaking changes, especially since every package tends to release new versions.

### Semantic Versioning


![A diagram showing semantic versioning](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3jjpyq3053rc4chz3oa8.png)

The figure above illustrates the major, minor, and patch versions. 

## What is a pip?
[Pip](https://pypi.org/project/pip/) is a package management tool in Python that we can use to install these dependencies. Visit [PyPI](https://pypi.org/)(Python Package Index) to install and publish Python packages. Click [here](https://docs.python.org/3/tutorial/modules.html#packages) to learn more about modules and packages.

`pip`

Run the above on the CLI to see a list of `pip` commands. Feeling adventurous? Play around the commands and come back to continue reading.

Mostly when you are working with virtual environments, you use `pip` alongside it to install dependencies. 

### Why installing packages globally using pip is a bad idea
- It becomes challenging to keep track of project dependencies across different projects when packages are installed globally.
- When working on a new project and installing the same package, pip automatically uninstalls the previous version. This can lead to compatibility issues with other projects.

Let’s take a look at the tools to create virtual environments.

## Tools for creating virtual environments

- venv
- virtualenv
- pipenv

## Venv
[Venv](https://docs.python.org/3/library/venv.html) is a lightweight built-in module in Python 3.3 and later versions for creating virtual environments requiring no installation.

### Creating a virtual environment
`mkdir env
py -m venv env/my_env`
`my_env` is the name of the virtual environment. Replace `my_env` with the name of the project.

### Activating a virtual environment
`.\my_env\Scripts\activate`

Now run this:
`pip list`

This command lets you see the packages you installed in the virtual environment. For now, only pip is available in the virtual environment. 
Installing packages into the virtual environment
`pip install python-decouple
pip install pillow
pip install django
pip install django-autoslug`

Install the above packages. Now run the `pip list`. You can see a list of dependencies.

### Deactivating a virtual environment
`deactivate`

To deactivate the environment, run the above command and run the `pip list` again.

### Reproducing the virtual environment
From the documentation, it says the virtual environment should be simple to delete and recreate from scratch.

`pip freeze > requirements.txt`
Delete the virtual environment, create a virtual environment, and activate it. Then run the command below:
`pip install -r requirements.txt`

This is particularly useful when collaborating on a project. You want to recreate the virtual environment on your machine.
NOTE: Always .gitignore the folder where the virtual environment is created.

### Specify the Python Interpreter while creating the virtual environment
`py –list`
Run the above command to see a list of Python versions installed on your system. Automatically Python uses the latest version installed. If you want to use another version while creating the virtual environment, run:

`py -X.Y -m venv myenv: e.g py -3.9 -m venv myenv`

NOTE: If the Python version is not installed or in the path you will get the error below. To install Python, click [here](https://www.python.org/downloads/windows/).

![An image showing an error message on the terminal](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m6fp4pphgy9vnrqa7hbn.png)

## Virtualenv
[Virtualenv](https://virtualenv.pypa.io/en/latest/) is a third-party [library](https://www.geeksforgeeks.org/libraries-in-python/amp/) requiring installation that is used to create virtual environments in Python. It has been available in earlier versions of Python such as Python 2. It is still being used; however, since Python 3.3, a subset of it has been integrated into the standard library under the `venv` module. `Venv` is not a replacement for `virtualenv`. It has small differences with the `virtualenv` library. To learn more about it, visit the documentation. 

To get started using it you have to install the virtualenv library.

`pip install pipx
pipx install virtualenv`

You can choose to use `pip` to install the library except for a small difference; the documentation recommended using `pipx` for the installation.

### Create the environment 
`virtualenv my_env`
This creates a folder in your current directory.

### Activate the environment
`.\my_env\Scripts\activate`
This activates the virtual environment.
Specify the Python Interpreter while creating the virtual environment
`virtualenv -p python-interpreter-path`

You need the right path that ends with `python.exe`. You can easily get this path by checking the path in environment variables.
"C:\path\to\your\python.exe" 
"C:\Users\<YourUsername>\AppData\Local\Programs\Python\Python3x\python.exe" 
NB: Replace `x` with the version. 
It should look like this on your system:
`virtualenv -p "C:\Users\<YourUsername>\AppData\Local\Programs\Python\Python312\python.exe" my_env`

### Virtualenvwrapper
[Virtualenvwrapper](https://virtualenvwrapper.readthedocs.io/en/latest/) creates and activates the virtual environment. It keeps all your environment in one place. It is an extension to the `virtualenv` tool. With virtualenvwrapper, you only need to type `workon` to switch between virtual environments. You can run the same command to see a list of virtual environments.
For windows we have a special package we use called [virtualenvwrapper-win](https://pypi.org/project/virtualenvwrapper-win/#:~:text=The%20idea%20behind%20virtualenvwrapper%20is,7%2F8%2F10). It only works on command prompt, so be sure to switch to a command prompt.
`pip install virtualenvwrapper-win`
`mkvirtualenv <name>`
Replace <name> with `venv` or any other name.
`lsvirtualenv`
It displays the list of virtual environments that have been created.
`workon <name>`
It activates the virtual environment. If the name is not specified, it lists the available environments.
`deactivate`
It deactivates the virtual environment.
To learn more commands you can check out the docs.

## Pipenv
[Pipenv](https://pipenv.pypa.io/en/latest/) is a virtualenv management tool and a package management tool. It combines venv or virtualenv and pip in a single command. It also works alongside pyenv and asdf to manage multiple python environments. It automatically creates a virtual environment and can be used for installation of packages as well.

### Install Pipenv Tool
`pip install pipenv`

### Activate the virtual environment
`pipenv shell`
NOTE: If you install a package with the `pipenv` command without activating the virtual environment, it will create a virtual environment for the project and ask you to run `pipenv shell` to activate it.

### Install packages
`pipenv install django
pipenv install python-decouple`

### Specify a version
`pipenv --python 3.9`
The version of Python specified must be installed on your system or it will request you to install `pyenv` or `asdf`.

### Exit the virtual environment
`Exit` or `exit` 

### Reproducing the virtual environment
To recreate it. All you have to do is run this command, and it installs all the dependencies from Pipfile. To learn more about its features read the docs.
`pipenv install`

NOTE: If `pyenv` is available it will automatically install the required Python version.

To learn more about the commands click [here](https://pipenv.pypa.io/en/latest/cli.html) or run `pipenv` on the CLI.

## Configuring and using a virtual environment in VSCode
To switch the virtual environment on Vs Code, look at the bottom right of the code editor. You can click on it to switch between interpreters. Another way is using a keyboard shortcut `ctrl+shift+p`, and the command palette pops up. Search for Python interpreters to switch between interpreters.

![An image showing the Python Interpreter on the terminal](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4f1asabkdi1607om3kzy.png)

Others:
[Poetry](https://python-poetry.org/): This is a Python packaging and dependency management tool. You will likely find it useful if your focus is on packaging and dependency management such as publishing a package to PyPI.
[Conda](https://conda.io/projects/conda/en/latest/user-guide/getting-started.html): It is a package management and environment management system. You will most likely find it useful if you are into data science or scientific computing. It is not limited to these two careers but it is widely used in data science and scientific computing.

## Conclusion
In this tutorial we learned about virtual environments and their importance in managing multiple project dependencies. We used built-in modules and third-party libraries to create virtual environments. The choice of which virtual environment to choose from might be confusing at first but except for small differences, it doesn’t matter. The choice depends on your preferences and project standards. If you are a beginner you can start with `venv` since it is the easiest to set up. When you get comfortable with it you can explore other virtualenv and stick to it.

Until then, for more insights:
☕ If you enjoy my content, you can buy me a cup of coffee [here](https://www.buymeacoffee.com/ifeoluwapru).
🧸 Check my social media profiles: [Twitter](https://x.com/PraiseID3?t=WqgSov3o-htz89CukDu3oA&s=09), [Github](https://github.com/praise002), and [LinkedIn](https://www.linkedin.com/in/praise-ifeoluwa-idowu-back-end-developer-django?utm_source=share&utm_campaign=share_via&utm_content=profile&utm_medium=android_app).