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 virtualenvpip install virtualenvpip install virtualenv
Enter fullscreen mode Exit fullscreen mode
check it by running this in terminal
this will list out the available commands
virtualenvorvirtualenv --helpvirtualenv or virtualenv --helpvirtualenv or virtualenv --help
Enter fullscreen mode Exit fullscreen mode
syntax to create a virtual environment
Usage: virtualenv [OPTIONS] DEST_DIR-p PYTHON_EXE, --python=PYTHON_EXEUsage: virtualenv [OPTIONS] DEST_DIR -p PYTHON_EXE, --python=PYTHON_EXEUsage: virtualenv [OPTIONS] DEST_DIR -p PYTHON_EXE, --python=PYTHON_EXE
Enter fullscreen mode Exit fullscreen mode
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 venvvirtualenv --python=python3.7 venvvirtualenv --python=python3.7 venv
Enter fullscreen mode Exit fullscreen mode
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/venvvirtualenv --python=python3.7 test/venvvirtualenv --python=python3.7 test/venv
Enter fullscreen mode Exit fullscreen mode
Virtual environment is created but we dont have the access for it, need to be activated
source venv/bin/activatesource venv/bin/activatesource venv/bin/activate
Enter fullscreen mode Exit fullscreen mode
(venv) mac@macLBP %
this will activate the environment, you can see the venv in terminal and to disabled it just run
deactivatedeactivatedeactivate
Enter fullscreen mode Exit fullscreen mode
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 testenv1virtualenv --python=python testenv1virtualenv --python=python testenv1
Enter fullscreen mode Exit fullscreen mode
to create environment for python 3.7
virtualenv --python=python3.7 testenv2virtualenv --python=python3.7 testenv2virtualenv --python=python3.7 testenv2
Enter fullscreen mode Exit fullscreen mode
to create environment for python 3.8
virtualenv --python=python3.8 testenv3virtualenv --python=python3.8 testenv3virtualenv --python=python3.8 testenv3
Enter fullscreen mode Exit fullscreen mode
finally you can install any packages as you want, package dependence in one project will not affect others
virtualenv --python=python3 test/venvcd testsource venv/bin/activatepython -VPython 3.7.3pip freezepip install flaskpip freezeclick==7.1.2Flask==1.1.2itsdangerous==1.1.0Jinja2==2.11.2MarkupSafe==1.1.1Werkzeug==1.0.1deactivatevirtualenv --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 deactivatevirtualenv --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
Enter fullscreen mode Exit fullscreen mode
暂无评论内容