How to Create Virtual Environment in Python

Python is most widely used programming languages across the globe. With its simple and easy syntax, Python is a popular choice for beginners and experienced programmers. Python is quite a versatile high level programming language. It can be used to build all kinds of applications, from simple scrips to sophisticated enterprise business applications. Here we will learn how to Create Virtual Environment in Python.

Installing Python from Source

As you learned how to install Python from source in our previous tutorial, if you are new then you can go through how to install python from source code in Linux.

Creating Virtual Environment in Python

Python virtual environment is self-contained directory tree that contains a Python installation and some additional packages. It allows you to install Python modules in an isolated area for a specific project, rather than installed globally. In this way, you do not have to worry about affecting other Python projects.

Here, in this example, we’ll create a new Python 3.8 project called Python_app_1. First, We need to create the project directory and switch to it.

$  mkdir python_app
$  cd python_app

Inside the project, run the following command to create a virtual environment named python_app_venv .

$  python3.8 -m venv python_app_venv

Activate the virtual environment.

$  source python_app_venv/bin/activate

Once python virtual environment activated, the shell prompt will be prefixed with the name of the environment. Starting with Python 3.8, when creating virtual environment by default pip, the package manager for Python is installed.

$  (python_app_venv)$ python -v

#output
Python 3.8.5

Within the virtual environment, you can use python instead of python3.8 & pip instead of pip3.8.

Once you are done with your work, you can deactivate the virtual environment. To deactivate the environment, enter deactivate in shell prompt and you will return to your normal shell.

$  (python_app_venv)$ deactivate

After executing above command, you will be exited to normal shell.

Conclusion

So far you have learned how to create virtual environment in python 3. Stay tune for more tutorials.

See More : An Introduction of Python Programming Language

Share on:

Ravindra Kumar

Hi, This is Ravindra. I am founder of TheCodeCloud. I am AWS Certified Solutions Architect Associate & Certified in Oracle Cloud as well. I am DevOps and Data Science Enthusiast.

Recommended Reading..

1 thought on “How to Create Virtual Environment in Python”

Leave a Comment