从0到1,公司内网离线安装python环境及依赖包

背景

公司网络经常因为安全问题,内网服务器不能直联外网,而且级别有申请外网权限,流程也很麻烦。
所以通常的解决方法是,直接将本地的Python环境,直接迁移到服务器中

常遇问题

  1. Python无法运行问题:本地环境,多为macOS或者window系统,因为Python的运行环境,由于CPU不同,直接copy到Linux的服务器上,是无法直接运行的
  2. 依赖包:Python脚本,通常用的依赖包较多,内网无法直接下载,需要有一个批量下载再导入到测试环境的办法。

解决思路

找一台可连接外网的Linux,通过“联网安装”,将所需Python、pip、依赖包,全部下载完成,然后再将Linux服务器中的环境,直接copy到目标内网服务器中,并完成环境变量的配置


本地联调环境

本文示例,采用macbook笔记本,使用PyCharm工具。
注:此步骤,如果本地联调已完成,可忽略。

  1. 打开PyCharm,新建项目,一般默认选择即可

    image.png

  2. 创建完成后,会生成以下目录


    image.png
  3. 脚本中缺少依赖包时,使用IDE工具的自动获取功能


    image.png
  4. 依赖包完成后,可以查看到在site-packages目录下,多了依赖包

    image.png

  5. 执行脚本,成功运行


    image.png

以下为本地环境重点

  1. 获取依赖包列表
(venv) BUG:SmsSessionIdPython herman$ pip --version
pip 10.0.1 from /Users/herman/Desktop/workspace/aibank_dtt/AutomaticTroubleShooting/SmsSessionIdPython/venv/lib/python3.7/site-packages/pip-10.0.1-py3.7.egg/pip (python 3.7)
(venv) BUG:SmsSessionIdPython herman$ pip freeze
Pillow==8.1.0
pytesseract==0.3.7
selenium==3.141.0
urllib3==1.26.2
You are using pip version 10.0.1, however version 20.3.3 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.

  1. 将依赖包列表,放入源码中(可参考以下目录]),创建requirements.txt,将列表信息存入
image.png

:Python项目目录结构可参照以下:

ProjectName
│ readme 项目说明文档
│ requirements.txt 存放依赖的外部Python包列表
│ setup.py 安装、部署、打包的脚本
├─ bin 存放脚本,执行文件等
│ └─ projectname
├─ docs 文档和配置
│ └─ abc.rst
│ └─ conf.py 配置文件
└─ projectname 工程源码(包括源码、测试代码等)
│ main.py 程序入口
│ init.py
└─ tests 测试代码
└─ test_main.py
└─ init.py
  1. 安装依赖包
pip install requirements.txt

:本地联调环境(macOSwindows),编译的内容,在Linux上无法使用,需要通过以上方法,将依赖包的内容导出,能用于在联网的Linux服务器中编译。

联网服务器安装

  1. 按照python或python3
[root@VM_0_2_centos python_script]# yum install python3
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
正在解决依赖关系
--> 正在检查事务
---> 软件包 python3.x86_64.0.3.6.8-18.el7 将被 安装
--> 正在处理依赖关系 python3-libs(x86-64) = 3.6.8-18.el7,它被软件包 python3-3.6.8-18.el7.x86_64 需要
--> 正在处理依赖关系 python3-setuptools,它被软件包 python3-3.6.8-18.el7.x86_64 需要
--> 正在处理依赖关系 python3-pip,它被软件包 python3-3.6.8-18.el7.x86_64 需要
--> 正在处理依赖关系 libpython3.6m.so.1.0()(64bit),它被软件包 python3-3.6.8-18.el7.x86_64 需要
--> 正在检查事务
---> 软件包 python3-libs.x86_64.0.3.6.8-18.el7 将被 安装
--> 正在处理依赖关系 libtirpc.so.1()(64bit),它被软件包 python3-libs-3.6.8-18.el7.x86_64 需要
---> 软件包 python3-pip.noarch.0.9.0.3-8.el7 将被 安装
---> 软件包 python3-setuptools.noarch.0.39.2.0-10.el7 将被 安装
--> 正在检查事务
---> 软件包 libtirpc.x86_64.0.0.2.4-0.16.el7 将被 安装
--> 解决依赖关系完成

依赖关系解决

