How to create a virtual environment in Python?
Tagged: python
- AuthorPosts
- October 24, 2020 at 1:02 PM #4141Santhosh Kumar DKeymaster@santhosh
The
venv
module allows us to create a virtual environment within a site’s directory. Each virtual environment has its own Python library and can have its own independent set of installed Python packages. In this tutorial, I am going to use Python 3.8.2 and Command Prompt to create a virtual environment.Create a virtual environment
First, we have to move to the directory within which we want to create a virtual environment. Use the command
venv
to create a virtual environment. For instance, let’s create a directory calledenvironment
, and create a virtual environment inside that directory as shown below.python -m venv environment
Alternatively, we can directly mention the path of the directory within which we want to create a virtual environment there. For instance, the below command will create a directory called
environment
and creates a virtual environment inside the directory as per the mentioned path.python -m venv /directory/site/environment
Activate a virtual environment
Once a virtual environment has been created, we need to activate it using a platform-specific script in the virtual environment’s directory. The following commands can be used to activate a virtual environment.
Platform Shell Command to activate virtual environment Windows Command Prompt venv\Scripts\activate.bat
PowerShell venv\Scripts\Activate.ps1
POSIX bash/zsh venv/bin/activate
fish venv/bin/activate.fish
csh/tcsh venv/bin/activate.csh
PowerShell Core venv/bin/Activate.ps1
Note: Replace
venv
with the path of the directory containing the virtual environment.For instance, in this tutorial, we have created a virtual environment inside a directory called
environment
. To activate it, we can use the Command Prompt specific activation script as follows.environment\Scripts\activate.bat
Our virtual environment is now active as it shows the name
(environment)
in the command line.
Source: venv — Creation of virtual environments
Learn more Python tips.
- AuthorPosts
- You must be logged in to reply to this topic.