When you create a Python application then there is a need to create virtual environment. Virtual environment helps you to maintain your dependencies in an isolated location. This doesn't effect the global system dependencies. Also, using virtual environment you can have python application that supports different version of Python
In this post we will be creating Python2.7 virtual environment
- Python2.7 must be installed
- pip must be installed
Step 1 : Find the location of Python Interpreter
- You need to find the path of Python2.7 interpreter
- This will depend upon the operating system that you are using
Find Python2.7 Interpreter Path in Linux/Mac Operating System
- Open you terminal and use below command
Find Python2.7 Interpreter Path in Windows Operating System
- Open the Command Prompt and use below command
- You will get the python Interpreter path. Copy the path of interpreter as it will be needed in subsequent steps.
Step 2 : Install virutalenv python package
pip install virtutalenv
Step 3 : Create Virtual environment
- Go to the directory where you want to create Python2.7 virtutalenv
- Use below command and replace [path-of-python-2.7-interpreter] with the value obtained in step 1
virtutalenv -p [path-of-python-2.7-interpreter] venv
- This will create a folder with name venv. All your python packages for a particular application will be installed in their respective venv folder.
Step 4 : Activating the virtual environment
- Before installing any python package or library you have to activate the virtual environment.
Activate Virtual Environment in Linux/Mac Operating System
Activate Virtual Environment in Windows Operating System
Step 5 : Deactivate virtual environment
- use the below command to deactivate the virtual environment
Step 6 : Deleting the virtual environment
- Just remove all the contents from venv folder, and your virtual environment will be deleted.