How to Create Python Virtual Environment

2022-02-13 Python

Table of Contents

In this article, we will introduce how to create a python virtual environment.

Create Python Virtual Environment

What is virtual environment

The main purpose of Python virtual environments is to create an isolated environment for Python projects. This means that each project can have its own dependencies, regardless of what dependencies every other project has.
You can think of virtual environments as package rooms for each projects.
We can manage our Python environment easily and select different Python runtime version for each projects with using virtual environment.

Create Python Virtual Environment

Install Pipenv

Pipenv is a tool that aims to bring the best of all packaging worlds (bundler, composer, npm, cargo, yarn, etc.) to the Python world. Windows is a first-class citizen, in our world.

We can use the command below to install Pipenv globally.

pip install --user pipenv

To execute the above command, we can open VS Code and select the bash shell, enter the above command and execute it.

Open VS Code and Select the bash shell
Create Python Virtual Environment

Enter the above command
Create Python Virtual Environment

Create a virtual environment with Pipenv

The virtual environment is always associated with a project. So let’s create a new project(folder) firstly.
Let’s click Open Folder button on the left sidebar.

Create Python Virtual Environment

A folder selection dialog will show up and let’s select the target root folder and create a new folder under it.
Next, select it and click Select Folder button to open it.

Create Python Virtual Environment

Create Python Virtual Environment

For the new folder, a trust selection dialog window will pop up.
Let’s check the Trust checkbox and click Yes, I trust the authors button.

Create Python Virtual Environment

Create a Python 3 virtual environment with Pipenv

pipenv --python 3

Create Python Virtual Environment

Create Python Virtual Environment

And let’s activate this virtual environment.

pipenv shell

Create Python Virtual Environment

Let’s check this new environment and list all python libraries.

pip list

The result will be as follows.

Package    Version
---------- -------
pip        21.3.1
setuptools 60.5.0
wheel      0.37.1

Install required libraries and run app

For example, we want to create a interactive web app with streamlit library, we should install streamlit library.

pip install streamlit 

When finished, we can use the command below to start a interactive streamlit app locally.

streamlit hello

Create Python Virtual Environment

Let’s see the result in the browser.

Create Python Virtual Environment

Other commands

If we don’t activate the virtual environment and want to install some libraries outside the environment, we can use the command below.

pipenv install <package>

Install from requirements.txt

pipenv install -r ./requirements.txt

Check security vulnerabilities

pipenv check

Lock the whole environment for constructing a same one in the future.

pipenv lock

We can use the command below to construct a completely same environment specified in Pipfile.lock file.
The exact versions of libraries specified in Pipfile.lock will be installed.

pipenv sync

We can use the command below to delete a pipenv virtual environment.

pipenv --rm

Conclusion

To manage our Python environment easily and select different Python runtime version for each projects, we can use pipenv to create and manage virtual environment.
We can also reconstruct a same environment with pipenv.

Subscribe and be the FIRST reader of our latest articles

* indicates required

Contact us