top of page

Posts

How to create virtual environment using Python's “venv”



In this tutorial, I’m going to create a virtual environment using Python's venv.


What is venv?


venv is a module introduced in Python version 3.3 for creating Python virtual environments on top of the base Python.


By using virtual environments, you can install Python packages on a per-project basis rather than system-wide. This allows you to use different versions of packages for different projects and avoids conflicts between dependencies when launching other projects.


The venv is recommended for creating virtual environments for Python versions 3.5 and later. (2023/08)



Let’s try out

Now, let's create a virtual environment..


If you're using Python version 3.3 or later, there's no need for additional installations since venv is included as a standard feature.


For this example, I used Python 3.11.0 on Windows.


python -V
Python 3.11.0

Create a virtual environment


For convenience, we'll create a directory named 'test_dir' and create the virtual environment inside it.

l named it 'myenv1' to facilitate managing the virtual environment.



mkdir test_dir
cd test_dir
python -m venv myenv1

To make it more readable, I've hidden some columns, but here's the contents of the venv as the following.

SInce I’m going to describe only how to use it, I’m not going to describe the details this time, but since it is an important directory/file for configuring a virtual environment, do not make any changes.


ls myenv1
 
Directory: C:\Users\ichik\test_dir\myenv1
 
Mode LastWriteTime  Name
---- -------------  ----
d----- 2023/08/28 13:30 Include
d----- 2023/08/28 13:30 Lib
d----- 2023/08/28 13:30 Scripts
-a---- 2023/08/28 13:30 pyvenv.cfg


Activate the virtual environment


Use the following command to activate the virtual environment. If the environment name is displayed at the beginning of the prompt, it means it has been activated successfully.


.\myenv1\Scripts\Activate.ps1
(myenv1) PS C:\Users\ichik\test_dir>

Please note that the command may vary depending on your execution environment. Please refer to the table below.



Platform

Shell

Command for Activating the Virtual Environment

POSIX

bash/zsh

$ source <venv>/bin/activate

POSIX

fish

$ source <venv>/bin/activate.fish

POSIX

csh/tcsh

$ source <venv>/bin/activate.csh

POSIX

PowerShell

$ <venv>/bin/Activate.ps1

Windows

cmd.exe

C:> <venv>\Scripts\activate.bat

Windows

PowerShell

PS C:> <venv>\Scripts\Activate.ps1

While the environment name is displayed, the Python in use is the one from the virtual environment, not the base Python.

By default, the Python version in the virtual environment is the same as the base Python version.


python -V
Python 3.11.0

As mentioned earlier, packages in the virtual environment are independent, so you can install packages as needed.


Deactivating the virtual environment


Use the following command to deactivate the virtual environment.


(myenv1) PS C:\Users\ichik\test_dir> deactivate

If you no longer need the virtual environment, you can delete the entire directory.


Example 1: Copy the virtual environment for reuse


If you want to use the virtual environment on a different PC or share it with others for development, it's not recommended to simply copy the venv itself. Instead, create a list of packages and share it along with your source code using version control tools like git.


You can generate a list of packages with the following command.


(myenv1) PS C:\Users\ichik\test_dir> python -m pip freeze > requirements.txt

Then, in a new virtual environment, you can install the packages from the requirements.txt file using the following command.


(myenv1) PS C:\Users\ichik\test_dir> python -m pip install -r requirements.txt

Example 2: Start up the Virtual Environment with a Different Python Version (Windows)


It's also possible to start a virtual environment with a different Python version by installing the desired version. First, check the installed Python versions:


py --list
-V:3.11 * Python 3.11 (64-bit)
-V:3.9 Python 3.9 (64-bit)

In this example, we'll create a virtual environment using Python 3.9:


py -3.9 -m venv pyenv39
.\pyenv39\Scripts\Activate.ps1

Now you have a virtual environment running with a different Python version:


pyenv39) PS C:\Users\ichik\test_dir> python -V
Python 3.9.13

You can execute your program with a different Python version by referencing the source code and requirements.txt and pip install.


Summary



・venv is a module introduced in Python 3.3 for creating lightweight Python virtual environments on top of the base Python.

・It allows you to use different versions of packages for different projects and avoids conflicts between dependencies.

・You can launch other projects without worrying about the impact of dependencies.


Thank you for reading.


This blog post is translated from a blog post written by Sota Ichiki on our Japanese website Beyond Co..


Recent Posts

See All

Comments


bottom of page