Advantages for Virtual Environment:
- separates python used for development from system python and other projects
- avoids issues with versions between projects
- allows ability to populate package list for projects for portablility between systems
What is Virtual Environment
Python virutal environment is essentially changing the path of your current terminal session. By appending to the front of the path environment variable it will then use the virutal environment that is setup with its own bin and lib files for python verses the system’s python bin and lib. This allows us to separate the system python packages away from project packages. So the project must be ran while virtual environment is active. You should see (venv) at the start of the command line prompter when the activate is sourced.
Path before activate:
Have virtualenv installed
pip install virtualenv
This is the basic workflow of starting a new project using virtual environment.
# Creates and goes to project directory mkdir proj && cd proj # Makes virtual environment folder name venv python -m venv venv # To activate virtual environment - sourcing the activate file . venv/bin/activate
At this time install any package dependencies for your python project with pip.
#Create list of python packages for project. pip freeze > requirements.txt # To exit/deactivate vitual environment deactivate