Creating Python Virtual Environments

There are multiple ways for creating python virtual environments:
  1. Using venv - a builtin module that is available from Python 3.6 (or)
  2. Using virtualenv - a python plugin package
  3. Using pipenv - a python plugin which combines pip and virtual environment
We will be using virtualenv package for creating python virtual environments, as it would work for all versions of python. An alternative - pipenv usage is also explained at the end.

How Python Virtual Environment Creation Works

What basically happens is, a complete copy of an existing python environment is made. So this means that, whatever version of python virtual environment you would like to set up, you need to install it in your system first. Once installed, you can create as many virtual environments from it as you want. 

Setting up Your Default Python

For ease of use, I would recommend to have a default python environment. You can do this by adding the path of the python installation to environment variables. This option is available during python installation as "Add Path to Environment Variables", which would be unchecked by default. Check this option during installation. 

How to Check if you have a Default Python Environment?

Open Command Prompt and execute below command.

    python --version

If the above command returns a python version number, then that is your default python environment version. If it doesn't return a python version, then it either means python installation path is not added to environment variables or python is not installed.

If you would like to know the location of the default python installation, use the below command (for Windows) and this would return the location of the default python installation.

    where python

Installing virtualenv

Open command prompt and install virtualenv by using command below.
    
    pip install virtualenv

This would install the virtualenv module in your default python environment. Using this, you can then create python virtual environments (of any python version that is installed in your system)

Creating Python Virtual Environment

Typically its recommended to create a python virtual environment for each project that you work on. The location of such virtual environments are usually setup in each project folder with name of venv.

Go to the directory where you would like to create the python virtual environment (like the project directory) and type cmd in Windows Explorer path. This would open up the command prompt window at the current directory.

Virtual Environment for the Default Python Installation

To create a virtual environment from the default python installation, use the below command in command prompt.

    virtualenv <any_name>
    
    Eg: virtualenv venv
The above command creates a virtual environment with the name "venv" in the current directory.

Virtual Environment for Any Installed Python Version

To create virtual environment for any python version installed in your system, the command format is 

    virtualenv -p <python exe path> <name/path for virtual environment>

For example, if you want to create a virtual environment for Python 2.7 which is installed at C:\Python27, the command would be:

    virtualenv -p C:\Python27\python.exe venv

Alternatively, you can specify the absolute path for the location of the virtual environment, as below.

    virtualenv -p C:\Python27\python.exe D:\PythonEnvironments\Proj1_venv

[Alternative] Using pipenv

pipenv works as a combination of creating virtual environment as well as a replacement for pip, which manages python packages. The virtual environments created by pipenv are stored externally and there is a one to one mapping between the folder and its virtual environment. pipenv also maintains a pip file, which is a replacement for requirements.txt with improved features and better dependency handling and upgrades.

To install pipenv, use
    pip install pipenv

To create/activate a python virtual environment for the current directory, use
    pipenv shell

To install any packages, use pipenv instead of pip.For example, instead of pip install flask, use
    pipenv install flask

To uninstall a package, use
    pipenv uninstall flask

To delete the python virtual environment for the current directory, use
    pipenv --rm
 
Do you use python virtual environments in your project? Where do you maintain the virtual environments? Let us know in the comments.

No comments:

Feel free to leave a piece of your mind.

Powered by Blogger.