How do I set up a virtual environment in Python?

Setting up a virtual environment in Python is a common practice to manage dependencies and isolate project-specific packages. Follow these steps to create a virtual environment:

### Using `virtualenv`:
1. Install `virtualenv`: If you haven't installed `virtualenv` yet, you can do so using pip:


pip install virtualenv


2. Create a Virtual Environment: Navigate to your project directory in the terminal and run:


virtualenv venv


This command creates a new directory named `venv` that contains a standalone Python environment.
3. Activate the Virtual Environment:
- On Windows:


venv\Scripts\activate


- On macOS and Linux:


source venv/bin/activate



### Using `venv` (built-in in Python 3.3+):
1. Create a Virtual Environment: Navigate to your project directory in the terminal and run:


python -m venv venv


This command creates a new directory named `venv` with a Python environment.
2. Activate the Virtual Environment:
- On Windows:


venv\Scripts\activate


- On macOS and Linux:


source venv/bin/activate



### Common Commands:
- Deactivate the Virtual Environment:


deactivate


- Install Packages:
After activating the virtual environment, you can use pip to install packages specific to your project.

By following these steps, you can set up a virtual environment in Python to manage dependencies for your projects effectively.
How do I set up a virtual environment in Python?

Related Questions