VeighNa量化社区
你的开源社区量化交易平台 | vn.py | vnpy
Member
avatar
加入于:
帖子: 4
声望: 2

vnpy 4.0 Docker镜像制作过程中,从continuumio/anaconda3:latest 作为基础镜像,vnpy 4.0主程序安装成功,ta-lib和pyside6等都安装成功。
容器内已经安装的apt模块:net-tools gcc libxcb1 libxcb1-dev libgl1-mesa-dev libxcb-cursor0 build-essential python3-dev ninja-build ,已经pip 安装的模块:meson meson-python。

按照pip install vnpy_ctp ,或者本地安装(gitclone 代码本地后 pip install)均报错如下:

Obtaining file:///vnpy/vnpy_ctp
  Checking if build backend supports build_editable: started
  Checking if build backend supports build_editable: finished with status 'done'
  Preparing editable metadata (pyproject.toml): started
  Preparing editable metadata (pyproject.toml): finished with status 'error'
  error: subprocess-exited-with-error

  × Preparing editable metadata (pyproject.toml) did not run successfully.
  │ exit code: 1
  ╰─> [55 lines of output]
      + meson setup --reconfigure /vnpy/vnpy_ctp /vnpy/vnpy_ctp/.vnpy_ctpapi -Dbuildtype=release -Db_ndebug=if-release -Db_vscrt=md --native-file=/vnpy/vnpy_ctp/.vnpy_ctpapi/meson-python-native-file.ini
      The Meson build system
      Version: 1.8.1
      Source dir: /vnpy/vnpy_ctp
      Build dir: /vnpy/vnpy_ctp/.vnpy_ctpapi
      Build type: native build
      Project name: vnpy_ctp
      Project version: 6.7.7.1
      C++ compiler for the host machine: c++ (gcc 12.2.0 "c++ (Debian 12.2.0-14+deb12u1) 12.2.0")
      C++ linker for the host machine: c++ ld.bfd 2.40
      Host machine cpu family: aarch64
      Host machine cpu: aarch64
      Program python found: YES (/opt/conda/bin/python3.13)
      Message: 使用pybind11路径: /opt/conda/lib/python3.13/site-packages/pybind11/include
      Message: 构建目标系统: linux
      Library thostmduserapi_se found: YES
      Library thosttraderapi_se found: YES
      WARNING: Please do not define rpath with a linker argument, use install_rpath
      or build_rpath properties instead.
      This will become a hard error in a future Meson release.

      WARNING: Please do not define rpath with a linker argument, use install_rpath
      or build_rpath properties instead.
      This will become a hard error in a future Meson release.

      Build targets in project: 2

      vnpy_ctp 6.7.7.1

        User defined options
          Native files: /vnpy/vnpy_ctp/.vnpy_ctpapi/meson-python-native-file.ini
          b_ndebug    : if-release
          b_vscrt     : md
          buildtype   : release

      Found ninja-1.11.1 at /usr/bin/ninja

      Generating targets:   0%|          | 0/2 eta ?


      Writing build.ninja:   0%|          | 0/32 eta ?

      Cleaning... 0 files.
      + /usr/bin/ninja
      [1/2] Linking target vnctpmd.cpython-313-aarch64-linux-gnu.so
      FAILED: vnctpmd.cpython-313-aarch64-linux-gnu.so
      c++  -o vnctpmd.cpython-313-aarch64-linux-gnu.so vnctpmd.cpython-313-aarch64-linux-gnu.so.p/vnpy_ctp_api_vnctp_vnctpmd_vnctpmd.cpp.o -Wl,--as-needed -Wl,--allow-shlib-undefined -Wl,-O1 -shared -fPIC '-Wl,-rpath,$ORIGIN/../vnpy_ctp/api' -Wl,--start-group -lstdc++ '-Wl,-rpath,$ORIGIN' /vnpy/vnpy_ctp/vnpy_ctp/api/libthostmduserapi_se.so -Wl,--end-group
      /usr/bin/ld: /vnpy/vnpy_ctp/vnpy_ctp/api/libthostmduserapi_se.so: error adding symbols: file in wrong format
      collect2: error: ld returned 1 exit status
      [2/2] Linking target vnctptd.cpython-313-aarch64-linux-gnu.so
      FAILED: vnctptd.cpython-313-aarch64-linux-gnu.so
      c++  -o vnctptd.cpython-313-aarch64-linux-gnu.so vnctptd.cpython-313-aarch64-linux-gnu.so.p/vnpy_ctp_api_vnctp_vnctptd_vnctptd.cpp.o -Wl,--as-needed -Wl,--allow-shlib-undefined -Wl,-O1 -shared -fPIC '-Wl,-rpath,$ORIGIN/../vnpy_ctp/api' -Wl,--start-group -lstdc++ '-Wl,-rpath,$ORIGIN' /vnpy/vnpy_ctp/vnpy_ctp/api/libthosttraderapi_se.so -Wl,--end-group
      /usr/bin/ld: /vnpy/vnpy_ctp/vnpy_ctp/api/libthosttraderapi_se.so: error adding symbols: file in wrong format
      collect2: error: ld returned 1 exit status
      ninja: build stopped: subcommand failed.
      [end of output]

  note: This error originates from a subprocess, and is likely not a problem with pip.
error: metadata-generation-failed

× Encountered error while generating package metadata.
╰─> See above for output.

note: This is an issue with the package mentioned above, not pip.
hint: See above for details.

