Create a virtual environments in Python

Create virtual environments in Python is easy and they are quite useful if you need to test and install libraries that may conflict with libraries in other projects. They allow you to isolate an environment and test safely. Creating a virtual environment in Python is easy and we’ll show you how to do it.

» More Python Examples

On linux Debian 11

To create a virtual environment in Python or virtual environment, you can use this command in Linux:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
python3 -m venv /local_path/venv
python3 -m venv /local_path/venv
python3 -m venv /local_path/venv

Later, if you want to use it:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
source /local_path/venv/bin/activate
source /local_path/venv/bin/activate
source /local_path/venv/bin/activate

On Windows

To create a virtual environment on Windows first make sure you have virtualenv installed:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
pip install virtualenv
pip install virtualenv
pip install virtualenv

To create a virtual environment you must go to the folder where you want the environment to be created.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
cd project_path
cd project_path
cd project_path

Later you create the environment or environment with the following command:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
virtualenv venv
virtualenv venv
virtualenv venv

Finally you activate the environment:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
\project_path\Scripts\activate
\project_path\Scripts\activate
\project_path\Scripts\activate

Una vez que has activado el entorno, el prompt en tu terminal cambiará usando el nombre del ambiente virtual.

We hope this instructions of how to Create a virtual environments in Python will be useful for you.