==============================================================================================================================
 Package                             架构                    版本                              源                        大小
==============================================================================================================================
正在安装:
 python3                             x86_64                  3.6.8-18.el7                      updates                   70 k
为依赖而安装:
 libtirpc                            x86_64                  0.2.4-0.16.el7                    os                        89 k
 python3-libs                        x86_64                  3.6.8-18.el7                      updates                  6.9 M
 python3-pip                         noarch                  9.0.3-8.el7                       os                       1.6 M
 python3-setuptools                  noarch                  39.2.0-10.el7                     os                       629 k

事务概要
==============================================================================================================================
安装  1 软件包 (+4 依赖软件包)

总下载量:9.3 M
安装大小:48 M
Is this ok [y/d/N]: y
Downloading packages:
(1/5): python3-3.6.8-18.el7.x86_64.rpm                                                                 |  70 kB  00:00:00
(2/5): libtirpc-0.2.4-0.16.el7.x86_64.rpm                                                              |  89 kB  00:00:00
(3/5): python3-setuptools-39.2.0-10.el7.noarch.rpm                                                     | 629 kB  00:00:00
(4/5): python3-libs-3.6.8-18.el7.x86_64.rpm                                                            | 6.9 MB  00:00:01
(5/5): python3-pip-9.0.3-8.el7.noarch.rpm                                                              | 1.6 MB  00:00:01
------------------------------------------------------------------------------------------------------------------------------
总计                                                                                          6.9 MB/s | 9.3 MB  00:00:01
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  正在安装    : libtirpc-0.2.4-0.16.el7.x86_64                                                                            1/5
  正在安装    : python3-setuptools-39.2.0-10.el7.noarch                                                                   2/5
  正在安装    : python3-pip-9.0.3-8.el7.noarch                                                                            3/5
  正在安装    : python3-3.6.8-18.el7.x86_64                                                                               4/5
  正在安装    : python3-libs-3.6.8-18.el7.x86_64                                                                          5/5
  验证中      : libtirpc-0.2.4-0.16.el7.x86_64                                                                            1/5
  验证中      : python3-setuptools-39.2.0-10.el7.noarch                                                                   2/5
  验证中      : python3-libs-3.6.8-18.el7.x86_64                                                                          3/5
  验证中      : python3-3.6.8-18.el7.x86_64                                                                               4/5
  验证中      : python3-pip-9.0.3-8.el7.noarch                                                                            5/5

已安装:
  python3.x86_64 0:3.6.8-18.el7

作为依赖被安装:
  libtirpc.x86_64 0:0.2.4-0.16.el7               python3-libs.x86_64 0:3.6.8-18.el7      python3-pip.noarch 0:9.0.3-8.el7
  python3-setuptools.noarch 0:39.2.0-10.el7

完毕!
  1. 安装pip
    使用脚本安装和升级pip
[root@VM_0_2_centos python_script]# wget https://bootstrap.pypa.io/get-pip.py
--2021-01-20 17:36:09--  https://bootstrap.pypa.io/get-pip.py
正在解析主机 bootstrap.pypa.io (bootstrap.pypa.io)... 151.101.228.175, 2a04:4e42:1a::175
正在连接 bootstrap.pypa.io (bootstrap.pypa.io)|151.101.228.175|:443... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:1886796 (1.8M) [text/x-python]
正在保存至: “get-pip.py”

100%[====================================================================================>] 1,886,796   13.0KB/s 用时 91s

2021-01-20 17:37:41 (20.4 KB/s) - 已保存 “get-pip.py” [1886796/1886796])

运行pip获取的脚本get-pip.py

[root@VM_0_2_centos python_script]# python3 get-pip.py
Collecting pip
  Downloading pip-20.3.3-py2.py3-none-any.whl (1.5 MB)
     |████████████████████████████████| 1.5 MB 34 kB/s
Collecting wheel
  Downloading wheel-0.36.2-py2.py3-none-any.whl (35 kB)
Installing collected packages: pip, wheel
  Attempting uninstall: pip
    Found existing installation: pip 9.0.3
    Uninstalling pip-9.0.3:
      Successfully uninstalled pip-9.0.3
Successfully installed pip-20.3.3 wheel-0.36.2

查看已安装的pipi版本pip -V

