企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
## 背景 哈哈,说起来也是挺逗的,在这个AI时代,是个程序员都得变身成全能学霸,不然分分钟就被时代的巨轮给甩飞了。我呢?作为一个小菜鸟,心里头那个急啊,生怕一不小心就被AI给“智能”掉了。 结果呢,还没跟AI大战三百回合,就先被自己的电脑配置环境给“虐”了。简直就是一场没有硝烟的战争,安装包、驱动、兼容性问题……想骂人喽! 但话说回来,这过程虽然坎坷,却也乐在其中。毕竟,哪个程序员不是在不断试错中成长的呢? 所以啊,虽然被本地环境虐得有点惨,但我也算是痛并快乐着吧。毕竟,每一次的失败都是向成功迈进的一步嘛!接下来听我来说说,是怎么一步一步北虐待的!!! ## 开始被虐 ### phpy官方镜像 当我按照phpy官方镜像`phpswoole/phpy:1.0.5`安装`torch` ``` pip install torch torchvision torchaudio ``` 出现以下的错误信息 ``` ERROR: Could not find a version that satisfies the requirement torch (from versions: none) ERROR: No matching distribution found for torch ``` > 这个错误信息表明你的Python环境在尝试安装PyTorch(一个流行的机器学习库)时未能找到任何合适的版本。 通过AI也搜索到了问题的解决办法 ![](https://img.kancloud.cn/e3/dc/e3dc3cbd377b3edad46b47cb53e6e440_803x748.png) 下来我们通过指定版本,实际上如果同时确定torch版本以及适合的CUDA版本,应该使用下面的指令: ```ts pip install torch==2.2.0+cpu torchvision torchaudio -f https://download.pytorch.org/whl/torch_stable.html ``` 使用这条指令,可以直接在官方对应的网站检索相应版本下载。可以看出是按照成功了 ```ts (venv) /var/www # pip install torch==2.2.0+cpu torchvision torchaudio -f https://download.pytorch.org/whl/torch_stable.html Looking in links: https://download.pytorch.org/whl/torch_stable.html Collecting torch==2.2.0+cpu Downloading https://download.pytorch.org/whl/cpu/torch-2.2.0%2Bcpu-cp312-cp312-linux_x86_64.whl (186.7 MB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 186.7/186.7 MB 4.3 MB/s eta 0:00:00 Collecting torchvision Downloading https://download.pytorch.org/whl/rocm6.0/torchvision-0.18.1%2Brocm6.0-cp312-cp312-linux_x86_64.whl (65.7 MB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 65.7/65.7 MB 5.5 MB/s eta 0:00:00 Collecting torchaudio Downloading https://download.pytorch.org/whl/rocm6.0/torchaudio-2.3.1%2Brocm6.0-cp312-cp312-linux_x86_64.whl (1.7 MB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 1.7/1.7 MB 5.1 MB/s eta 0:00:00 Collecting filelock (from torch==2.2.0+cpu) Downloading filelock-3.15.4-py3-none-any.whl.metadata (2.9 kB) Collecting typing-extensions>=4.8.0 (from torch==2.2.0+cpu) Downloading typing_extensions-4.12.2-py3-none-any.whl.metadata (3.0 kB) Collecting sympy (from torch==2.2.0+cpu) Downloading sympy-1.13.2-py3-none-any.whl.metadata (12 kB) Collecting networkx (from torch==2.2.0+cpu) Downloading networkx-3.3-py3-none-any.whl.metadata (5.1 kB) Collecting jinja2 (from torch==2.2.0+cpu) Downloading jinja2-3.1.4-py3-none-any.whl.metadata (2.6 kB) Collecting fsspec (from torch==2.2.0+cpu) Downloading fsspec-2024.6.1-py3-none-any.whl.metadata (11 kB) Collecting numpy (from torchvision) Downloading numpy-2.1.0-cp312-cp312-musllinux_1_1_x86_64.whl.metadata (60 kB) INFO: pip is looking at multiple versions of torchvision to determine which version is compatible with other requirements. This could take a while. Collecting torchvision Downloading https://download.pytorch.org/whl/rocm5.7/torchvision-0.18.1%2Brocm5.7-cp312-cp312-linux_x86_64.whl (65.5 MB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 65.5/65.5 MB 1.6 MB/s eta 0:00:00 Downloading https://download.pytorch.org/whl/cu121/torchvision-0.18.1%2Bcu121-cp312-cp312-linux_x86_64.whl (7.0 MB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 7.0/7.0 MB 2.2 MB/s eta 0:00:00 Downloading https://download.pytorch.org/whl/cu118/torchvision-0.18.1%2Bcu118-cp312-cp312-linux_x86_64.whl (6.3 MB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 6.3/6.3 MB 2.9 MB/s eta 0:00:00 Downloading https://download.pytorch.org/whl/cpu/torchvision-0.18.1%2Bcpu-cp312-cp312-linux_x86_64.whl (1.6 MB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 1.6/1.6 MB 3.0 MB/s eta 0:00:00 Downloading https://download.pytorch.org/whl/rocm6.0/torchvision-0.18.0%2Brocm6.0-cp312-cp312-linux_x86_64.whl (65.7 MB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 65.7/65.7 MB 5.0 MB/s eta 0:00:00 Downloading https://download.pytorch.org/whl/rocm5.7/torchvision-0.18.0%2Brocm5.7-cp312-cp312-linux_x86_64.whl (65.5 MB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 65.5/65.5 MB 5.3 MB/s eta 0:00:00 Downloading https://download.pytorch.org/whl/cu121/torchvision-0.18.0%2Bcu121-cp312-cp312-linux_x86_64.whl (7.0 MB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 7.0/7.0 MB 5.4 MB/s eta 0:00:00 INFO: pip is still looking at multiple versions of torchvision to determine which version is compatible with other requirements. This could take a while. Downloading https://download.pytorch.org/whl/cu118/torchvision-0.18.0%2Bcu118-cp312-cp312-linux_x86_64.whl (6.3 MB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 6.3/6.3 MB 2.5 MB/s eta 0:00:00 Downloading https://download.pytorch.org/whl/cpu/torchvision-0.18.0%2Bcpu-cp312-cp312-linux_x86_64.whl (1.6 MB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 1.6/1.6 MB 4.7 MB/s eta 0:00:00 Downloading https://download.pytorch.org/whl/rocm5.7/torchvision-0.17.2%2Brocm5.7-cp312-cp312-linux_x86_64.whl (65.5 MB) ━━━━━━━━━━━━━━━━━━━━━╺━━━━━━━━━━━━━━━━━━ 35.1/65.5 MB 1.5 MB/s eta 0:00:20 Downloading certifi-2024.8.30-py3-none-any.whl (167 kB) Downloading charset_normalizer-3.3.2-cp312-cp312-musllinux_1_1_x86_64.whl (142 kB) Downloading idna-3.8-py3-none-any.whl (66 kB) Downloading MarkupSafe-2.1.5-cp312-cp312-musllinux_1_1_x86_64.whl (33 kB) Downloading mpmath-1.3.0-py3-none-any.whl (536 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 536.2/536.2 kB 3.9 MB/s eta 0:00:00 Downloading urllib3-2.2.2-py3-none-any.whl (121 kB) Installing collected packages: mpmath, urllib3, typing-extensions, sympy, pillow, numpy, networkx, MarkupSafe, idna, fsspec, filelock, charset-normalizer, certifi, requests, jinja2, torch, torchvision, torchaudio Successfully installed MarkupSafe-2.1.5 certifi-2024.8.30 charset-normalizer-3.3.2 filelock-3.15.4 fsspec-2024.6.1 idna-3.8 jinja2-3.1.4 mpmath-1.3.0 networkx-3.3 numpy-2.1.0 pillow-10.4.0 requests-2.32.3 sympy-1.13.2 torch-2.2.0+cpu torchaudio-2.2.0+rocm5.7 torchvision-0.17.0+rocm5.7 typing-extensions-4.12.2 urllib3-2.2.2 ``` ### opencv-python 接下来我们安装`opencv-python`这个库 ``` pip install opencv-python ``` 安装错误信息 ```ts # pip install opencv-python Collecting opencv-python Using cached opencv-python-4.10.0.84.tar.gz (95.1 MB) Installing build dependencies ... done Getting requirements to build wheel ... error error: subprocess-exited-with-error × Getting requirements to build wheel did not run successfully. │ exit code: 1 ╰─> [33 lines of output] Traceback (most recent call last): File "/path/to/venv/lib/python3.12/site-packages/pip/_vendor/pyproject_hooks/_in_process/_in_process.py", line 353, in <module> main() File "/path/to/venv/lib/python3.12/site-packages/pip/_vendor/pyproject_hooks/_in_process/_in_process.py", line 335, in main json_out['return_val'] = hook(**hook_input['kwargs']) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/path/to/venv/lib/python3.12/site-packages/pip/_vendor/pyproject_hooks/_in_process/_in_process.py", line 112, in get_requires_for_build_wheel backend = _build_backend() ^^^^^^^^^^^^^^^^ File "/path/to/venv/lib/python3.12/site-packages/pip/_vendor/pyproject_hooks/_in_process/_in_process.py", line 77, in _build_backend obj = import_module(mod_path) ^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/lib/python3.12/importlib/__init__.py", line 90, in import_module return _bootstrap._gcd_import(name[level:], package, level) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "<frozen importlib._bootstrap>", line 1387, in _gcd_import File "<frozen importlib._bootstrap>", line 1360, in _find_and_load File "<frozen importlib._bootstrap>", line 1310, in _find_and_load_unlocked File "<frozen importlib._bootstrap>", line 488, in _call_with_frames_removed File "<frozen importlib._bootstrap>", line 1387, in _gcd_import File "<frozen importlib._bootstrap>", line 1360, in _find_and_load File "<frozen importlib._bootstrap>", line 1331, in _find_and_load_unlocked File "<frozen importlib._bootstrap>", line 935, in _load_unlocked File "<frozen importlib._bootstrap_external>", line 995, in exec_module File "<frozen importlib._bootstrap>", line 488, in _call_with_frames_removed File "/tmp/pip-build-env-tj3_9lus/overlay/lib/python3.12/site-packages/setuptools/__init__.py", line 16, in <module> import setuptools.version File "/tmp/pip-build-env-tj3_9lus/overlay/lib/python3.12/site-packages/setuptools/version.py", line 1, in <module> import pkg_resources File "/tmp/pip-build-env-tj3_9lus/overlay/lib/python3.12/site-packages/pkg_resources/__init__.py", line 2172, in <module> register_finder(pkgutil.ImpImporter, find_on_path) ^^^^^^^^^^^^^^^^^^^ AttributeError: module 'pkgutil' has no attribute 'ImpImporter'. Did you mean: 'zipimporter'? [end of output] note: This error originates from a subprocess, and is likely not a problem with pip. error: subprocess-exited-with-error × Getting requirements to build wheel did not run successfully. │ exit code: 1 ╰─> See above for output. note: This error originates from a subprocess, and is likely not a problem with pip. ``` 错误信息 ``` note: This error originates from a subprocess, and is likely not a problem with pip. error: subprocess-exited-with-error × Getting requirements to build wheel did not run successfully. 这个错误是由于 Python 3.12 中的`pkgutil`模块与某些旧版本的`setuptools`或其他依赖包不兼容所导致的。在 Python 3.12 中,`pkgutil.ImpImporter`已经被移除,这导致了`AttributeError`。 ``` **解决方案** 1. 升级 setuptools :`pip install --upgrade setuptools` 2. 清除 pip 的缓存:`pip cache purge` 3. 其他N种解决方案都不行 > 结论是:解决不了!解决不了!解决不了!我放弃了。时间不早了 ![](https://img.kancloud.cn/a1/53/a1536682838112fa69a4dae7b6bfa446_1145x207.png) ### 构建自己的phpy镜像 由于官方默认镜像库基础镜像是使用`Base image php:8.2-alpine`,会有各种不兼容问题。 ![](https://img.kancloud.cn/f2/38/f23829853327f879b69311c75327b1bc_1057x306.png) 所以这里拉取官方仓库重新构建,使用基础镜像`Base image php:8.2.13-cli-bullseye` ``` git clone git@github.com:swoole/phpy.git ``` 构建 ``` docker build -t tinywan/docker-php-phpy:8.2.13 . ``` ![](https://img.kancloud.cn/f2/0e/f20ee135e638292db44c41765faeb6f9_1184x330.png) 运行容器 ``` docker run -it -v d:/dnmp/www/ai:/opt tinywan/docker-php-phpy:8.2.13 ``` 进入容器 ``` docker exec -it fab440ffc331ca9d47f09 sh ``` 接下来我们安装`opencv-python`这个库 ``` apt install pip pip install opencv-python ``` 可以看到很快速的安装成功了 ![](https://img.kancloud.cn/63/90/639081cce2b945baf03b81b258de082c_966x192.png) ## 基础镜像版本区别 |版本|说明|案例| |--|-----------------|--| |alpine|Alpine Linux 操作系统,它是一个独立发行版本,相比较 Debian 操作系统来,更加轻巧|`python:3.11.4-alpine`| |slim|基于Debian操作系统最小操作系统,删除了许多非必需的软件包,优化了体积|`python:3.11.4-slim`| |bookworm|Debian 12 |`python:3.11.4-bookworm`| |bullseye|Debian 11 |`python:3.11.4-bullseye`| 通过`docker images | sort -k7 -h` 比较镜像大小 ```ts docker pull --quiet python:3.11.4 docker pull --quiet python:3.11.4-slim docker pull --quiet python:3.11.4-alpine docker pull --quiet python:3.11.4-bookworm docker pull --quiet python:3.11.4-slim-bookworm ``` ### 完整官方镜像 根据 DockerHub 的说法,没有合格标签的完整镜像是事实上的镜像,如果你不确定并且刚开始,应该使用它。 例如:`python:3.11.4 node:20.3.0` 这些镜像基于最新的稳定 Debian 操作系统版本。我通常在尝试在开发环境中快速启动一个项目时,首先使用其中之一。 完整镜像是最安全的选择,当你试图在开发环境中快速启动某事时,你应该使用它。原因是,它可能包含你的应用程序或脚本运行所需的一切。但是,在部署到生产之前,一定要选择对你来说最小最安全的镜像。 ### alpine Alpine 镜像基于 Alpine Linux 项目,该项目是专门为容器内部使用而构建的操作系统。很长一段时间以来,这些是最受欢迎的镜像变体,因为它们的体积非常小。 然而,一些团队正在弃用alpine镜像,因为这些镜像可能会导致难以调试的兼容性问题。具体来说,如果使用python镜像,一些 `wheels`将被构建成与Debian兼容,并且需要重新编译,才能与基于apline的镜像一起工作。 使用 Alpine 镜像的主要原因是使你的结果镜像尽可能小。基础镜像将小于 5MB。当前的 python 基础镜像(将 python 添加到基础 alpine 镜像)为 78.9MB。那仍然相对较小。 ### slim slim 镜像是完整镜像的精简版本。这个镜像通常只安装运行你特定工具所需的最小内容。就 Python 而言,那就是运行 python 所需的最少包,对于 node.js 也是如此。 通过省略不常用的工具,镜像变得更小。如果你有空间限制,不需要完整版本,请使用此镜像。 ### bullseye 带有 bullseye、bookworm、stretch、buster 或 jessie 标签的镜像是不同 Debian 版本的代号。 如果你的代码与特定版本的 Debian 操作系统兼容,请选择其中一个镜像。通常情况下,当你安装超出基础操作系统提供的包时,就会这样。在这种情况下,你要确保你留在同一个 Debian 版本上,这样你就不会在将来破坏你的构建。 ## 本地镜像 ``` $ docker images |grep phpy tinywan/docker-php-phpy 8.2.13 5110f1243760 1.64GB phpswoole/phpy 1.0.5 3b7e3abc6948 143MB ``` ### phpy 官方镜像 ```ts docker pull phpswoole/phpy:1.0.5 ``` 通过`docker scout quickview phpswoole/phpy:1.0.5`查看镜像大小 ![](https://img.kancloud.cn/f2/38/f23829853327f879b69311c75327b1bc_1057x306.png) > 基础镜像`Base image php:8.2-alpine` ### phpy 本地构建镜像 ``` docker scout quickview tinywan/docker-php-phpy:8.2.13 ``` ![](https://img.kancloud.cn/6a/01/6a01e64daaad1b22146d2a84a80a3e4f_943x233.png) > 基础镜像`Base image php:8.2-bullseye`