Working with the virtual environment in Python with Windows

Share

Photo by Ben Neale on Unsplash

Suppose, you are working on a flask application, you have all the requirements installed for the project including flask in your global environment which is your computer system. Now, imagine if you upgrade the flask application in your global environment and other packages that are used for your application have been changed as Python updates their packages and older functions used in your project from an older version of flask might deprecate. Thus, your code might break with those updated functions. It would be better if our project has an isolated environment where they had only the dependencies and packages that they need and specific versions they need. That is why a virtual environment is used. The virtual environment is a way that we can separate different Python environments for different projects. 

From the Python documentation:

A virtual environment is an isolated Python environment where a project’s dependencies are installed in a different directory from those installed in the system’s default Python path and other virtual environments.

Now that we have understood what a virtual environment is, let’s dive into working with it. Here, I have used windows, but it can be done with other operating systems as well. So, lets get started. I have used the command prompt, you can do it with PowerShell or Gitbash as well.

  1. Open the command prompt and go to the directory where you plan to do the project with as ‘cd yourproject’ or you can simply create a project folder as:

> mkdir myproject

> cd myproject

You can also use the command prompt of your code editor. Let’s say you are using Visual Studio Code, open terminal by “Ctrl + Shift + `” and choose ‘cmd’ from ‘+’ on your right as follows:

  1. If you don’t have virtualenv installed, install as:

> pip install virtualenv

  1. Creating a virtual environment inside your project file

> python -m venv venv_name

Here, when we run -m and specify a module here ‘venv’, Python will search sys.path and execute venv as our main module. Now we should have the new environment as venv_name. 

  1. Activate virtual environment.

> venv_name\Scripts\activate.bat

This command will activate our virtual environment.

Now that we have our virtual environment activated, we can type ‘pip list’ command to check all packages and their versions we have inside the venv.  You can install other packages too. Let’s say we need to install ‘requests’ for our project, we can do that as:

> pip install requests

Here, requests are installed only in your virtual environment.

  1. Getting Requirements.txt

> pip freeze

This will list down all the packages used in our project in the correct format, it is useful as it helps others to check dependencies used in our project and create their own environment. Simply copy and paste the output in the requirements.txt file.

  1. Deactivate the environment

You can deactivate your environment anytime by:

> deactivate

  1. Delete the virtual environment

You can simply hover over to the virtual environment name and delete the environment from project files or type the following command.

> rmdir venv_name /s 

Share

Leave a Reply

Your email address will not be published.

%d bloggers like this: