CentOS7 offline deployment of Python project

1 Introduction

The main process is as follows:
(1) Create a project environment and generate a requirements.txt file. (If there is one, you can jump out)
(2) Create a new test server with the same operating system as the target machine and python environment, and download the third-party dependency packages required by the Python project.
(3) Download dependent files from the test server and upload them to the offline server.
(4) Install the Python offline dependency package and run the Python project.

PS1: [pip3] in this article is set by the project environment. If you are prompted that there is no such command locally, try the pip command.
PS2: Offline installation python tutorial address

我以 window系统下开发环境,部署到 centos7 离线服务器上。

2. Create a project environment on the test server

如果已有requirements.txt,跳过本章节。
This project mainly uses pipenv to create a project virtual environment. If you are interested, please refer to pipenv-tutorial.
Since this project does not create a process project virtual environment, the requirements.txt file cannot be generated. The first step is to sort out the project virtual environment and prepare for generating the requirements.txt file.

(1) Create a project virtual environment

# 安装虚拟环境
pipenv install
# 进入虚拟环境
pipenv shell 

(2) Reinstall the third-party dependent packages in turn

Note: Execute the program repeatedly, and install the dependent packages sequentially through pipenv install until it does not appear

# 安装第三方依赖包命令
pipenv install <package 第三方依赖包> --skip-lock

(3) Generate requirements.txt

# 生成requirements.txt
pipenv lock                                # 生成速度慢,耐心等待
pipenv requirements > requirements.txt

3. Download the third-party dependency package in the virtual machine

According to the target server, create the same system environment and python environment virtual machine. The main reason is that there are many differences between the dependent packages of the window development environment and the centos7 environment of the target server. Try to download the dependent packages in the same environment to avoid strange errors. h
(Note: offline installation python tutorial address )

# 1.打开项目所在地址
cd <project_path>                               # project_path为项目地址

# 2.下载第三方依赖包
pip3 download pipenv -d packages                # 下载pipenv依赖包,保存至packages文件夹,用于创建离线环境
pip3 download -d packages -r requirements.txt   # 下载requirements.txt相关依赖包

# 3.安装pipenv依赖包,进入项目虚拟环境
pip3 install pipenv --no-index --find-links=packages   # 从packages文件夹,安装pipenv
ln -s /usr/local/python3/bin/pipenv /usr/local/bin/pipenv  # 创建pipenv指令
pipenv shell                                               # 进入虚拟环境

# 4.通过requirements.txt 批量安装依赖包
pipenv run pip3 install --no-index --find-links=packages -r requirements.txt

# 5.测试运行
# 在虚拟环境下,执行你所需 ***.py 文件,运行成功则ok。详见下图

insert image description here

Figure 1: Execute python files in a virtual environment

4. Transfer the third-party dependency package to the target server

  1. Download source code and third-party dependencies from the test server
  2. Use the WinSCP.exe tool to upload offline files to the server. (Note: /home/python3 folder, if it does not exist, please create it manually)

insert image description here
(PS: There are offline third-party dependent packages in the packages folder, please check the quantity after the download is complete)

Figure 2: Download files from the test server to the local

3. Upload from local to the target server
Upload to the /home/python3/Smart_Construction-master folder of the target server (Note: upload to your project folder as needed)

insert image description here

5. Install the python project offline on the target server

# 1.打开项目文件夹
cd /home/python3/Smart_Construction-master

# 2.安装pipenv依赖包
pip3 install pipenv --no-index --find-links=packages   # 从packages文件夹,安装pipenv

# 3.进入项目虚拟环境
ln -s /usr/local/python3/bin/pipenv /usr/local/bin/pipenv  # 创建pipenv指令
pipenv shell                                               # 进入虚拟环境

# 4.通过requirements.txt 批量安装依赖包
pipenv run pip3 install --no-index --find-links=packages -r requirements.txt

# 5.运行程序,搞定收工。

Guess you like

Origin blog.csdn.net/piao110liang/article/details/127923185