webpy离线安装部署
今年项目太忙好久没写文章了,之前用python写的读采集数据的服务要做部署(windows server 2012R2),服务器不能访问互联网,只能离线安装依赖的模块,下面就记录一个简单方便的方法。
一、导出项目引入的三方模块
- 打开命令行工具,cd到项目根目录下(pycharm点终端直接打开),执行
pip freeze > requirements.txt
命令
>> pip freeze > requirements.txt
根目录下会生成 requirements.txt 这样一个文件,里面是这样一堆内容
numpy==1.20.3
pandas==1.2.4
pyinstaller==4.4
pyinstaller-hooks-contrib==2021.2
python-dateutil==2.8.1
....
- 继续在命令行执行
pip download -d packages -r requirements.txt
,如下显示表示离线包下载成功
>> pip download -d packages -r requirements.txt
Collecting altgraph==0.17
Using cached altgraph-0.17-py2.py3-none-any.whl (21 kB)
Collecting chardet==4.0.0
...
Installing build dependencies ... done
Getting requirements to build wheel ... done
Preparing wheel metadata ... done
Collecting setuptools
Using cached setuptools-57.2.0-py3-none-any.whl (818 kB)
Saved d:\workarea\pycharmprojects\xgwj-dps\packages\altgraph-0.17-py2.py3-none-any.whl
Saved d:\workarea\pycharmprojects\xgwj-dps\packages\chardet-4.0.0-py2.py3-none-any.whl
...
Successfully downloaded altgraph chardet cheroot future jaraco.functools more-itertools numpy pandas pefile pyinstaller pyinstaller-hooks-contrib python-dateutil pytz pywin32-ctypes six web.py setuptools
在项目根目录会生成一个packages目录里面是下载的离线包,拷贝packages目录和requirements.txt 到服务器
二、服务器离线安装模块
服务器要先安装python配置环境(这不是重点略过),已经有Python环境之后,把拷贝的服务器的requirements.txt文件放到packages目录下,然后把requirements.txt文件里面的内容改成packages下文件的全名,像这样:
xlrd-1.2.0-py2.py3-none-any.whl
six-1.15.0-py2.py3-none-any.whl
pytz-2020.1-py2.py3-none-any.whl
python_dateutil-2.8.1-py2.py3-none-any.whl
pandas-1.0.4-cp37-cp37m-win_amd64.whl
然后打开命令行工具(在packages目录下),然后执行pip install --no-index --find-links=/packages/ -r requirements.txt
> pip install --no-index --find-links=/packages/ -r requirements.txt
Looking in links: /packages/
Processing e:\software\packages\altgraph-0.17-py2.py3-none-any.whl
Installing collected packages: altgraph
Successfully installed altgraph-0.17
二、项目部署
把项目整个拷贝一份,删除venv、build等等无关的目录,一般保留的是src、static和templates视项目目录而定,然后将删减后的项目上传到服务器,执行python app.py 8888
,app.py是入口文件,8888是端口号
> python app.py 8888
Traceback (most recent call last):
File "app.py", line 10, in <module>
import com.cstd.routing as router
ModuleNotFoundError: No module named 'ams'
报错显示找不到自己写的模块,因为运行的目录时python程序的目录,找不到自己项目目录,解决办法
直接用第一种方法写脚本启动,大概这样:
@echo 设置应用路径
set PYTHONPATH=D:\project\apps\src
@echo 启动应用
python app.py 8888
先设置临时的PYTHONPATH 然后启动项目,访问OK