Simplest way to create a virtual environment in Python 3

By Rahul Pandit

Posted on Monday, 07 October 2019

There are many ways to create a virtual environment in Python 3. Some use virtualenv, some use pyenv and some use poetry package. I think following is the easiest one.

I'm assuming that you're using Debian or Ubuntu or one of many Ubuntu-based Linux distribution and you are familiar with command line.

Install python3-venv package using apt.

sudo apt install python3-venv

Create a new directory which will house your virtual environment and your project and go into it.

mkdir foo && cd foo

Create a virtual environment called project_env.

python3 -m venv project_env

Activate the virtual environment you just created.

source project_env/bin/activate

Upgrade pip package.

pip install -U pip

If you have a requirements.txt file of your project, copy that into the project directory. Now you can install packages listed in requirements.txt by executing following command.

pip install -r requirements.txt

And when you finish working on the project, execute following command to deactivate the virtual environment.

deactivate


Cover Picture Credit : Photo by Brandable Box on Unsplash


Tags : Python




Recent Posts

Deploy Vaultwarden password manager, Portainer, Nginx and Certbot in Docker


Good Pi-hole blocklists that stop online ads, trackers and malware


Block online ads, trackers and malware with Pi-hole, WireGuard, DoT and DoH servers


Free third-party DNS for blocking ads and trackers


My Chess Notes