SetUp Python virtual environment

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

virtualenv

run this to install

pip install virtualenv
pip install virtualenv
pip install virtualenv

Enter fullscreen mode Exit fullscreen mode

check it by running this in terminal
this will list out the available commands

virtualenv
or
virtualenv --help
virtualenv
or
virtualenv --help
virtualenv 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_EXE
 Usage: virtualenv [OPTIONS] DEST_DIR
 -p PYTHON_EXE, --python=PYTHON_EXE
Usage: 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 venv
 virtualenv --python=python3.7 venv
virtualenv --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/venv
 virtualenv --python=python3.7 test/venv
virtualenv --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/activate
 source venv/bin/activate
source 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

deactivate
deactivate
deactivate

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 testenv1
 virtualenv --python=python testenv1
virtualenv --python=python testenv1

Enter fullscreen mode Exit fullscreen mode

to create environment for python 3.7

virtualenv --python=python3.7 testenv2
 virtualenv --python=python3.7 testenv2
virtualenv --python=python3.7 testenv2

Enter fullscreen mode Exit fullscreen mode

to create environment for python 3.8

virtualenv --python=python3.8 testenv3
 virtualenv --python=python3.8 testenv3
virtualenv --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/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
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
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

Enter fullscreen mode Exit fullscreen mode

原文链接:SetUp Python virtual environment

© 版权声明
THE END
喜欢就支持一下吧
点赞15 分享
Making the absolute best of ourselves is not an easy task. It is a pleasurable pursuit...but it requires patience, persistence, and perseverance.
做最好的自己并不容易,这是很美好的愿望,需要耐心、坚持和毅力
评论 抢沙发

请登录后发表评论

    暂无评论内容