How to Create and Activate a Virtual Environment with Python venv

In Python development, managing dependencies and isolating project environments is crucial for maintaining consistency and avoiding conflicts between different projects. One powerful tool for achieving this is Python’s built-in module venv, which allows you to create lightweight virtual environments. Here’s a step-by-step guide on how to create and activate a virtual environment using venv.

What is a Virtual Environment?

A virtual environment is an isolated Python environment that allows you to install dependencies for a specific project without affecting the global Python environment or other projects on your system. This ensures that each project has its own set of dependencies and versions, preventing conflicts and ensuring reproducibility.

Step 1: Install Python (if necessary)

If you haven’t already installed Python on your system, you’ll need to do so. You can download and install the latest version of Python from the official Python website:

Step 2: Open a Terminal or Command Prompt

Once Python is installed, open a terminal (on macOS and Linux) or command prompt (on Windows).

Step 3: Navigate to Your Project Directory

Navigate to the directory where you want to create your virtual environment. You can use the cd command to change directories.

cd path/to/your/project

Step 4: Create a Virtual Environment

To create a virtual environment, use the python -m venv command followed by the name of the directory where you want to create the virtual environment. For example:

python -m venv venv

This command will create a directory named venv containing the virtual environment files.

Step 5: Activate the Virtual Environment

To activate the virtual environment, use the appropriate command for your operating system:

  • On macOS and Linux:

source venv/bin/activate

  • On Windows:


Once activated, you’ll see the name of the virtual environment (in this case, (venv)) added to the beginning of your command prompt.

Step 6: Install Dependencies

With the virtual environment activated, you can now install dependencies for your project using pip. For example:

pip install package_name

Step 7: Deactivate the Virtual Environment

When you’re done working in the virtual environment, you can deactivate it using the deactivate command:


This will return you to your global Python environment.


Creating and activating a virtual environment with Python’s venv module is a fundamental skill for Python developers. By isolating project dependencies, you can ensure consistency and avoid conflicts between different projects. Start using virtual environments today to streamline your Python development workflow!

Now you’re ready to create and manage virtual environments for your Python projects using venv. Happy coding!

5 1 vote
Article Rating
Notify of
Inline Feedbacks
View all comments
Would love your thoughts, please comment.x