# Topic covered
* Virtual Environment introduction
* Need of virtual environment
* Virtual Env types
    * virtualenv
    * virtualenv & virtualenvwrapper

2.1 Virtual Environment

A virtual environment is a tool that helps to keep dependencies isolated. Using virtualenv allows you to avoid installing Python packages globally which could break system tools or other projects.

  • It is used to manage Python packages for different projects
  • This is one of the most important tools that most of the Python developers use.

Why do we need a virtual environment ?

Imagine a scenario where you are working on two web based python projects, One of them uses a Django 1.9 and the other uses Django 1.10 and so on.

In such situations virtual environment can be really useful to maintain dependencies of both the projects

2.2 virtualenv

virtualenv is a tool to create isolated Python environments. It allows you to have separate environments for different projects, with their own dependencies and Python versions.

# Install virtualenv
pip install virtualenv
sudo apt install python3.8-venv

# Optional: create all VENV at one place
mkdir ~/.virtualenvs/
cd ~/.virtualenvs/

# Create a virtual environment
virtualenv myenv

# Activate
source ~/.virtualenvs/myenv/bin/activate

# Install all requirements
pip install Django
pip install -r requirements.txt

# To export all its packages and version for other project
pip freeze > requirements.txt

# Deactivate the environment
deactivate

Create Venv with different specific python version

# Venv with py 2.7
virtualenv -p /usr/bin/python2.7 py27_env

# python3.6 -m venv testvenv

# Install fron req.txt
pip install -r req.txt

2.3 VirtualEnv with virtualenvwrapper

virtualenvwrapper is an extension on top of virtualenv that simplifies the management of virtual environments. It adds a layer of convenience with features like centralizing environments and providing easy commands for creating, deleting, and switching between environments.

# Install Virtualenvwrapper
pip install virtualenv
pip install virtualenvwrapper

# Add this to vim `~/.zshrc or ~/.basrc`
export VIRTUALENVWRAPPER_PYTHON=/usr/local/bin/python3.6
source /usr/local/bin/virtualenvwrapper.sh

# Restart Terminal

# Make Virtualenv
mkvirtualenv imojo

# Activate venv
workon imojo

# Deactivate
deactivate

# Delete Venv
rmvirtualenv imojo

Make Venv with specific python version

mkvirtualenv imojo -p`which python`
mkvirtualenv imojo -p /usr/local/bin/python3.6

Manage env variables

# action after venv is activated
vim /Users/amrit/.virtualenvs/cont_appp/bin/postactivate

# Add `.env` file
export $(cat /Users/amrit/Desktop/IM/contact_app/.env)

# Check
echo $SECRET_KEY
os.getenv('SECRET_KEY')
os.environ.get('SECRET_KEY')

# pip install python-dotenv

Setup and installation issue

# Virtualenvwrapper settings:

export VIRTUALENVWRAPPER_PYTHON=/usr/local/bin/python3.6
export WORKON_HOME=$HOME/.virtualenvs 		# (optional)
export VIRTUALENVWRAPPER_VIRTUALENV=/home/goran/.local/bin/virtualenv # (optional)
source /usr/share/virtualenvwrapper/virtualenvwrapper.sh
  • Add this lines end of ~/.bashrc file
    • VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3.6
      • path of python where wrapper is installed
      • Not the path of python you want to use
    • . ~/.local/bin/virtualenvwrapper.sh
      • find the path of virtualenvwrapper.sh file