[root@VM_0_2_centos python_script]# pip -V
pip 20.3.3 from /usr/local/lib/python3.6/site-packages/pip (python 3.6)
  1. 安装依赖包
    使用pip按照Python依赖包pip install xxxx
    本文以requirements.txt中的清单为例,使用pip install -r requirements.txt
[root@VM_0_2_centos python_script]# pip install -r requirements.txt
Collecting Pillow==8.1.0
  Downloading Pillow-8.1.0-cp36-cp36m-manylinux1_x86_64.whl (2.2 MB)
     |████████████████████████████████| 2.2 MB 7.7 kB/s
Collecting pytesseract==0.3.7
  Downloading pytesseract-0.3.7.tar.gz (13 kB)
Collecting selenium==3.141.0
  Downloading selenium-3.141.0-py2.py3-none-any.whl (904 kB)
     |████████████████████████████████| 904 kB 5.1 kB/s
Collecting urllib3==1.26.2
  Downloading urllib3-1.26.2-py2.py3-none-any.whl (136 kB)
     |████████████████████████████████| 136 kB 9.8 kB/s
Building wheels for collected packages: pytesseract
  Building wheel for pytesseract (setup.py) ... done
  Created wheel for pytesseract: filename=pytesseract-0.3.7-py2.py3-none-any.whl size=13945 sha256=f3a1e1961291425462b58cd6f51ca2e96b7f42b8914956a80fcf3e49e1a5b24f
  Stored in directory: /root/.cache/pip/wheels/c1/ca/d1/14e0dabe6f772bb5b45e006111268d943d4196fcb8798e3bf8
Successfully built pytesseract
Installing collected packages: urllib3, Pillow, selenium, pytesseract
Successfully installed Pillow-8.1.0 pytesseract-0.3.7 selenium-3.141.0 urllib3-1.26.2

安装依赖包问题

  1. 版本不符
    依赖包经常会因为Python版本操作系统的不同,导致安装不上,通常报错:
[root@bz4autdttap1003 packages]# pip install Pillow-8.1.0-cp36-cp36m-manylinux1_x86_64.whl
Pillow-8.1.0-cp36-cp36m-manylinux1_x86_64.whl is not a supported wheel on this platform.

· 这里有一个小知识点(新手扫盲)

wheel的命名:包版本号-Python版本号-none或非官方Python包-操作系统.whl。例如:Pillow-8.1.0-cp36-cp36m-manylinux1_x86_64.whl

  1. 相对应的包可以从官网下载:https://pypi.org/project/Cython/#files
    image.png

找到相对应的whl包以后,上传到packages目录下,执行pip install --no-index --find-links=/home/bxapp/packages -r requirments.txt安装即可。

[root@bz4autdttap1003 packages]# pip install --no-index --find-links=/home/bxapp/packages  -r requirments.txt
Looking in links: /home/bxapp/packages
Collecting Cython==0.29.21 (from -r requirments.txt (line 1))
Collecting Pillow==8.1.0 (from -r requirments.txt (line 2))
Collecting pytesseract==0.3.7 (from -r requirments.txt (line 3))
Collecting selenium==3.141.0 (from -r requirments.txt (line 4))
Collecting urllib3==1.26.2 (from -r requirments.txt (line 5))
Installing collected packages: Cython, Pillow, pytesseract, urllib3, selenium
  Running setup.py install for pytesseract ... done
Successfully installed Cython-0.29.21 Pillow-8.1.0 pytesseract-0.3.7 selenium-3.141.0 urllib3-1.26.2
[root@bz4autdttap1003 packages]# ll
total 7340
drwxr-xr-x 7 root  root     4096 Jun 29  2016 activate-power-mode
lrwxrwxrwx 1 root  root       33 Jan 25 11:51 cobol-syntax -> /Users/fenglu/github/cobol-syntax
-rw-rw-r-- 1 bxapp bxapp 1969113 Jan 25 15:06 Cython-0.29.21-cp37-cp37m-manylinux1_x86_64.whl
drwxr-xr-x 5 root  root     4096 Jun  4  2016 language-cobol
drwxr-xr-x 7 root  root     4096 Jun  4  2016 multi-wrap-guide
-rw-r--r-- 1 root  root  2226983 Jan 21 16:01 Pillow-8.1.0-cp36-cp36m-manylinux1_x86_64.whl
-rw-rw-r-- 1 bxapp bxapp 2227041 Jan 25 15:06 Pillow-8.1.0-cp37-cp37m-manylinux1_x86_64.whl
-rw-r--r-- 1 root  root    13715 Jan 21 16:01 pytesseract-0.3.7.tar.gz
-rw-r--r-- 1 root  root       60 Jun  4  2016 README.md
drwxr-xr-x 7 root  root     4096 Jul 14  2016 Remote-FTP
-rw-r--r-- 1 root  root       83 Jan 25 15:01 requirments.txt
-rw-r--r-- 1 root  root   904577 Jan 21 16:01 selenium-3.141.0-py2.py3-none-any.whl
-rw-r--r-- 1 root  root   136770 Jan 21 16:01 urllib3-1.26.2-py2.py3-none-any.whl
drwxr-xr-x 7 root  root     4096 Jun 29  2016 vim-mode

