Creating Python Virtual Environment in Windows and Linux



Virtual Environment is used to manage our Python projects more efficiently. Every project has a dedicated environment for better results and future updates. For example, we have a project which uses Django2.1 and another one that uses Django2.2. Now, in these types of cases, we can make use of virtual environments to both projects individually without any problem. In this tutorial, we are going to see how to work with the virtual environment. We need to install virtualenv. Run the following command to see whether virtualenv is installed on your computer or not.

virtualenv --version

If you run the above command, you will see the following(maybe different version) output.

16.7.4

If you didn't see the version of the virtualenv then, it's not installed on your computer. Run the following command to install it on your PC.

pip install virtualenv

Now, we are ready to create our virtual environments. Follow the below steps one by one to create a virtual environment.

Virtual Environment

  • Command to initialize the project with virtualenv.

virtualenv project_name

If you run the above command, you will get a folder with the name you mentioned.

  • Command to activate the virtualenv.

source project_name/bin/activate

Now, you are in a virtual environment. Python defaults install the latest version of it in the virtualenv.

  • Command to deactivate.

deactivate

Conclusion

If you have any doubts regarding the tutorial, mention them in the comment section.


Advertisements