How to create a Virtual Environment in Python in 1 Minute?
Tags: python, venv
Published: 2024-06-17 09:58:21

A virtual environment in Python is a **self-contained** directory that contains a specific **Python interpreter** and a set of **installed packages**. It allows you to **isolate** your Python project's dependencies from the system-wide Python installation and other projects.

By creating a virtual environment, you can have different versions of Python and different sets of packages for each project, without worrying about conflicts or dependencies between them. This is particularly useful when working on multiple projects that require different versions of packages or when collaborating with others who may have different setups.

When you activate a virtual environment, it modifies the system's PATH environment variable to prioritize the Python interpreter and packages within the virtual environment. This ensures that when you run Python commands or install packages, they are specific to that environment.

## How to create a virtual environment in Python?

There are several ways to create a virtual environment in Python, we will learn the most common methods which are using `venv` module using CLI and **extention** in VSCode.

### 1. Using `venv` module in CLI

The `venv` module is the built-in tool for creating virtual environments in Python. Here's how you can create a virtual environment using `venv` in the command line:

1. Open your terminal or command prompt.
2. Navigate to the directory where you want to create the virtual environment.
3. Run the following command to create a virtual environment named `myenv`:
  
    **Windows**:

    ```bash
    python -m venv myenv
    ```

    **macOS/Linux**:

    ```bash
    python3 -m venv myenv
    ```

4. To activate the virtual environment, run the following command:

    **Windows**:

    ```bash
    myenv\Scripts\activate
    ```

    **macOS/Linux**:

    ```bash
    source myenv/bin/activate
    ```

    This will run a script that modifies your shell's PATH to prioritize the Python interpreter and packages within the virtual environment.

5. You should see the name of the virtual environment `(myenv)` in your terminal prompt, indicating that the virtual environment is active.
    > Sometimes in VS Code you may not see the `(myenv)` in the terminal prompt, butyou can verify the activation by running `where.exe python` or `which python3` command. It should point to the Python interpreter within the virtual environment.
6. You can now install packages using `pip` or run Python scripts within this virtual environment.
7. To deactivate the virtual environment, simply run the `deactivate` command in the terminal which will be found in the `env > Scripts` folder in the project.
8. To delete the virtual environment, you can simply delete the directory containing it.
9. **That's it!** You have successfully created and activated a virtual environment in Python using `venv`.

### 2. Using the Python extension in VSCode

1. Open your Python project in Visual Studio Code (VSCode).
2. Click on the **Python interpreter** in the bottom left corner of the VSCode window.
3. Click on **Create Virtual Environment**.
4. Choose `venv` as the virtual environment type.
5. Select the base interpreter (Python version) to use for the virtual environment.
6. The virtual environment will be created in a `.venv` directory within your project.
7. Now if you open a new terminal in VSCode, it should automatically activate the virtual environment.
   > Sometimes in VS Code you may not see the `(myenv)` in the terminal prompt, butyou can verify the activation by running `where.exe python` or `which python3` command. It should point to the Python interpreter within the virtual environment.
   ![vs code env active message](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bqyhaas3bkpilttd3n50.png)
8. If the above did't work for you, use Python extension in VSCode to select the interpreter from the virtual environment.
    <https://marketplace.visualstudio.com/items?itemName=donjayamanne.python-environment-manager>

![vs code python extension tab](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2r4u8ayhoe2adun53asa.png)

## Conclusion

Creating a virtual environment in Python is a simple yet powerful way to manage your project's dependencies and ensure that they are isolated from other projects and the system-wide Python installation. By following the steps outlined above, you can quickly set up a virtual environment for your project and start developing with confidence.

I hope this article has been helpful in understanding how to create a virtual environment in Python. If you have any questions or feedback, please feel free to reach out. Happy coding!

Do share this article with your friends and colleagues who might find it useful. Thank you for reading!