# Topic covered
* Virtual Environment introduction
* Need of virtual environment
* Virtual Env types
* virtualenv
* virtualenv & virtualenvwrapper
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
virtualenv
It created venv at current location/dir, created along-with the project
- Issue
- Many different venv will be
scattered in many different location
- Changing venv path(moving/renaming)
breaks the venv
- Many different venv will be
# Install virtualenv
pip install virtualenv
sudo apt install python3.8-venv
# Create
virtualenv project1
# Activate
source project1/bin/activate
# Install all requirements.txt
pip install -r requirements.txt
# To export all its packages and version for other project
pip freeze > requirements.txt
# Exit from venv
deactivate
Create Venv with different specific python version
# Venv with py 2.7
virtualenv -p /usr/bin/python2.7 py27_env
# Install fron req.txt
pip install -r req.txt
Create venv in root location
# Install
pip install virtualenv
sudo apt install python3.8-venv
# Make Venv
mkdir ~/.virtualenvs/
cd ~/.virtualenvs/
python3.6 -m venv testvenv
# Activate venv
source ~/.virtualenvs/testvenv/bin/activate
VirtualEnv with Virtualwrapper
- All the venv are
created at one place
in C Drive - Manage env variable
- Easy to –> Activate, Delete
# 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
- find the path of
- VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3.6