In this article I will show you how to work with Python virtual environments.
I mostly use python virtual environment, because I don’t want to install python libraries globally on my dev machine, and this helps alot, because if I want to just check a python library, installing it and then uninstalling it globally is not a good idea.
Checking Virtual Environment
First step is if on your machine virtual environment is installed or not? If no, then you will need to install it. If you are using python 3, then it’s already installed on your machine. But for python 2, it is not by default installed.
To check if virtual environment is available on your machine, run this command:
To install virtualenv, run below command:
pip install virtualenv
For python 3, there is another library “venv” is by default included in standard library. So you don’t need to install anything related to virtual environment for python 3.
Creating Virtual Environment
To create a new virtual environment for your project, you need to execute below command:
For Python 2:
For Python 3:
python3 -m venv your_virtual_environment_name
These connamnd will create a new folder in you current executing location with your environment name.
Activating Virtual Environment
After creating it, you need to activate the virtual environment. That is little different on window and Linux systems.
After executing it, in your terminal you can see your environment name in parenthesis before prompt. Eg: (your_virtual_environment_name) $
Deactivating Virtual Environment
To deactivate, you need to execute deactivate comman. This works on both Windows and Linux.