Python
1 前言
一个问题,一篇文章,一出故事。
由于笔者今天接到部署Python的应用的工作任务,于是整理此文。
2 最佳实践
2.1 编译安装特定的版本
2.1.1 安装Python
2.1.2 检查是否满足版本需求
python -V
可见如下显示,
Python 3.9.18
2.2 部署应用
2.2.1 创建运行用户
useradd -d /data/python python su - python
2.2.2 创建虚拟环境
python -m venv venv
激活虚拟环境
source venv/bin/activate
进入程序目录
unzip cv_flask_web.zip cd cv_flask_web
2.2.3 安装依赖关系
pip install -r requirements.txt
2.2.4 尝试启动程序
python yolo_webapp.py
如果遇到如下错误提示,
Traceback (most recent call last): File "/data/python/cv_flask_web/yolo_webapp.py", line 1, in from flask import Flask,render_template,Response,request ModuleNotFoundError: No module named 'flask'
你可能需要使用如下命令解决,
pip install flask
如果遇到如下错误提示,
Traceback (most recent call last): File "/data/python/cv_flask_web/yolo_webapp.py", line 2, in import cv2 ModuleNotFoundError: No module named 'cv2'
你可能需要使用如下命令解决,
pip install opencv-python
如果遇到如下错误提示,
Traceback (most recent call last): File "/data/python/cv_flask_web/yolo_webapp.py", line 2, in import cv2 File "/data/python/venv/lib64/python3.9/site-packages/cv2/__init__.py", line 181, in bootstrap() File "/data/python/venv/lib64/python3.9/site-packages/cv2/__init__.py", line 153, in bootstrap native_module = importlib.import_module("cv2") File "/usr/lib64/python3.9/importlib/__init__.py", line 127, in import_module return _bootstrap._gcd_import(name[level:], package, level) ImportError: libGL.so.1: cannot open shared object file: No such file or directory
你可能需要使用如下命令解决,
dnf install -y mesa-libGL
如果遇到如下错误提示,
Traceback (most recent call last): File "/data/python/cv_flask_web/yolo_webapp.py", line 3, in import torch ModuleNotFoundError: No module named 'torch'
你可能需要使用如下命令解决,
pip install torch
如果遇到如下错误提示,
Traceback (most recent call last): File "/data/python/cv_flask_web/yolo_webapp.py", line 25, in model = torch.hub.load('ultralytics/yolov5', 'custom', path = 'clean_rm_r5.pt') File "/data/python/venv/lib64/python3.9/site-packages/torch/hub.py", line 638, in load repo_or_dir = _get_cache_or_reload( File "/data/python/venv/lib64/python3.9/site-packages/torch/hub.py", line 242, in _get_cache_or_reload _check_repo_is_trusted( File "/data/python/venv/lib64/python3.9/site-packages/torch/hub.py", line 312, in _check_repo_is_trusted Path(filepath).touch() File "/usr/lib64/python3.9/pathlib.py", line 1084, in __new__ raise NotImplementedError("cannot instantiate %r on your system" NotImplementedError: cannot instantiate 'WindowsPath' on your system
你可能需要使用如下命令解决,
cp /data/python/cv_flask_web/yolo_webapp.py /data/python/cv_flask_web/yolo_webapp.py.save vim /data/python/cv_flask_web/yolo_webapp.py
修改如下代码,
#pathlib.PosixPath = pathlib.WindowsPath pathlib.WindowsPath = pathlib.PosixPath
如果遇到如下错误提示,
Using cache found in /data/python/.cache/torch/hub/ultralytics_yolov5_master Traceback (most recent call last): File "/data/python/cv_flask_web/yolo_webapp.py", line 26, in model = torch.hub.load('ultralytics/yolov5', 'custom', path = 'clean_rm_r5.pt') File "/data/python/venv/lib64/python3.9/site-packages/torch/hub.py", line 647, in load model = _load_local(repo_or_dir, model, *args, **kwargs) File "/data/python/venv/lib64/python3.9/site-packages/torch/hub.py", line 676, in _load_local model = entry(*args, **kwargs) File "/data/python/.cache/torch/hub/ultralytics_yolov5_master/hubconf.py", line 135, in custom return _create(path, autoshape=autoshape, verbose=_verbose, device=device) File "/data/python/.cache/torch/hub/ultralytics_yolov5_master/hubconf.py", line 54, in _create from models.common import AutoShape, DetectMultiBackend File "/data/python/.cache/torch/hub/ultralytics_yolov5_master/models/common.py", line 18, in import pandas as pd ModuleNotFoundError: No module named 'pandas'
你可能需要使用如下命令解决,
pip install pandas
如果遇到如下错误提示,
Using cache found in /data/python/.cache/torch/hub/ultralytics_yolov5_master Traceback (most recent call last): File "/data/python/cv_flask_web/yolo_webapp.py", line 26, in model = torch.hub.load('ultralytics/yolov5', 'custom', path = 'clean_rm_r5.pt') File "/data/python/venv/lib64/python3.9/site-packages/torch/hub.py", line 647, in load model = _load_local(repo_or_dir, model, *args, **kwargs) File "/data/python/venv/lib64/python3.9/site-packages/torch/hub.py", line 676, in _load_local model = entry(*args, **kwargs) File "/data/python/.cache/torch/hub/ultralytics_yolov5_master/hubconf.py", line 135, in custom return _create(path, autoshape=autoshape, verbose=_verbose, device=device) File "/data/python/.cache/torch/hub/ultralytics_yolov5_master/hubconf.py", line 54, in _create from models.common import AutoShape, DetectMultiBackend File "/data/python/.cache/torch/hub/ultralytics_yolov5_master/models/common.py", line 19, in import requests ModuleNotFoundError: No module named 'requests'
你可能需要使用如下命令解决,
pip install requests
如果遇到如下错误提示,
Using cache found in /data/python/.cache/torch/hub/ultralytics_yolov5_master Traceback (most recent call last): File "/data/python/cv_flask_web/yolo_webapp.py", line 26, in model = torch.hub.load('ultralytics/yolov5', 'custom', path = 'clean_rm_r5.pt') File "/data/python/venv/lib64/python3.9/site-packages/torch/hub.py", line 647, in load model = _load_local(repo_or_dir, model, *args, **kwargs) File "/data/python/venv/lib64/python3.9/site-packages/torch/hub.py", line 676, in _load_local model = entry(*args, **kwargs) File "/data/python/.cache/torch/hub/ultralytics_yolov5_master/hubconf.py", line 135, in custom return _create(path, autoshape=autoshape, verbose=_verbose, device=device) File "/data/python/.cache/torch/hub/ultralytics_yolov5_master/hubconf.py", line 54, in _create from models.common import AutoShape, DetectMultiBackend File "/data/python/.cache/torch/hub/ultralytics_yolov5_master/models/common.py", line 22, in from PIL import Image ModuleNotFoundError: No module named 'PIL'
你可能需要使用如下命令解决,
pip install Pillow
2.2.5 测试使用gunicorn启动
pip install gunicorn
以上命令安装软件包后,你使用如下命令测试服务启动,
cd cv_flask_web gunicorn -w 4 -b 0.0.0.0:5000 yolo_webapp:app
2.2.6 配置服务启动
vim /etc/systemd/system/yolo.service
输入如下配置,
[Unit] Description=YOLO Flask Web App After=network.target [Service] User=python Group=python WorkingDirectory=/data/python/cv_flask_web #ExecStart=/data/python/venv/bin/python /data/python/cv_flask_web/yolo_webapp.py ExecStart=/data/python/venv/bin/gunicorn -w 4 -b 0.0.0.0:5000 yolo_webapp:app Restart=always Environment=PATH=/data/python/venv/bin [Install] WantedBy=multi-user.target
服务配置创建后,你需要使用如下命令使配置生效,
systemctl daemon-reloa
然后,你需要使用如下命令启动服务并设置自启动,
systemctl enable yolo.service systemctl start yolo.service systemctl status yolo.service
参考文献
==============
https://blog.csdn.net/weixin_45789365/article/details/143312796
没有评论