What is virtual environment?
A tool to create an isolated environment to run the different projects with their own dependence.
For example: If you want to run 2 django or flask projects in the same machine with different package version. You have to isolate them into different environments.
django1.2 in venv1
django2.2 in venv2
run this to install
pip install virtualenv check it by running this in terminal
this will list out the available commands
virtualenv or virtualenv --help syntax to create a virtual environment
Usage: virtualenv [OPTIONS] DEST_DIR -p PYTHON_EXE, --python=PYTHON_EXE PYTHON_EXE is for which version of python you are going to create a virtual environment
Create a virtual environment with name as venv, this will create it in the current working director
virtualenv --python=python3.7 venv to create at a specific directory you can go to the path or else you can run the command with the path of venv
virtualenv --python=python3.7 test/venv Virtual environment is created but we dont have the access for it, need to be activated
source venv/bin/activate (venv) mac@macLBP %
this will activate the environment, you can see the venv in terminal and to disabled it just run
deactivate to create environment for python 2, by default python will be taken a python2(in my machine its python 2 for you it may vary)
virtualenv --python=python testenv1 to create environment for python 3.7
virtualenv --python=python3.7 testenv2 to create environment for python 3.8
virtualenv --python=python3.8 testenv3 finally you can install any packages as you want, package dependence in one project will not affect others
virtualenv --python=python3 test/venv cd test source venv/bin/activate python -V Python 3.7.3 pip freeze pip install flask pip freeze click==7.1.2 Flask==1.1.2 itsdangerous==1.1.0 Jinja2==2.11.2 MarkupSafe==1.1.1 Werkzeug==1.0.1 deactivate
Top comments (2)
Since python3, I've been using
python3 -m venv venv. Is there anything virtualenv does extra compared to the other solution ?they both do the same thing, but with some difference
virtualenv which works in both python 2 and 3 and this will copy the python binary file to the venv location, for more you can check here
virtualenv.pypa.io/en/latest/
I tend to used to this method, you know people follow their own way of doing thing