[Step-by-step] How to Create Virtual Environment in Python 3?

[Step-by-step] How to Create Virtual Environment in Python 3?

What is Virtual Environment in Python? Why is it needed? What are the Python commands to create and activate Virtual Environment?

I’m explaining it here. At the end of this tutorial, you will get answers to all your questions related to the virtual environment in Python 3.

Why is Virtual Environment Required?

We work on multiple projects. Every project does not require the same environment. We don’t need all the Python libraries installed on our system for all our projects.

The virtual environment allows us to create a separate virtual environment. You can only install selected Python modules you want in your virtual environment. This makes your environment more clean and simple.

Like, if you are working on two projects- one is the Django web framework project and another is Data Science project. Obvious we don’t need the same environment and libraries/modules for both projects. We need a Django module for the first project whereas the Data Science project does not need that module. Numpy, Pandas are the most useful Python libraries for data science which is not required in the Django project.

Now, it is clear. You need a virtual environment before starting your new project.

Virtual Environment in Python 3 [Commands]

We are using a Python module called virtualenv for creating a virtual environment.

Following commands works with most of the widely used OS like Windows, Linux and macOS.

Install virtualenv Python Module Using the pip tool

The virtual environment feature does not come with the default with any of the Python versions like Python2 and Python3. You have to install an external Python module from the PyPi repository for creating a virtual environment.

Run the following command to install the virtual environment module on your system:

pip install virtualenv

Note: You need an internet connection to download and install the Python module. The above command will not work if you don’t have an internet connection.

It will prompt you a success message after installation.

Collecting virtualenv
Downloading https://files.pythonhosted.org/packages/62/77/
virtualenv-16.7.8-py2.py3-none-any.whl (3.4MB)
| 3.4MB 1.3MB/s
Installing collected packages: virtualenv
Successfully installed virtualenv-16.7.8

By default, it will install the latest version of the Python module virtualenv. If you want to install a specific version of the Python module, you can specify that in the pip install command. You can learn more about it in the managing Python module tutorial.

You are all set to create your first virtual environment.

Create Virtual Environment using venv Command

Let’s say, you are creating a virtual environment for your new project called toolAlpha-django. You can give any valid name to your virtual environment.

py -m venv toolAlpha-django

It will create a folder with the name toolAlpha-django in your current directory path.

Your first virtual environment has been created. You have to activate this environment before working on your new project.

Activate Virtual Environment

Activate this virtual environment before installing any Python modules or before using it in your project.


Just to make sure, check the Python environment if it is set properly or not using where python command for Windows. On Linux, you can check it with the command which python.

(toolAlpha-django) F:\~~\toolAlpha-django>where python

You can see the first path from the virtual environment that we have activated just now.


Your virtual environment is activated. You are free to use it. Start with installing important Python modules suitable for your project need.

You can always check all the installed Python modules in your project environment.

Check All Modules Installed in Virtual Environment

You can check all the modules you have installed in your virtual environment using the pip tool.

(toolAlpha-django) F:\~~\toolAlpha-django>pip freeze

The command pip freeze will list out all the modules present in your virtual environment.

Deactivate Virtual Environment

Before leaving the project or switching to another project virtual environment, you can deactivate your current virtual environment.

(toolAlpha-django) F:\~~\toolAlpha-django>deactivate

These are the simple commands you can use to create and use a virtual environment for your Python project.

These commands for creating a virtual environment in Python 3 are tested for Python 3 in the Windows environment. These commands also work in the Linux environment like Ubuntu, RedHat…

Happy Pythoning!

Leave a Reply

Your email address will not be published. Required fields are marked *