Python 虚拟环境 打包 py-all-in-one - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
Alfons
V2EX    Python

Python 虚拟环境 打包 py-all-in-one

  •  
  •   Alfons 2024-01-16 21:01:18 +08:00 3117 次点击
    这是一个创建于 640 天前的主题,其中的信息可能已经有所发展或是发生改变。

    搞了个 python 虚拟环境 打包的工具,解决 无公网环境安装 python 、不同 Linux 操作系统 Python 环境兼容的问题

    https://github.com/Alfonsxh/py-all-in-one

    32 条回复    2024-01-18 19:42:54 +08:00
    jackOff
        1
    jackOff  
       2024-01-16 21:23:05 +08:00
    我的问题是可以在 arm 设备上使用吗? x86 架构的 python 打包工具实际上很多?如果可以的话请给出最简单的 demo 打包实例
    gumuxi
        2
    gumuxi  
       2024-01-16 22:25:14 +08:00
    用 docker 不就好了么
    Alfons
        3
    Alfons  
    OP
       2024-01-17 09:14:16 +08:00
    @gumuxi 我接触的很多企业用户,还是很保守的,接受不了 docker 这种方案
    Alfons
        4/span>
    Alfons  
    OP
       2024-01-17 09:14:34 +08:00
    @jackOff 可以的,一会补充使用方案
    Geekerstar
        5
    Geekerstar  
       2024-01-17 09:16:09 +08:00
    太巧了,昨天正需要这个,今天就看到了
    MasterCai
        6
    MasterCai  
       2024-01-17 09:18:13 +08:00 via iPhone
    为什么不用 conda-pack ?有什么区别呢?
    Alfons
        7
    Alfons  
    OP
       2024-01-17 09:22:05 +08:00
    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 源码的方式
    Alfons
        8
    Alfons  
    OP
       2024-01-17 09:22:47 +08:00
    @MasterCai 试过很多方案,没办法解决动态库 glibc 依赖的问题
    Geekerstar
        9
    Geekerstar  
       2024-01-17 09:35:41 +08:00
    安装三方依赖包的时候报错了,是因为这个地址 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
    Alfons
        10
    Alfons  
    OP
       2024-01-17 09:46:26 +08:00
    @Geekerstar 是在本地 Docker desktop 里设置 了 容器 proxy 吗
    Geekerstar
        11
    Geekerstar  
       2024-01-17 09:52:21 +08:00
    @Alfons 好像没有哦,我是在服务器上运行的。我马上重新弄个干净的虚拟机再试试。感谢你的工具,很牛逼,希望能成功,我昨天搞了一天离线安装没搞好
    Alfons
        12
    Alfons  
    OP
       2024-01-17 09:56:35 +08:00
    @Geekerstar 嗯嗯,不客气。

    你应该是机器上的环境问题,刚在我自己环境上是可以打出来的

    [root@0d315fcc076b alfonstest]# source /usr/local/python3/3.9.0/alfonstest/bin/activate
    (alfonstest) [root@0d315fcc076b alfonstest]# python --version
    Python 3.9.0
    Etuloser
        13
    Etuloser  
       2024-01-17 10:01:20 +08:00
    @Alfons 请教一下,不太理解这个项目使用场景,如果是项目打包,使用 pyinstaller 就好,如果是 python 环境的话,直接到服务器上编译安装指定版本,使用的时候用 virtualenv 指定解释器路径就行,请问有什么区别或者说优势嘛。
    gumuxi
        14
    gumuxi  
       2024-01-17 10:14:00 +08:00
    @Alfons #8 虚拟环境最大的问题就是 glibc 难以通用兼容
    gumuxi
        15
    gumuxi  
       2024-01-17 10:15:58 +08:00
    @Alfons #3 部署上就好啦,还要求技术细节嘛。我们也是离线内网环境,我搞了一键安装 docker+docker-compose ,自动导入镜像启动容器服务啥的,服务可用稳定靠谱,兼容各种 linux 发行版,amd 和 arm 架构,客户倒是不关心用的啥技术部署的。
    Geekerstar
        16
    Geekerstar  
       2024-01-17 10:22:32 +08:00
    我换了个虚拟机还是不行,是不是网络有问题呢?安装三方包报 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
    Alfons
        17
    Alfons  
    OP
       2024-01-17 12:49:24 +08:00
    @Etuloser 无外网访问,虚拟环境中的动态库依赖的 glibc ,不同操作系统难以通用
    Alfons
        18
    Alfons  
    OP
       2024-01-17 12:56:24 +08:00
    @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
    Geekerstar
        19
    Geekerstar  
       2024-01-17 13:28:43 +08:00
    @Alfons 好的,感谢,我晚上回家试试
    ruanimal
        20
    ruanimal  
       2024-01-17 14:57:03 +08:00
    看了下代码,你这个不如直接用 miniconda 吧
    table cellpadding="0" cellspacing="0" border="0" width="100%"> Geekerstar
        21
    Geekerstar  
       2024-01-17 20:24:26 +08:00
    @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'
    Geekerstar
        22
    Geekerstar  
       2024-01-17 21:42:55 +08:00
    @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
    Alfons
        23
    Alfons  
    OP
       2024-01-17 21:46:52 +08:00
    @Geekerstar 完整的 requirements.txt 发一下呢
    Alfons
        24
    Alfons  
    OP
       2024-01-17 21:48:59 +08:00
    @ruanimal 针对的场景不一样,试了很多 python 虚拟环境打包的方案,确实没办法解决所有的依赖问题,才启动的这个项目
    Geekerstar
        25
    Geekerstar  
       2024-01-18 09:21:21 +08:00
    @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
    Alfons
        26
    Alfons  
    OP
       2024-01-18 09:41:36 +08:00
    @Geekerstar 留个邮箱吧,我发你
    Geekerstar
        27
    Geekerstar  
       2024-01-18 10:13:22 +08:00
    @Alfons 感谢大佬,对了我是用的 ubuntu ,不知道是不是和这个有关? [email protected]
    Alfons
        28
    Alfons  
    OP
       2024-01-18 11:55:08 +08:00
    @Geekerstar 有些问题,我再看看
    Geekerstar
        29
    Geekerstar  
       2024-01-18 13:39:46 +08:00
    @Alfons 好的,感谢大佬
    Geekerstar
        30
    Geekerstar  
       2024-01-18 15:55:45 +08:00
    @Alfons 大佬,解决了可以给我回复一下么?或者把打好的包发我邮箱,再次感谢
    Alfons
        31
    Alfons  
    OP
       2024-01-18 18:36:02 +08:00
    @Geekerstar 再试一下,更新 docker 镜像再试试
    Geekerstar
        32
    Geekerstar  
       2024-01-18 19:42:54 +08:00
    @Alfons 试过了,完全 OK ,感谢大佬开源的牛逼工具
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     2513 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 24ms UTC 11:16 PVG 19:16 LAX 04:16 JFK 07:16
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86