构建 Python 模块

Python 中调用 PHP 的函数时需要先构建 Pythonphpy 模块。

编译依赖

  • cmake 3.16 或更高版本
  • php 8.1 (embed) 或更高版本,编译 PHP 时需要增加 --enable-embed 参数
  • Python 3.8 或更高版本

编译配置

PHP_CONFIG

指定 php-config 指令的路径,默认为相对路径,例如:

  1. cmake . -D PHP_CONFIG=/usr/local/php/bin/php-config

PYTHON_CONFIG

指定 python-config 指令的路径,默认为相对路径,例如:

  1. cmake . -D PHP_CONFIG=/usr/local/bin/python3-config

构建

  1. make -j 4

编译成功后在 tests/lib 目录下会生成 phpy.so,可以将此文件复制到任意 Pythonsys.path 目录中。

conda 工具

可使用 conda 工具来管理 Python 环境。

创建 Python 环境

  1. conda env create -n py38 python=3.8
  2. # 激活
  3. conda activate py38

pip 加速

  1. # 阿里云
  2. pip config set global.index-url https://mirrors.aliyun.com/pypi/simple
  3. # 清华源
  4. pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple

单元测试

  1. pip install pytest
  2. pytest -v tests/