Virtual python development environment to build

1. What is the python virtual environment?

The main purpose of Python virtual environment is to create mutually independent operating environment to different projects. In a virtual environment, each project has its own dependencies, regardless of the other projects. Different virtual environments in the same package can have different versions. Also, there is no limit the number of virtual environment, we can easily use virtualenv or pyenv and other tools to create multiple virtual environments.

2. Why use a virtual environment?

In the actual project development, we usually according to their own needs to download a variety of appropriate framework library, such as Scrapy, Beautiful Soup and so on, but each project may use framework libraries are not the same, or different versions of the framework, so we need to constantly update according to the needs or uninstall the library. Direct hate our operating environment Python development environment and make our project caused a lot of unnecessary trouble, the management is also very confusing. As in the following scenario:

Scene 1: A project needs a framework version 1.0, this library project B requires version 2.0. If you do not install the virtual environment, then when you use these items, you will need to uninstall back and forth, so it is easy to give your project brings inexplicable error; 

Scenario 2: the company before the project needs python2.7 operational environment, and you take over the project needs to run in python3 environment, think you should know, if you do not use a virtual environment, which of these two items may not be available at the same time, using the project before python3 the company may not be able to run, anyway the new project is run in trouble. And if the virtual environment can configure different operating environment, so that the two projects for the two projects can be run simultaneously.

 3, install the virtual environment ( Linux systems )

3.1, install, create a python virtual environment running tool

Linux installed python pip tool is not installed by default, so you must first install pip tool.

(Note: If there is no command pip, then install epel source, then yum install python-pip)

[root@localhost ~]# yum install python-pip

[root@localhost ~]# pip install --upgrade pip

[root@localhost ~]# yum install python-virtualenv

Note: If the download is too slow can download a domestic source:

[root@localhost ~]# pip install -i http://pypi.douban.com/simple/ --trusted-host=pypi.douban.com/simple virtualenv

3.2, create, start, exit the virtual environment

Create a virtual environment using the default python version

[root@localhost ~]# virtualenv py01

Start the virtual environment

[root@localhost ~]# cd py01/bin/

[root@localhost bin]# source active

View python version

(py01) [root@localhost bin]# python

Exit Virtual Environment

(py01) [root@localhost bin]# deactivate

Specified python version to create a virtual environment (-p specifies the installation path of python)

[root@localhost ~]# virtualenv -p /usr/local/python3.7.7/bin/python3 py02

The remaining operations Ibid.

4, virtual environment management tool

4.1 Installation Kit

[root@localhost ~]# pip install -i https://pypi.douban.com/simple/ virtualenvwrapper

4.2, find the file

[root@localhost ~]# find / -name virtualenvwrapper.sh

/usr/bin/virtualenvwrapper.sh

4.3, adding environment variables

[root@localhost ~]# vim .bashrc

(Add content below the final edge)

export WORKON_HOME=$HOME/.virtualenvs
source /usr/bin/virtualenvwrapper.sh

4.4、重启机器

[root@localhost ~]# reboot

4.5、使用管理工具创建虚拟环境

①使用默认python版本创建

[root@localhost ~]# mkvirtualenv test01

②使用指定python版本创建

[root@localhost ~]# mkvirtualenv --python=/usr/local/python3.7.7/bin/python3 test02

4.6、进入虚拟环境

[root@localhost ~]# workon test01

查看python版本

(test01) [root@localhost ~]# python

Python 2.7.5 (default, Oct 30 2018, 23:45:53) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux2
Type "help", "copyright", "credits" or "license" for more information.

[root@localhost ~]# workon test02

查看python版本

(test02) [root@localhost ~]# python

Python 3.7.7 (default, Mar 27 2020, 12:29:36) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] on linux
Type "help", "copyright", "credits" or "license" for more information.

4.7、退出虚拟环境

(test01) [root@localhost ~]# deactivate

Guess you like

Origin www.cnblogs.com/Caesars/p/12585296.html