部署基于python的web发布环境(第二篇):环境部署3:centos7.5安装python,安装使用virtualenv

 

一、下载python

[root@web src]# pwd
/usr/src
​
[root@web src]# wget https://www.python.org/ftp/python/3.7.1/Python-3.7.1.tar.xz

  

二、安装依赖

[root@web src]# yum -y install gcc-* openssl-*  sqlite-devel libffi-devel

  

 

三、开始安装

[root@web src]# pwd
/usr/src
[root@web src]# ls
cmake-3.6.0-rc1  kernels       nginx-1.15.5
debug            mysql-5.7.29  Python-3.7.1.tar.xz
[root@web src]# tar xf Python-3.7.1.tar.xz
#开始配置
[root@web src]# cd Python-3.7.1
[root@web Python-3.7.1]# ls
aclocal.m4           Doc         m4               Parser         README.rst
CODE_OF_CONDUCT.rst  Grammar     Mac              PC             setup.py
config.guess         Include     Makefile.pre.in  PCbuild        Tools
config.sub           install-sh  Misc             Programs
configure            Lib         Modules          pyconfig.h.in
configure.ac         LICENSE     Objects          Python
[root@web Python-3.7.1]# 
./configure --enable-optimizations --with-openssl=/usr/bin/openssl
​
#开始编译
[root@web Python-3.7.1]# make
​
...
eld-initializers -Werror=implicit-function-declaration -fprofile-use -fprofile-correction  -I. -I./Include    -DPy_BUILD_CORE -o Programs/_testembed.o ./Programs/_testembed.c
gcc -pthread   -Xlinker -export-dynamic -o Programs/_testembed Programs/_testembed.o libpython3.7m.a -lpthread -ldl  -lutil   -lm  
make[1]: Leaving directory `/usr/src/Python-3.7.1'
#安装
[root@web Python-3.7.1]# make install
...
Looking in links: /tmp/tmp__poor6k
Collecting setuptools
Collecting pip
Installing collected packages: setuptools, pip
Successfully installed pip-10.0.1 setuptools-39.0.1
[root@web Python-3.7.1]# 

  



四、安装测试

[root@web Python-3.7.1]# python3
Python 3.7.1 (default, Feb 15 2020, 22:02:16) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 

  


默认安装路路径:/usr/local/lib/python3.7

 

五、安装\卸载第三方包

#升级pip工具

pip 是 Python 包管理工具,该工具提供了对Python 包的查找、下 载、安装、卸载的功能。

[root@web Python-3.7.1]# pip3 install --upgrade pip
pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.
Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError("Can't connect to HTTPS URL because the SSL module is not available.")': /simple/pip/
Retrying (Retry(total=3, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError("Can't connect to HTTPS URL because the SSL module is not available.")': /simple/pip/
​

  

出现ssl错误,修改解压出来的源码包中的Modules/Setup文件,取消其中的四行注释

 


[root@web Python-3.7.1]# pwd
/usr/src/Python-3.7.1
[root@web Python-3.7.1]# vi Modules/Setup
​
# Socket module helper for SSL support; you must comment out the other
# socket line above, and possibly edit the SSL variable:
#SSL=/usr/local/ssl
#_ssl _ssl.c \
#       -DUSE_SSL -I$(SSL)/include -I$(SSL)/include/openssl \
#       -L$(SSL)/lib -lssl -lcrypto
去掉上面四行的注释
即
SSL=/usr/local/ssl
_ssl _ssl.c \
       -DUSE_SSL -I$(SSL)/include -I$(SSL)/include/openssl \
       -L$(SSL)/lib -lssl -lcrypto

  


然后再次执行make make install

[root@web Python-3.7.1]# pwd
/usr/src/Python-3.7.1
[root@web Python-3.7.1]# make
...
[root@web Python-3.7.1]# make install
 

  

升级pip

[root@web Python-3.7.1]# pip3 install --upgrade pip
Collecting pip
  Downloading https://files.pythonhosted.org/packages/54/0c/d01aa759fdc501a58f431eb594a17495f15b88da142ce14b5845662c13f3/pip-20.0.2-py2.py3-none-any.whl (1.4MB)
    100% |████████████████████████████████| 1.4MB 125kB/s 
Installing collected packages: pip
  Found existing installation: pip 10.0.1
    Uninstalling pip-10.0.1:
      Successfully uninstalled pip-10.0.1
Successfully installed pip-20.0.2
[root@web Python-3.7.1]# 

  


 

 

使用pip安装、卸载ipython

#安装
[root@web Python-3.7.1]# pip3 install ipython
Collecting ipython
  Downloading ipython-7.12.0-py3-none-any.whl (777 kB)
     |████████████████████████████████| 777 kB 179 kB/s 
Collecting decorator
  Downloading decorator-4.4.1-py2.py3-none-any.whl (9.2 kB)
Requirement already satisfied: setuptools>=18.5 in /usr/local/lib/python3.7/site-packages (from ipython) (39.0.1)
Collecting jedi>=0.10
  Downloading jedi-0.16.0-py2.py3-none-any.whl (1.1 MB)
     |████████████████████████████████| 1.1 MB 242 kB/s 
Collecting pexpect; sys_platform != "win32"
  Downloading pexpect-4.8.0-py2.py3-none-any.whl (59 kB)
     |████████████████████████████████| 59 kB 231 kB/s 
Collecting pygments
  Downloading Pygments-2.5.2-py2.py3-none-any.whl (896 kB)
     |████████████████████████████████| 896 kB 133 kB/s 
Collecting pickleshare
  Downloading pickleshare-0.7.5-py2.py3-none-any.whl (6.9 kB)
Collecting backcall
  Downloading backcall-0.1.0.tar.gz (9.7 kB)
Collecting prompt-toolkit!=3.0.0,!=3.0.1,<3.1.0,>=2.0.0
  Downloading prompt_toolkit-3.0.3-py3-none-any.whl (348 kB)
     |████████████████████████████████| 348 kB 126 kB/s 
Collecting traitlets>=4.2
  Downloading traitlets-4.3.3-py2.py3-none-any.whl (75 kB)
     |████████████████████████████████| 75 kB 206 kB/s 
Collecting parso>=0.5.2
  Downloading parso-0.6.1-py2.py3-none-any.whl (97 kB)
     |████████████████████████████████| 97 kB 234 kB/s 
Collecting ptyprocess>=0.5
  Downloading ptyprocess-0.6.0-py2.py3-none-any.whl (39 kB)
Collecting wcwidth
  Downloading wcwidth-0.1.8-py2.py3-none-any.whl (17 kB)
Collecting six
  Downloading six-1.14.0-py2.py3-none-any.whl (10 kB)
Collecting ipython-genutils
  Downloading ipython_genutils-0.2.0-py2.py3-none-any.whl (26 kB)
Installing collected packages: decorator, parso, jedi, ptyprocess, pexpect, pygments, pickleshare, backcall, wcwidth, prompt-toolkit, six, ipython-genutils, traitlets, ipython
    Running setup.py install for backcall ... done
Successfully installed backcall-0.1.0 decorator-4.4.1 ipython-7.12.0 ipython-genutils-0.2.0 jedi-0.16.0 parso-0.6.1 pexpect-4.8.0 pickleshare-0.7.5 prompt-toolkit-3.0.3 ptyprocess-0.6.0 pygments-2.5.2 six-1.14.0 traitlets-4.3.3 wcwidth-0.1.8
​
#使用
[root@web Python-3.7.1]# ipython
Python 3.7.1 (default, Feb 15 2020, 22:39:51) 
Type 'copyright', 'credits' or 'license' for more information
IPython 7.12.0 -- An enhanced Interactive Python. Type '?' for help.
​
In [1]: print('hello world')                                                  
hello world
​
In [2]: exit()     
​
#卸载
[root@web Python-3.7.1]# pip3 uninstall ipython
Found existing installation: ipython 7.12.0
Uninstalling ipython-7.12.0:
  Would remove:
    /usr/local/bin/iptest
    /usr/local/bin/iptest3
    /usr/local/bin/ipython
    /usr/local/bin/ipython3
    /usr/local/lib/python3.7/site-packages/IPython/*
    /usr/local/lib/python3.7/site-packages/ipython-7.12.0.dist-info/*
    /usr/local/share/man/man1/ipython.1.gz
Proceed (y/n)? y
  Successfully uninstalled ipython-7.12.0
[root@web Python-3.7.1]# 
                     

  

六、安装、使用虚拟环境

virtualenv 是一个创建隔绝的Python环境的工具。virtualenv创建一个包含所有必要 的可执行文件的文件夹,用来使用Python工程所需的包。

pip3 install virtualenv

  

出现错误

[root@web Python-3.7.1]# pip3 install virtualenv
Collecting virtualenv
  WARNING: Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'NewConnectionError('<pip._vendor.urllib3.connection.VerifiedHTTPSConnection object at 0x7efd6303eba8>: Failed to establish a new connection: [Errno -2] Name or service not known')': /packages/a3/dd/e43866ac0d25cd1ecbde0814f6cd7372ea257fbdf443dc9f0a01740da93d/virtualenv-20.0.4-py2.py3-none-any.whl
  WARNING: Retrying (Retry(total=3, connect=None, read=None, redirect=None, status=None)) after connection broken by 'NewConnectionError('<pip._vendor.urllib3.connection.VerifiedHTTPSConnection object at 0x7efd6303e438>: Failed to establish a new connection: [Errno -2] Name or service not known')': /packages/a3/dd/e43866ac0d25cd1ecbde0814f6cd7372ea257fbdf443dc9f0a01740da93d/virtualenv-20.0.4-py2.py3-none-any.whl
  WARNING: Retrying (Retry(total=2, connect=None, read=None, redirect=None, status=None)) after connection broken by 'NewConnectionError('<pip._vendor.urllib3.connection.VerifiedHTTPSConnection object at 0x7efd6303ec18>: Failed to establish a new connection: [Errno -2] Name or service not known')': /packages/a3/dd/e43866ac0d25cd1ecbde0814f6cd7372ea257fbdf443dc9f0a01740da93d/virtualenv-20.0.4-py2.py3-none-any.whl
  WARNING: Retrying (Retry(total=1, connect=None, read=None, redirect=None, status=None)) after connection broken by 'NewConnectionError('<pip._vendor.urllib3.connection.VerifiedHTTPSConnection object at 0x7efd6303ed68>: Failed to establish a new connection: [Errno -2] Name or service not known')': /packages/a3/dd/e43866ac0d25cd1ecbde0814f6cd7372ea257fbdf443dc9f0a01740da93d/virtualenv-20.0.4-py2.py3-none-any.whl
  WARNING: Retrying (Retry(total=0, connect=None, read=None, redirect=None, status=None)) after connection broken by 'NewConnectionError('<pip._vendor.urllib3.connection.VerifiedHTTPSConnection object at 0x7efd6303edd8>: Failed to establish a new connection: [Errno -2] Name or service not known')': /packages/a3/dd/e43866ac0d25cd1ecbde0814f6cd7372ea257fbdf443dc9f0a01740da93d/virtualenv-20.0.4-py2.py3-none-any.whl
ERROR: Could not install packages due to an EnvironmentError: HTTPSConnectionPool(host='files.pythonhosted.org', port=443): Max retries exceeded with url: /packages/a3/dd/e43866ac0d25cd1ecbde0814f6cd7372ea257fbdf443dc9f0a01740da93d/virtualenv-20.0.4-py2.py3-none-any.whl (Caused by NewConnectionError('<pip._vendor.urllib3.connection.VerifiedHTTPSConnection object at 0x7efd6303ed30>: Failed to establish a new connection: [Errno -2] Name or service not known'))
​
[root@web Python-3.7.1]# 

  


 

解决方式换pip源,

临时使用豆瓣的源

pip3 install virtualenv -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com

  

永久修改

[root@web Python-3.7.1]# pip3 config set global.index-url https://mirrors.aliyun.com/pypi/simple/

  

 

安装成功

[root@web Python-3.7.1]# pip3 install virtualenv -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com
Looking in indexes: http://pypi.douban.com/simple/
Collecting virtualenv
  Downloading http://pypi.doubanio.com/packages/a3/dd/e43866ac0d25cd1ecbde0814f6cd7372ea257fbdf443dc9f0a01740da93d/virtualenv-20.0.4-py2.py3-none-any.whl (4.6 MB)
     |████████████████████████████████| 4.6 MB 426 kB/s 
Collecting importlib-metadata<2,>=0.12; python_version < "3.8"
  Downloading http://pypi.doubanio.com/packages/8b/03/a00d504808808912751e64ccf414be53c29cad620e3de2421135fcae3025/importlib_metadata-1.5.0-py2.py3-none-any.whl (30 kB)
Collecting distlib<1,>=0.3.0
  Downloading http://pypi.doubanio.com/packages/7d/29/694a3a4d7c0e1aef76092e9167fbe372e0f7da055f5dcf4e1313ec21d96a/distlib-0.3.0.zip (571 kB)
     |████████████████████████████████| 571 kB 1.1 MB/s 
Collecting filelock<4,>=3.0.0
  Downloading http://pypi.doubanio.com/packages/93/83/71a2ee6158bb9f39a90c0dea1637f81d5eef866e188e1971a1b1ab01a35a/filelock-3.0.12-py3-none-any.whl (7.6 kB)
Requirement already satisfied: six<2,>=1.9.0 in /usr/local/lib/python3.7/site-packages (from virtualenv) (1.14.0)
Collecting appdirs<2,>=1.4.3
  Downloading http://pypi.doubanio.com/packages/56/eb/810e700ed1349edde4cbdc1b2a21e28cdf115f9faf263f6bbf8447c1abf3/appdirs-1.4.3-py2.py3-none-any.whl (12 kB)
Collecting zipp>=0.5
  Downloading http://pypi.doubanio.com/packages/46/42/f2dd964b2a6b1921b08d661138148c1bcd3f038462a44019416f2342b618/zipp-2.2.0-py36-none-any.whl (4.6 kB)
Installing collected packages: zipp, importlib-metadata, distlib, filelock, appdirs, virtualenv
    Running setup.py install for distlib ... done
Successfully installed appdirs-1.4.3 distlib-0.3.0 filelock-3.0.12 importlib-metadata-1.5.0 virtualenv-20.0.4 zipp-2.2.0
[root@web Python-3.7.1]# 
​

  

 

使用虚拟环境

使用python3,创建一个叫web的虚拟环境

[root@web Python-3.7.1]# virtualenv -p python3 web
created virtual environment in 338ms CPython3Posix(dest=/usr/src/Python-3.7.1/web, clear=False, global=False) with seeder FromAppData pip=latest setuptools=latest wheel=latest app_data_dir=/root/.local/share/virtualenv/seed-v1 via=copy
[root@web Python-3.7.1]# 
​
[root@web Python-3.7.1]# ls
aclocal.m4           Include          Modules            Python
build                install-sh       Objects            python-config
CODE_OF_CONDUCT.rst  Lib              Parser             python-config.py
config.guess         libpython3.7m.a  PC                 python-gdb.py
config.log           LICENSE          PCbuild            README.rst
config.status        m4               profile-run-stamp  setup.py
config.sub           Mac              Programs           Tools
configure            Makefile         pybuilddir.txt     web
configure.ac         Makefile.pre     pyconfig.h
Doc                  Makefile.pre.in  pyconfig.h.in
Grammar              Misc             python
[root@web Python-3.7.1]# 
#可以看到目录中多了一个web文件夹

  


使虚拟环境生效,在此环境中装django

[root@web Python-3.7.1]# pwd
/usr/src/Python-3.7.1
[root@web Python-3.7.1]# source web/bin/activate
(web) [root@web Python-3.7.1]# pip3 install django==2.1.8
Looking in indexes: https://mirrors.aliyun.com/pypi/simple/
Collecting django==2.1.8
  Downloading https://mirrors.aliyun.com/pypi/packages/a9/e4/fb8f473fe8ee659859cb712e25222243bbd55ece7c319301eeb60ccddc46/Django-2.1.8-py3-none-any.whl (7.3 MB)
     |████████████████████████████████| 7.3 MB 282 kB/s 
Requirement already satisfied: pytz in /usr/src/Python-3.7.1/web/lib/python3.7/site-packages (from django==2.1.8) (2019.3)
Installing collected packages: django
Successfully installed django-2.1.8
(web) [root@web Python-3.7.1]#
​

  

 

使用django创建项目myweb

(web) [root@web Python-3.7.1]# ls
aclocal.m4           Include          Modules            Python
build                install-sh       Objects            python-config
CODE_OF_CONDUCT.rst  Lib              Parser             python-config.py
config.guess         libpython3.7m.a  PC                 python-gdb.py
config.log           LICENSE          PCbuild            README.rst
config.status        m4               profile-run-stamp  setup.py
config.sub           Mac              Programs           Tools
configure            Makefile         pybuilddir.txt     web
configure.ac         Makefile.pre     pyconfig.h
Doc                  Makefile.pre.in  pyconfig.h.in
Grammar              Misc             python
(web) [root@web Python-3.7.1]# django-admin.py
​

  

创建django项目

(web) [root@web Python-3.7.1]# django-admin.py startproject myweb
​
#增加了文件夹myweb
(web) [root@web Python-3.7.1]# ls
aclocal.m4           Include          Modules            python
build                install-sh       myweb              Python
CODE_OF_CONDUCT.rst  Lib              Objects            python-config
config.guess         libpython3.7m.a  Parser             python-config.py
config.log           LICENSE          PC                 python-gdb.py
config.status        m4               PCbuild            README.rst
config.sub           Mac              profile-run-stamp  setup.py
configure            Makefile         Programs           Tools
configure.ac         Makefile.pre     pybuilddir.txt     web
Doc                  Makefile.pre.in  pyconfig.h
Grammar              Misc             pyconfig.h.in
(web) [root@web Python-3.7.1]# 

  


修改项目中的settings.py中的allowed_host,使得任何ip都可以访问

(web) [root@web Python-3.7.1]# pwd
/usr/src/Python-3.7.1
(web) [root@web Python-3.7.1]# vi ./myweb/myweb/settings.py
​
...
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
​
ALLOWED_HOSTS = ['*']

  



启动django项目

(web) [root@web myweb]# pwd
/usr/src/Python-3.7.1/myweb
(web) [root@web myweb]# ls
manage.py  myweb
(web) [root@web myweb]# python3 manage.py runserver 192.168.199.169:8000
Performing system checks...
​
System check identified no issues (0 silenced).
​
You have 15 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.
​
February 15, 2020 - 15:48:01
Django version 2.1.8, using settings 'myweb.settings'
Starting development server at http://192.168.199.169:8000/
Quit the server with CONTROL-C.

  

 

 

可以通过访问

http://192.168.199.169:8000/

测试是否开启成功。

 

退出虚拟环境命令

deactivate

  

virtualenv使用

 

使用示例:建立一个叫env1的虚拟环境

mkdir ~/Envs/env1
virtualenv –p /usr/local/bin/python3

  

 

参数说明

$ virtualenv [OPTIONS] DEST_DIR 
选项: 
–version 
显示当前版本号。 
-h, –help 
显示帮助信息。 
-v, –verbose 
显示详细信息。 
-q, –quiet 
不显示详细信息。 
-p PYTHON_EXE, –python=PYTHON_EXE 
指定所用的python解析器的版本,比如 –python=python2.5 就使用2.5版本的解析器创建新的隔离环境。 默认使用的是当前系统安装(/usr/bin/python)的python解析器 
–clear 
清空非root用户的安装,并重头开始创建隔离环境。 
–no-site-packages (20.0.0+版本之后取消改参数)
令隔离环境不能访问系统全局的site-packages目录。 
–system-site-packages 
令隔离环境可以访问系统全局的site-packages目录。 
–unzip-setuptools 
安装时解压Setuptools或Distribute 
–relocatable 
重定位某个已存在的隔离环境。使用该选项将修正脚本并令所有.pth文件使用相当路径。 
–distribute 
使用Distribute代替Setuptools,也可设置环境变量VIRTUALENV_DISTRIBUTE达到同样效要。 
–extra-search-dir=SEARCH_DIRS 
用于查找setuptools/distribute/pip发布包的目录。可以添加任意数量的–extra-search-dir路径。 
–never-download 
禁止从网上下载任何数据。此时,如果在本地搜索发布包失败,virtualenv就会报错。 
–prompt==PROMPT 
定义隔离环境的命令行前缀。 

  

 

重要说明

当前时间2020年2月,virtualenv的最新版本,以20.0.0+为例,已经取消了–-no-site-packages参数,默认直接使用–-no-site-packages模式。有另外一个参数--system-site-packages,作用是可以访问源Python解释器的库,但是要慎用。所以,这里推荐,直接使用默认模式virtualenv -p xxx destination

posted on 2020-02-28 19:43  Nicholas--  阅读(659)  评论(0编辑  收藏  举报

导航