Install Python virtual environment step by step

Using a Python virtual environment (venv) is a great way to manage dependencies for your projects and ensure that each project has its own isolated environment. This prevents conflicts between project requirements and allows you to experiment with different package versions without affecting other projects. Here’s how you can create and use a virtual environment in Python step by step:

Step 1: Install Python

Ensure that Python is installed on your system. You can download Python from if it’s not already installed. You can verify the installation and the version of Python by running the following command in your command prompt or terminal:

python --version

Or python3 --version if your system differentiates between Python 2 and Python 3.

Step 2: Create a Virtual Environment

  1. Open your terminal or command prompt.
  2. Navigate to the directory where you want to create your project. You can do this with the cd command. For example:
   cd path/to/your/project
  1. Once you are in the project directory, run the following command to create a virtual environment. Replace env_name with the name you wish to give your virtual environment:
   python -m venv env_name

Or use python3 if python points to Python 2.x on your system.

This command creates a directory named env_name in your project directory, containing the virtual environment.

Step 3: Activate the Virtual Environment

Activating the virtual environment will change your shell’s prompt to show what virtual environment you’re using, and modify the environment so that running python will get you that particular version and installation of Python.

  • On Windows:
  • On macOS and Linux:
  source env_name/bin/activate

Step 4: Install Packages

With the virtual environment activated, you can install packages using pip, the Python package installer. For example, to install the requests library, you would run:

pip install requests

These packages will be installed within the virtual environment only, and won’t affect the rest of your system.

Step 5: Run Python Code

You can now run Python scripts in your activated virtual environment. Any command python or python3 will use the Python interpreter and the libraries available in your virtual environment.

Step 6: Deactivate the Virtual Environment

Once you are done working in the virtual environment, you can deactivate it by running:


This command will revert to the system’s default Python interpreter and settings.

Step 7: Managing Dependencies

To keep track of your project’s dependencies, you can export the list of installed packages to a file:

pip freeze > requirements.txt

And later, you can install all the dependencies from this file in another environment:

pip install -r requirements.txt


Using virtual environments is a best practice for Python development. It helps in managing dependencies, ensuring projects are isolated from each other, and makes it easy to replicate environments across different systems or deployment stages.

Leave a Comment