Create python virtual environment command

To create a virtual environment in Python, you can use the venv module which is built into the Python standard library in Python 3.3 and later versions.

Create a virtual environment in Python

To create a virtual environment in Python:

  1. Open the terminal or cmd on your system.
  2. Execute command - python3 -m venv env. This will create an "env" folder in the current folder that will have the virtual environment files.

Here is an example of how to create a virtual environment named "myenv" in a directory named "python_project":

python3 -m venv python_project/myenv

If you want to create the virtual environment in the current directory then you can execute the following command.

python3 -m venv env

Where env is the environment name. You can change it as per your wish.

Create virtual environment using pipenv or conda

You can also use pipenv or conda to create virtual environment.

pipenv --python 3.8
conda create --name myenv python=3.8

Activate the virtual environment

Once you have created the virtual environment, you can activate it by running the activate script.

source env/bin/activate
conda activate myenv

Deactivate the virtual environemnt

You can deactivate it by using

conda deactivate

This will change your shell's prompt to indicate that you are now working in the virtual environment and you can install packages without affecting the system-wide packages.

Was this helpful?