DEV Community

Cover image for How to Create a Python Virtual Environment in Windows and Linux
Donesrom
Donesrom

Posted on

How to Create a Python Virtual Environment in Windows and Linux

A virtual environment is a tool that allows you to create isolated Python environments. Think of your computer as a house with different rooms. A virtual environment could act like a room within that house.

When building a house, each room is built with its purpose in mind. For example, a kitchen will have installations that are only suited for a kitchen, such as a sink.

A virtual environment behaves the same way. It allows you to create special areas within your file system to build projects isolated from all other projects within your computer.

Virtual environments are useful for a number of reasons, including:

  • To avoid conflicts between different Python projects. If you have multiple Python projects, each with its own dependencies, you can use virtual environments to keep the dependencies of each project separate. This can help to prevent conflicts and errors.
  • To install different versions of Python packages. Some Python packages may require a specific version of Python. Virtual environments allow you to install different versions of Python packages in each environment without affecting the other environments.
  • To test new Python packages. If you are trying out a new Python package, you can create a virtual environment to test it out without affecting your system Python environment.

This guide will show you the most straightforward way to create a virtual environment on Windows and Linux so you can get on with your project.

How to Create a Virtual Environment on Windows

Step 1: Install Python
You can download the installer from the Python website.

Step 2: Install Pip
Run the following command in a terminal window:



python -m pip install --user pip



Enter fullscreen mode Exit fullscreen mode

Pip is a package manager for Python.

Step 3: Install VirtualEnv
Run the following command on your terminal window:



python -m pip install --user virtualenv



Enter fullscreen mode Exit fullscreen mode

VirtualEnv is a tool that allows you to create isolated Python environments.

Step 4: Create a folder/directory

Type,



mkdir dir_name



Enter fullscreen mode Exit fullscreen mode

Replace dir_name with the name of your project’s directory.

Step 5: Create your virtual environment

To create a virtual environment,Run the following command in a terminal window



mkvirtualenv source



Enter fullscreen mode Exit fullscreen mode

Make sure to replace source with the name of your virtual environment:

Note: Windows will create and activate your virtual environment at the same time. However, once you exit the terminal, you will need to activate your virtual environment once more.

How to Activate Your Virtual Environment on Windows

You will need to activate your virtual environment if you intend to use it.

Step 1: Activate the virtual environment

Run the following command in a terminal window:



cd dir_name
workon source


Enter fullscreen mode Exit fullscreen mode

Replace source with the name of your virtual environment.

The prompt of your terminal window will now be prefixed with the name of your virtual environment like this:

Activated Virtual Environment on Windows

This indicates that you are currently working in the virtual environment.

Install Packages in your Virtual Environment

You can now install packages in your virtual environment by running the following command in a terminal window:



pip install package_name



Enter fullscreen mode Exit fullscreen mode

For example, to install the numpy package, you would run the following command:



pip install numpy



Enter fullscreen mode Exit fullscreen mode

Deactivate the virtual environment

When you are finished working in your virtual environment, you can deactivate it by running the following command in a terminal window:



deactivate



Enter fullscreen mode Exit fullscreen mode

How to Create a Virtual Environment on Linux

Step 1: Install Python

You can install Python by running the following command in a terminal window:



sudo apt install python3



Enter fullscreen mode Exit fullscreen mode

Note: The sudo command temporarily grants a user elevated privileges to run commands that they would not normally be able to run, such as installing software or changing system settings.

Step 2: Install Pip

Run the following command in a terminal window:



sudo apt install python3-pip



Enter fullscreen mode Exit fullscreen mode

Pip is a package manager for Python.

Step 3: Install VirtualEnv

VirtualEnv is a tool that allows you to create isolated Python environments.

You can install it by running the following command in a terminal window:



sudo pip3 install virtualenv



Enter fullscreen mode Exit fullscreen mode

Step 4: Create a virtual environment

To create a virtual environment, run the following command in a terminal window:



virtualenv my_project



Enter fullscreen mode Exit fullscreen mode

Replace my_project with the name of your virtual environment.

This will create a new directory called “my_project” in your current working directory. This directory will contain all the files and packages needed for your virtual environment.

How to Activate Your Virtual Environment on Linux

Step 1:Activate the virtual environment

To activate your virtual environment, run the following command in a terminal window



source my_project/bin/activate



Enter fullscreen mode Exit fullscreen mode

Replace my_project with the name of your virtual environment.
The prompt of your terminal window will now be prefixed with the name of your virtual environment like this:

Linux Virtual Environment
This indicates that you are currently working in the virtual environment.

Install packages

You can now install packages in your virtual environment by running the following command in a terminal window:



pip3 install package_name



Enter fullscreen mode Exit fullscreen mode

For example, to install the numpy package, you would run the following command:



pip3 install numpy

Enter fullscreen mode Exit fullscreen mode




Deactivate the virtual environment

When you are finished working in your virtual environment, you can deactivate it by running the following command in a terminal window:



deactivate

Enter fullscreen mode Exit fullscreen mode




Conclusion

This guide has shown you how to create a Python virtual environment in Windows and Linux.

Virtual environments are a useful tool for managing Python dependencies and ensuring reproducibility. By following the steps in this guide, you can create isolated Python environments for your projects and avoid conflicts and errors.

Top comments (0)