Python - Setup Virtual Environment in Windows and Linux
Python - Setup Virtual Environment in Windows and Linux
Introduction
- A
Virtual Environment
is a Python environment, that is an isolated working copy of Python that allows you to work on a specific project without affecting other projects. - So basically it is a tool that enables multiple side-by-side installations of Python, one for each project.
Creating a Python virtual environment in Linux
Install Pip on your host.
1
sudo apt-get install python-pip
Then install
virtualenv
and check the version.1 2
pip install virtualenv virtualenv --version
Create a virtual environment now by providing a name.
1
virtualenv virtualenv_name
This will create a folder named
virtualenv_name
will be created. If you want to create a virtualenv for specific python version, type1
$ virtualenv -p /usr/bin/python3 virtualenv_name
or
1
virtualenv -p /usr/bin/python2.7 virtualenv_name
Now at last we just need to activate it, using command
1
source virtualenv_name/bin/activate
Now you are in a Python virtual environment You can deactivate using
1
deactivate
Creating Python virtualenv in Windows
If python is installed in your system, then pip comes in handy. So simple steps are: 1) Install virtualenv using
1
pip install virtualenv
Now in which ever directory you are, this line below will create a virtualenv there
1
python -m venv myenv
To active the virtual environment in the same directory, type the below command
1
myenv\Scripts\activate
To deactive the virtual environment, type
1
deactivate
This post is licensed under CC BY 4.0 by the author.