不知哪里出了问题?是否有解法?在此拜谢。

Super Moderator
avatar
加入于:
帖子: 89
声望: 8

问题核心:CTP API 的动态链接库(libthostmduserapi_se.so 和 libthosttraderapi_se.so)为 x86_64 架构,而你的 Docker 容器运行在 ARM64 架构(如 Mac M1/M2 芯片),导致链接失败。


解决方案

  1. 启用 QEMU 模拟 x86_64 架构

    • 安装 QEMU 用户模式工具,注册处理器以支持 x86_64 架构的容器运行。
  2. 强制使用 x86_64 基础镜像

    • 在 Dockerfile 中指定基础镜像的平台为 linux/amd64,确保容器内所有依赖均为 x86_64 架构。
  3. 安装预编译的 vnpy_ctp 包

    • 通过 pip 安装官方预编译包(已包含 x86_64 架构的二进制库),无需手动编译。
  4. 验证容器架构一致性

    • 构建镜像时指定目标平台为 linux/amd64,运行容器后检查架构是否为 x86_64。

关键点

  • 架构一致性:容器内所有依赖(Python、库文件)必须为 x86_64 架构。
  • 性能影响:QEMU 模拟会带来约 20%-30% 的性能损耗,建议在 x86_64 宿主机上构建生产环境镜像。
Member
avatar
加入于:
帖子: 4
声望: 2

感谢楼上AI回复,基本上是正解。不过最佳时间顺着这个方向,经过本人亲测,完善如下:

MacOS M 系列芯片 部署 VeighNa 4.0 最新非官方镜像指南

1. 问题:为实现VeighNa 4.0镜像多架构(主要考虑AMD x86和ARM arch64)方式部署,以便macOS M系列芯片(当下大多数苹果笔记本电脑芯片架构)以x86芯片架构方式运行vnpy 4.0 docker镜像。

当前官方尚未有4.0镜像,故只好采用github上第三方制作的镜像(本例中采用:dehim/veighna)

2. 用x86制作vnpy镜像的必要性:

  • 为什么要做vnpy4.0镜像? vnpy4.0拥有alpha因子交易模块,其它模块还没有;由于刚推出,尚未有官方的vnpy4.0镜像发布,只好采用第三方或者DIY。
  • 为什么要在x86架构下,封装vnpy4.0? 图形模块talib,ctp等模块需要编译时,需要考虑芯片架构。目前ctp官方引用API模块中,涉及linux的动态链接库 github.com/vnpy/vnpy_ctp/vnpy_ctp/api/目录下libthosttraderapi_se.so和libthostmduserapi_se.so两个文件,来自于上期所官方API,是x86架构,官方未发布ARM架构的动态链接库,若在ARM架构下直接pip install,会导致CTP模块安装失败。

3. 确认问题:(以在macOS M3芯片 笔记本为例)

在宿主机上查询芯片架构:

uname -a

会显示包含 arm64或者amd64字样,表示不同架构的64位程序。
在容器中查询芯片架构:(找一个可以运行的镜像<image_name>查询)

docker image inspect <image_name> | grep Architecture

会显示容器的架构,同样显示CPU架构字样
以上问题显示Docker的芯片架构与镜像的芯片架构不匹配,需要找到兼容运行的方法。其实,只需要运用macOS M系列芯片自带的Rosetta,即可实现在M系列arch芯片下运行x86程序的目的。

4. 在macOS中设置Docker支持多架构

(1) 安装macOS M芯片版本的Docker Desktop,并完成注册用户登录。
(2)打开docker desktop,并在设置/General中
选择:Virtual Machine Options/Apple Virtualization framework/Use Rosetta for x86/amd64 emulation on Apple Silicon 选项

description

(3)重启docker desktop后,选择任何一个linux镜像,再次运行

docker image inspect <image_name> | grep Architecture

显示架构若为x86 AMD字样,则成功

5. 下载vnpy40镜像

采用dehim/veighna的github镜像,强制linux/arm64架构芯片平台运行: --platform=linux/amd64

docker run -it --name vnpy40 \
 --platform=linux/amd64 \
  -e DISPLAY=$DISPLAY \
  -e XDG_RUNTIME_DIR=/tmp/runtime-root \
  -v /tmp/.X11-unix:/tmp/.X11-unix \
  -v ~/vnpy_docker/strategies:/shareVolume_demo/config/vnpy/strategies \
 -v ~/vnpy_docker/vntrader:/shareVolume_demo/config/vnpy/vntrader \
  dehim/veighna \
  /bin/bash

可能来源网络不稳定,需要多次重新安装。
若发现安装未启动,手动使用

docker activate vnpy40
docker exec -it vnpy40 /bin/bash

进入容器。

6. 说明

(1)vnpy的重要文件夹:
vnpy所在目录:
• vnpy包(含alpha等子目录):/usr/local/lib/python3.12/dist-packages/vnpy
• vnpy主目录(包含examples):/shareVolume_demo/config/vnpy

(2)验证方法
在容器启动的状态下,运行

docker exec vnpy40 ls -la /shareVolume_demo/config/vnpy

可查询veighNa安装的主要文件。
可以运行以下命令查询已经安装的vnpy模块。

pip list
© 2015-2022 上海韦纳软件科技有限公司
备案服务号:沪ICP备18006526号

沪公网安备 31011502017034号

【用户协议】
【隐私政策】
【免责条款】