————————————————————
————————————————————

题外话备忘

Linux上跑chromedriver,需要先安装浏览器yum install chromium,还需要安装``

[root@VM_0_2_centos python_script]# yum install chromium
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
正在解决依赖关系
--> 正在检查事务
---> 软件包 chromium.x86_64.0.87.0.4280.88-1.el7 将被 安装
--> 正在处理依赖关系 chromium-common(x86-64) = 87.0.4280.88-1.el7,它被软件包 chromium-87.0.4280.88-1.el7.x86_64 需要
…………………………
……………………………
已安装:
  chromium.x86_64 0:87.0.4280.88-1.el7

作为依赖被安装:
  ……………………
  rest.x86_64 0:0.8.1-2.el7                                      setools-libs.x86_64 0:3.3.8-4.el7
  sound-theme-freedesktop.noarch 0:0.8-3.el7                     xkeyboard-config.noarch 0:2.24-1.el7
…………………………
作为依赖被升级:
  atk.x86_64 0:2.28.1-2.el7                 audit.x86_64 0:2.8.5-4.el7               audit-libs.i686 0:2.8.5-4.el7
  audit-libs.x86_64 0:2.8.5-4.el7           avahi-autoipd.x86_64 0:0.6.31-20.el7     avahi-libs.x86_64 0:0.6.31-20.el7
……………………
完毕!

还需要安装yum install GConf2

[root@VM_0_2_centos python_script]# yum install GConf2
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
正在解决依赖关系
--> 正在检查事务
---> 软件包 GConf2.x86_64.0.3.2.6-8.el7 将被 安装
--> 解决依赖关系完成

依赖关系解决

==============================================================================================================================
 Package                      架构                         版本                                源                        大小
==============================================================================================================================
正在安装:
 GConf2                       x86_64                       3.2.6-8.el7                         os                       1.0 M

事务概要
==============================================================================================================================
安装  1 软件包

总下载量:1.0 M
安装大小:6.3 M
Is this ok [y/d/N]: y
Downloading packages:
GConf2-3.2.6-8.el7.x86_64.rpm                                                                          | 1.0 MB  00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  正在安装    : GConf2-3.2.6-8.el7.x86_64                                                                                 1/1
  验证中      : GConf2-3.2.6-8.el7.x86_64                                                                                 1/1

已安装:
  GConf2.x86_64 0:3.2.6-8.el7

完毕!
  1. 执行脚本,如果报错
[root@VM_0_2_centos python_script]# python3 SmsVerificationCode.py
dir_path = /home/herman/python_script
dir.path = /home/herman/python_script/chromedriver
Traceback (most recent call last):
  File "SmsVerificationCode.py", line 19, in 
    driver = webdriver.Chrome(executable_path="/home/herman/python_script/chromedriver")
  File "/usr/local/lib/python3.6/site-packages/selenium/webdriver/chrome/webdriver.py", line 81, in __init__
    desired_capabilities=desired_capabilities)
  File "/usr/local/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 157, in __init__
    self.start_session(capabilities, browser_profile)
  File "/usr/local/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 252, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
  File "/usr/local/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "/usr/local/lib/python3.6/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: exited abnormally.
  (unknown error: DevToolsActivePort file doesn't exist)
  (The process started from chrome location /usr/bin/chromium-browser is no longer running, so ChromeDriver is assuming that Chrome has crashed.)

你可能感兴趣的:(从0到1,公司内网离线安装python环境及依赖包)