python project to solve sandbox dependence problem in production environment without external network
In our actual production project deployment process, such as banking, government intranet, can not access certain dependency sources. Considering the actual situation, we will see how to solve this problem.
development environment
Establishing Project Development Path
mkdir -p /data/python/project/
Let's see if there is a pip command tool.
pip's command-line installation can be seen on the official website link:
https://pip.pypa.io/en/stable/installing/#installing-with-get-pip-py
root@-dev:/data/python/project# whereis pip pip: /usr/bin/pip /usr/local/bin/pip /usr/local/bin/pip2.7 /root/miniconda2/bin/pip /usr/share/man/man1/pip.1.gz
Installation and development of Python virtual environment sandbox development tools
pip install virtualenv #install
Enter the Development Catalogue
cd /data/python/project/
Creating virtual sandbox virtual alenv
virtualenv venv
View Sandbox Catalogue
Entering the virtual environment, we can avoid polluting the python environment of the system by entering the sandbox environment and executing the project code we developed.
root@dev:/data/python/project# source venv/bin/activate (venv) root@dev:/data/python/project#
When you see a sandbox environment, the command line headers with a sandbox directory name [venv]
Exit from Virtual Environment
deactivate
View the differences between virtual python environment and system python environment
(venv) root@dev:/data/python/project# pwd /data/python/project (venv) root@dev:/data/python/project# which python /data/python/project/venv/bin/python (venv) root@dev:/data/python/project# python -V Python 2.7.12 (venv) root@dev:/data/python/project# deactivate root@dev:/data/python/project# which python /root/miniconda2/bin/python root@dev:/data/python/project# python -V Python 2.7.13 :: Continuum Analytics, Inc. root@dev:/data/python/project#
You can see that both python path calls and versions are different
We installed some development modules in the virtual python environment, such as Flask.
(venv) root@dev:/data/python/project# pip install Flask Collecting Flask Using cached Flask-0.12.2-py2.py3-none-any.whl Collecting itsdangerous>=0.21 (from Flask) Collecting click>=2.0 (from Flask) Using cached click-6.7-py2.py3-none-any.whl Collecting Werkzeug>=0.7 (from Flask) Using cached Werkzeug-0.12.2-py2.py3-none-any.whl Collecting Jinja2>=2.4 (from Flask) Using cached Jinja2-2.9.6-py2.py3-none-any.whl Collecting MarkupSafe>=0.23 (from Jinja2>=2.4->Flask) Installing collected packages: itsdangerous, click, Werkzeug, MarkupSafe, Jinja2, Flask Successfully installed Flask-0.12.2 Jinja2-2.9.6 MarkupSafe-1.0 Werkzeug-0.12.2 click-6.7 itsdangerous-0.24 (venv) root@dev:/data/python/project#
In order to transplant these required execution modules in the non-extranet environment, the code is first developed in the virtual sandbox environment, and then packaged and deployed.
We pack the whole virtual ALV environment related projects, and generate a complete project package with execution environment. After decompression, we use it, that is, the above development project directory / data/python/project whole package compression.
Use the virtualenv --relocatable instruction to modify the ENV execution environment with variable locations.
Practical operation,
Or project directory / data/python/project
cd /data/python/project
View the virtual environment pip source code before virtualenv -- relocatable. / venv execution
root@dev:/data/python/project# ll total 12 drwxr-xr-x 3 root root 4096 8 May 15:08 ./ drwxr-xr-x 3 root root 4096 8 May 15:08 ../ drwxr-xr-x 6 root root 4096 8 May 15:08 venv/ root@dev:/data/python/project# root@dev:/data/python/project# pwd /data/python/project root@dev:/data/python/project# cat venv/bin/pip #!/data/python/project/venv/bin/python ###Focus on this line of code, which is the python reference path before executing virtualenv -- relocatable. / ven # -*- coding: utf-8 -*- import re import sys from pip import main if __name__ == '__main__': sys.argv[0] = re.sub(r'(-script\.pyw?|\.exe)?$', '', sys.argv[0]) sys.exit(main()) root@dev:/data/python/project#
View the virtual environment pip source code after virtualenv -- relocatable. / venv execution
root@dev:/data/python/project# pwd /data/python/project root@dev:/data/python/project# ll total 12 drwxr-xr-x 3 root root 4096 8 May 15:08 ./ drwxr-xr-x 3 root root 4096 8 May 15:08 ../ drwxr-xr-x 6 root root 4096 8 May 15:08 venv/ root@dev:/data/python/project# virtualenv --relocatable ./venv/ Making script /data/python/project/venv/bin/pip relative Making script /data/python/project/venv/bin/pip2 relative Making script /data/python/project/venv/bin/python-config relative Making script /data/python/project/venv/bin/easy_install relative Making script /data/python/project/venv/bin/wheel relative Making script /data/python/project/venv/bin/pip2.7 relative Making script /data/python/project/venv/bin/easy_install-2.7 relative root@dev:/data/python/project# cat venv/bin/pip #!/usr/bin/env python2.7 ###Focus on this line of code, which is the python reference path after executing virtualenv -- relocatable. / ven import os; activate_this=os.path.join(os.path.dirname(os.path.realpath(__file__)), 'activate_this.py'); exec(compile(open(activate_this).read(), activate_this, 'exec'), dict(__file__=activate_this)); del os, activate_this # -*- coding: utf-8 -*- import re import sys from pip import main if __name__ == '__main__': sys.argv[0] = re.sub(r'(-script\.pyw?|\.exe)?$', '', sys.argv[0]) sys.exit(main())root@dev:/data/python/project#
You can see that after virtualenv --relocatable execution, the python path invoked by the pip command in the virtual sandbox environment has changed.
Deploy the project
After the above execution of virtualenv --relocatable
-
The virtual sandbox venv in the development environment is packaged and compressed.
tar -cvf venv.tar venv/
-
Draw project code to the online production environment directory / app/proj1
Then throw venv.tar into the / app/proj1 directory and unzip it. Prepare for solving project dependencies.
-
Enter the / app/proj1/venv directory
A key:
Modify the active file in bin directory
vim /app/proj1/venv/bin/activeChange the variable name: VIRTUAL_ENV from the original packaged / data/python/project/venv to the actual / app/proj1/venv
*** So far, the whole project relies on the solution, and the *** python sandbox environment is transplanted from the development environment to the production environment.