Python virtualenv virtual environment

In the development of Python applications, system installation Python3 only one version: 3.4. All third-party packages will be pipinstalled to Python3 the site-packagesdirectory.

If we want to develop multiple applications simultaneously, and that these applications will share a Python, it is installed in the system's Python 3. If the application A needs jinja 2.7, while application B needs jinja 2.6 how to do?

In this case, each application may need to have their own set of "independence" of the Python runtime environment. virtualenv is used to create a "quarantine" of the Python runtime environment for an application.

First, we use the pipinstallation virtualenv:

[root@bogon ~]# pip3 install virtualenv
Collecting virtualenv
Downloading https://files.pythonhosted.org/packages/8b/12/8d4f45b8962b03ac9efefe5ed5053f6b29334d83e438b4fe379d21c0cb8e/virtualenv-16.7.5-py2.py3-none-any.whl (3.3MB)
|████████████████████████████████| 3.3MB 615kB/s
Installing collected packages: virtualenv
Successfully installed virtualenv-16.7.

After the installation is complete, you can look

[root@bogon bin]# ls
2to3 django-admin.py idle3.6 pip3.6 python3 python3.6m-config uwsgi wsdump.py
2to3-3.6 docker-compose jsonschema __pycache__ python3.6 python3-config uwsgi.log
chardetect easy_install-3.6 pip pydoc3 python3.6-config pyvenv uwsgi.pid
django-admin idle3 pip3 pydoc3.6 python3.6m pyvenv-3.6 virtualenv

After installation is complete check the version using virtualenv --version

[root @ bogon bin] # virtualenv --version
bash: virtualenv: command not found ...
[root @ bogon bin] #

Calm, because I'm not using that comes Python2, with 3, yum these needs with a Python2, so my default or Python2

Next, add a soft link

[root@bogon bin]# ln -s /usr/local/python3/bin/virtualenv /usr/bin/virtualevn
[root@bogon bin]# virtualevn --version
16.7.5
[root@bogon bin]#

Get

Then, suppose we want to develop a new project that requires a separate set of Python runtime environment, you can do this:

First, create a directory:

[root@bogon shaobo]# mkdir ven

Next, create a stand-alone Python runtime environment, named venv:

 Well, toss for a long time has not suggesting that configparser module can not be found, there is no way

Only to re-install the Python

Before the first clean uninstall, delete methods, I will delete all the files all


1. First install some dependencies (if there are dependency problems prompted to install) I encountered:
yum -y groupinstall "Development Tools"
yum -y install zlib-devel bzip2-devel OpenSSL-devel ncurses-devel SQLite-devel readline- devel tk-devel gdbm-devel db4 -devel libpcap-devel xz-devel libffi-devel

 

2. The official website Download Source Package
wget https://www.python.org/ftp/python/3.7.1/Python-3.7.1.tar.xz


3. Unzip the file
the xz -d Python-3.7.1.tar.xz
tar -xvf Python-3.7.1.tar


4. Enter file directory
CD-3.7.1 the Python
 
5. The installation starts compiling
./configure --prefix = / usr / local / python3.7
the make
the make the install

6. Let centos7 use python3.7

ln -s /usr/local/python3.7/bin/python3 /usr/bin/python3

ln -s /usr/local/python3.7/bin/pip3 /usr/bin/pip3

This installation is complete.

 Repeat to install the top virtualenv

pip3 install virtualenv

I do not know what this is, with reference to others

pip3 install gunicorn

[root@bogon testvenv]# virtualenv --no-site-packages -p /usr/bin/python3 venv
Running virtualenv with interpreter /usr/bin/python3
Already using interpreter /usr/bin/python3
Using base prefix '/usr/bin/python3.7'
New python executable in /opt/shaobo/testvenv/venv/bin/python3
Also creating executable in /opt/shaobo/testvenv/venv/bin/python
Installing setuptools, pip, wheel...
done.
[root@bogon testvenv]# ls

Get excited yo

The third step: activate the virtual environment

First go to the bin directory

[root@bogon venv]# cd bin/
[root@bogon bin]# ls
activate activate.fish activate_this.py easy_install pip pip3.7 python3 python-config
activate.csh activate.ps1 activate.xsh easy_install-3.7 pip3 python python3.7 wheel

Then activate the virtual environment

[root@bogon bin]# source ./activate
(venv) [root@bogon bin]#

We try to install a request

(venv) [root@bogon bin]# pip3 install request
Collecting request
Downloading https://files.pythonhosted.org/packages/f1/27/7cbde262d854aedf217061a97020d66a63163c5c04e0ec02ff98c5d8f44e/request-2019.4.13.tar.gz
Collecting get (from request)

Then we enter site_packages view the installed modules

(venv) [root@bogon python3.7]# cd site-packages/
(venv) [root@bogon site-packages]# ls
easy_install.py pkg_resources __pycache__ setuptools
get post query_string setuptools-41.2.0.dist-info
get-2019.4.13.dist-info post-2019.4.13.dist-info query_string-2019.4.13.dist-info wheel
pip public request wheel-0.33.6.dist-info
pip-19.2.3.dist-info public-2019.4.13.dist-info request-2019.4.13.dist-info
(venv) [root@bogon site-packages]#

Exit Virtual Environment

(venv) [root@bogon bin]# deactivate
[root@bogon bin]#

ok, the end!

Guess you like

Origin www.cnblogs.com/leslie003/p/11499041.html