搞了个 python 虚拟环境 打包的工具,解决 无公网环境安装 python 、不同 Linux 操作系统 Python 环境兼容的问题
1
jackOff 322 天前
我的问题是可以在 arm 设备上使用吗? x86 架构的 python 打包工具实际上很多?如果可以的话请给出最简单的 demo 打包实例
|
2
gumuxi 322 天前
用 docker 不就好了么
|
5
Geekerstar 321 天前
太巧了,昨天正需要这个,今天就看到了
|
6
MasterCai 321 天前 via iPhone
为什么不用 conda-pack ?有什么区别呢?
|
7
Alfons OP docker run -it --rm --platform linux/amd64 \
-v `pwd`/test/Python:/packages/Python \ -v `pwd`/test/requirements.txt:/packages/requirements.txt \ -v `pwd`/test/build:/packages/build alfonsxh/py-all-in-one:latest --install-dir /usr/local/python_env/ --project alfonstest --python-version 3.8.18 docker 参数: - --platform linux/amd64 - 平台可以选 linux/amd64 、linux/arm64 - `pwd`/test/Python - Python 安装包位置,打包前需要将 Python 源码下载保存在该目录。源码下载地址: https://www.python.org/ftp/python/ (当前只支持 tgz 包) - `pwd`/test/requirements.txt - 项目依赖的 Python 模块 - `pwd`/test/build - 打完包后,保存的目录 命令参数: --install-dir - Python 环境安装的目录,对应在部署机器上的路径 --project - 项目名称,虚拟环境会生成在 {install-dir}/{python_version}/{project} 目录下 --python-version - Python 版本,根据 pwd/test/Python 目录下的 Python 源码包进行选择。暂时只支持先下载 Python 源码的方式 |
9
Geekerstar 321 天前
安装三方依赖包的时候报错了,是因为这个地址 403 么?
+++++++++++++++++安装项目第三方依赖包+++++++++++++++++ [ OK ]: Execute cmd -> source /usr/local/python3/3.9.0/alfonstest/bin/activate && export LD_LIBRARY_PATH=/usr/local/python3/3.9.0/alfonstest/main/lib:$LD_LIBRARY_PATH && pip install --upgrade pip && pip install -r /packages/requirements.txt Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple Collecting pip ERROR: HTTP error 403 while getting https://pypi.tuna.tsinghua.edu.cn/packages/15/aa/3f4c7bcee2057a76562a5b33ecbd199be08cdb4443a02e26bd2c3cf6fc39/pip-23.3.2-py3-none-any.whl#sha256=5052d7889c1f9d05224cd41741acb7c5d6fa735ab34e339624a614eaaa7e7d76 (from https://pypi.tuna.tsinghua.edu.cn/simple/pip/) (requires-python:>=3.7) ERROR: Could not install requirement pip from https://pypi.tuna.tsinghua.edu.cn/packages/15/aa/3f4c7bcee2057a76562a5b33ecbd199be08cdb4443a02e26bd2c3cf6fc39/pip-23.3.2-py3-none-any.whl#sha256=5052d7889c1f9d05224cd41741acb7c5d6fa735ab34e339624a614eaaa7e7d76 because of HTTP error 403 Client Error: Forbidden for url: https://pypi.tuna.tsinghua.edu.cn/packages/15/aa/3f4c7bcee2057a76562a5b33ecbd199be08cdb4443a02e26bd2c3cf6fc39/pip-23.3.2-py3-none-any.whl for URL https://pypi.tuna.tsinghua.edu.cn/packages/15/aa/3f4c7bcee2057a76562a5b33ecbd199be08cdb4443a02e26bd2c3cf6fc39/pip-23.3.2-py3-none-any.whl#sha256=5052d7889c1f9d05224cd41741acb7c5d6fa735ab34e339624a614eaaa7e7d76 (from https://pypi.tuna.tsinghua.edu.cn/simple/pip/) (requires-python:>=3.7) WARNING: You are using pip version 20.2.3; however, version 23.3.2 is available. You should consider upgrading via the '/usr/local/python3/3.9.0/alfonstest/bin/python3 -m pip install --upgrade pip' command. Traceback (most recent call last): File "build.py", line 513, in <module> prompt=args.project, File "build.py", line 91, in wrapper res = func(*args, **kwargs) File "build.py", line 406, in run self.pip_install() File "build.py", line 91, in wrapper res = func(*args, **kwargs) File "build.py", line 209, in pip_install requirements=requirements_path)) File "build.py", line 71, in run_local_cmd raise TypeError: exceptions must be old-style classes or derived from BaseException, not NoneType |
10
Alfons OP @Geekerstar 是在本地 Docker desktop 里设置 了 容器 proxy 吗
|
11
Geekerstar 321 天前
@Alfons 好像没有哦,我是在服务器上运行的。我马上重新弄个干净的虚拟机再试试。感谢你的工具,很牛逼,希望能成功,我昨天搞了一天离线安装没搞好
|
12
Alfons OP @Geekerstar 嗯嗯,不客气。
你应该是机器上的环境问题,刚在我自己环境上是可以打出来的 [root@0d315fcc076b alfonstest]# source /usr/local/python3/3.9.0/alfonstest/bin/activate (alfonstest) [root@0d315fcc076b alfonstest]# python --version Python 3.9.0 |
13
Etuloser 321 天前
@Alfons 请教一下,不太理解这个项目使用场景,如果是项目打包,使用 pyinstaller 就好,如果是 python 环境的话,直接到服务器上编译安装指定版本,使用的时候用 virtualenv 指定解释器路径就行,请问有什么区别或者说优势嘛。
|
15
gumuxi 321 天前
@Alfons #3 部署上就好啦,还要求技术细节嘛。我们也是离线内网环境,我搞了一键安装 docker+docker-compose ,自动导入镜像启动容器服务啥的,服务可用稳定靠谱,兼容各种 linux 发行版,amd 和 arm 架构,客户倒是不关心用的啥技术部署的。
|
16
Geekerstar 321 天前
我换了个虚拟机还是不行,是不是网络有问题呢?安装三方包报 403 ,三方包是下面这些:
Flask==3.0.0 scikit-learn==1.3.1 flasgger==0.9.7.1 pandas==2.1.4 matplotlib==3.8.2 statsmodels==0.14.1 PyWavelets==1.5.0 minio==7.2.2 openpyxl==3.1.2 @Alfons |
18
Alfons OP @Geekerstar 应该还是网络的问题,我这边是可以的
+++++++++++++++++安装项目第三方依赖包+++++++++++++++++ [ OK ]: Execute cmd -> source /usr/local/python3/3.9.0/alfonstest/bin/activate && export LD_LIBRARY_PATH=/usr/local/python3/3.9.0/alfonstest/main/lib:$LD_LIBRARY_PATH && pip install --upgrade pip && pip install -r /packages/requirements.txt Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple Collecting pip Downloading https://pypi.tuna.tsinghua.edu.cn/packages/15/aa/3f4c7bcee2057a76562a5b33ecbd199be08cdb4443a02e26bd2c3cf6fc39/pip-23.3.2-py3-none-any.whl (2.1 MB) |████████████████████████████████| 2.1 MB 512 kB/s Installing collected packages: pip Attempting uninstall: pip Found existing installation: pip 20.2.3 Uninstalling pip-20.2.3: Successfully uninstalled pip-20.2.3 Successfully installed pip-23.3.2 |
19
Geekerstar 321 天前
@Alfons 好的,感谢,我晚上回家试试
|
20
ruanimal 321 天前
看了下代码,你这个不如直接用 miniconda 吧
|
21
Geekerstar 321 天前
@Alfons 安装依赖包成功,最后复制的时候又报错了,这个是因为什么原因呢?
[ OK ]: 拷贝动态库 /usr/local/python_env/3.9.0/alfonstest/lib/python3.9/site-packages/scipy/io/../../scipy.libs/libgfortran-040039e1.so.5.0.0 -> /usr/local/python_env/system_lib/alfonstest/libgfortran-040039e1.so.5.0.0 成功! [ OK ]: 拷贝动态库 /usr/local/python_env/3.9.0/alfonstest/lib/python3.9/site-packages/scipy/linalg/../../scipy.libs/libgfortran-040039e1.so.5.0.0 -> /usr/local/python_env/system_lib/alfonstest/libgfortran-040039e1.so.5.0.0 成功! [ OK ]: 拷贝动态库 /usr/local/python_env/3.9.0/alfonstest/lib/python3.9/site-packages/scipy/odr/../../scipy.libs/libgfortran-040039e1.so.5.0.0 -> /usr/local/python_env/system_lib/alfonstest/libgfortran-040039e1.so.5.0.0 成功! [ OK ]: 拷贝动态库 /usr/local/python_env/3.9.0/alfonstest/lib/python3.9/site-packages/scipy/optimize/../../scipy.libs/libgfortran-040039e1.so.5.0.0 -> /usr/local/python_env/system_lib/alfonstest/libgfortran-040039e1.so.5.0.0 成功! [ OK ]: 拷贝动态库 /usr/local/python_env/3.9.0/alfonstest/lib/python3.9/site-packages/scipy/optimize/_trlib/../../../scipy.libs/libgfortran-040039e1.so.5.0.0 -> /usr/local/python_env/system_lib/alfonstest/libgfortran-040039e1.so.5.0.0 成功! [ OK ]: 拷贝动态库 /usr/local/python_env/3.9.0/alfonstest/lib/python3.9/site-packages/scipy/sparse/linalg/_dsolve/../../../../scipy.libs/libgfortran-040039e1.so.5.0.0 -> /usr/local/python_env/system_lib/alfonstest/libgfortran-040039e1.so.5.0.0 成功! [ OK ]: 拷贝动态库 /usr/local/python_env/3.9.0/alfonstest/lib/python3.9/site-packages/scipy/sparse/linalg/_eigen/arpack/../../../../../scipy.libs/libgfortran-040039e1.so.5.0.0 -> /usr/local/python_env/system_lib/alfonstest/libgfortran-040039e1.so.5.0.0 成功! [ OK ]: 拷贝动态库 /usr/local/python_env/3.9.0/alfonstest/lib/python3.9/site-packages/scipy/sparse/linalg/_isolve/../../../../scipy.libs/libgfortran-040039e1.so.5.0.0 -> /usr/local/python_env/system_lib/alfonstest/libgfortran-040039e1.so.5.0.0 成功! [ OK ]: 拷贝动态库 /usr/local/python_env/3.9.0/alfonstest/lib/python3.9/site-packages/scipy/sparse/linalg/_propack/../../../../scipy.libs/libgfortran-040039e1.so.5.0.0 -> /usr/local/python_env/system_lib/alfonstest/libgfortran-040039e1.so.5.0.0 成功! [ OK ]: 拷贝动态库 /usr/local/python_env/3.9.0/alfonstest/lib/python3.9/site-packages/scipy/spatial/../../scipy.libs/libgfortran-040039e1.so.5.0.0 -> /usr/local/python_env/system_lib/alfonstest/libgfortran-040039e1.so.5.0.0 成功! [ OK ]: 拷贝动态库 /usr/local/python_env/3.9.0/alfonstest/lib/python3.9/site-packages/scipy/special/../../scipy.libs/libgfortran-040039e1.so.5.0.0 -> /usr/local/python_env/system_lib/alfonstest/libgfortran-040039e1.so.5.0.0 成功! Traceback (most recent call last): File "build.py", line 513, in <module> prompt=args.project, File "build.py", line 91, in wrapper res = func(*args, **kwargs) File "build.py", line 409, in run self.pack_library() File "build.py", line 91, in wrapper res = func(*args, **kwargs) File "build.py", line 248, in pack_library shutil.copyfile(src=src_file, dst=dst_file) File "/usr/lib64/python2.7/shutil.py", line 82, in copyfile with open(src, 'rb') as fsrc: IOError: [Errno 2] No such file or directory: 'not' |
22
Geekerstar 321 天前
@Alfons 好像是和 requirements.txt 里的内容有关,试了下只留下一些是可以打包成功的。但是全部放上去就会报上面的错。
Flask==3.0.0 scikit-learn==1.3.1 flasgger==0.9.7.1 pandas==2.1.4 matplotlib==3.8.2 statsmodels==0.14.1 PyWavelets==1.5.0 minio==7.2.2 openpyxl==3.1.2 |
23
Alfons OP @Geekerstar 完整的 requirements.txt 发一下呢
|
25
Geekerstar 320 天前
@Alfons 这个就是完整的,如果您那边能打包成功,能不能把打包后的文件发我试试能不能安装呢?
Flask==3.0.0 scikit-learn==1.3.1 flasgger==0.9.7.1 pandas==2.1.4 matplotlib==3.8.2 statsmodels==0.14.1 PyWavelets==1.5.0 minio==7.2.2 openpyxl==3.1.2 |
26
Alfons OP @Geekerstar 留个邮箱吧,我发你😂
|
27
Geekerstar 320 天前
@Alfons 感谢大佬,对了我是用的 ubuntu ,不知道是不是和这个有关? [email protected]
|
28
Alfons OP @Geekerstar 有些问题,我再看看
|
29
Geekerstar 320 天前
@Alfons 好的,感谢大佬
|
30
Geekerstar 320 天前
@Alfons 大佬,解决了可以给我回复一下么?或者把打好的包发我邮箱,再次感谢
|
31
Alfons OP @Geekerstar 再试一下,更新 docker 镜像再试试
|
32
Geekerstar 320 天前
@Alfons 试过了,完全 OK ,感谢大佬开源的牛逼工具
|