如何自定义普通模式和开发模式下的python安装?

问题描述 投票:0回答:0

我正在构建一个

example_package
,我想在
normal
develop
模式下自定义安装过程。这是项目结构:

.
├── cpp
│   ├── CMakeLists.txt
│   └── helloworld.cpp
├── pyproject.toml
├── setup.py
└── src
    └── example_package
        └── __init__.py

每个文件的内容如下所示

  • pyproject.toml
    :添加了最少的信息。
[build-system]
requires = ["setuptools>=62.0.0", "wheel", "cmake>=3.20"]
build-backend = "setuptools.build_meta"

[project]
name = "example_package"
version = "0.0.1"
requires-python = ">=3.8"

[tool.setuptools.packages.find]
where = ["src"]
  • setup.py
from setuptools import setup
from setuptools.command import develop, build_py, install
import subprocess

class CustomDevelop(develop.develop):
    def run(self):
        super(CustomDevelop, self).run()
        print("Execute CustomDevelop ......")

class CustomBuildPy(build_py.build_py):
    def run(self):
        super(CustomBuildPy, self).run()
        print("Execute CustomBuildPy ......")

class CustomInstall(install.install):
    def run(self):
        super(CustomInstall, self).run()
        print("Execute CustomInstall ......")
        
setup(
    cmdclass={
        'develop': CustomDevelop,
        'build_py': CustomBuildPy,
        'install': CustomInstall
    }
)

安装

普通模式

  • 创建、激活
    venv
    ,并更新
    pip
    setuptools
    python3 -m venv venv/ && source venv/bin/activate && python3 -m pip install --upgrade pip setuptools
    
  • normal
    模式安装:
    python3 -m pip install -vv . 2>&1 | grep -E "running|Execute"
    
  • 输出信息:
  running egg_info
  running dist_info
  running bdist_wheel
  running build
  running build_py
  running egg_info
  Execute CustomBuildPy ......
  running install
  running install_lib
  running install_egg_info
  running install_scripts
  Execute CustomInstall ......

这是预期的。

CustomBuildPy
CustomInstall
被处决了。

开发模式

  • 创建、激活
    venv
    ,并更新
    pip
    setuptools
    python3 -m venv venv/ && source venv/bin/activate && python3 -m pip install --upgrade pip setuptools
    
  • develop
    模式安装:
    python3 -m pip install -vv -e . 2>&1 | grep -E "running|Execute"
    
  • 输出信息:
  running egg_info
  running dist_info
  running editable_wheel
  running build_py
  Execute CustomBuildPy ......
  running egg_info

这不是我所期望的,因为只有

CustomBuildPy
运行但没有
CustomDevelop
.

我真正想做的事

我之所以需要在正常和开发模式的安装过程中执行costumed命令是因为我有一个

helloworld.cpp
需要编译成可执行文件
helloworld
并放入
sys.path
。 比如
helloworld.cpp
的内容很琐碎:

#include <iostream>
int main(int argc, char **argv) {
  std::cout << "Hello World!!!\n";
  return 0;
}

相关的

CMakeLists.txt
也是微不足道的:

cmake_minimum_required(VERSION 3.20)
project(HelloWorld)
add_executable(helloworld helloworld.cpp)

CMakeLists.txt
helloworld.cpp
cpp
文件夹中,如:

.
├── cpp
│   ├── CMakeLists.txt
│   └── helloworld.cpp
├── pyproject.toml
├── setup.py
└── src
    └── example_package
        └── __init__.py

最后,我把

CustomBuildPy
改成:

class CustomBuildPy(build_py.build_py):
    def run(self):
        super(CustomBuildPy, self).run()
        print("Execute CustomBuildPy ......")
        subprocess.check_call("cmake -S cpp/ -B cpp/build && cmake --build cpp/build", shell=True)
        subprocess.check_call("cp cpp/build/helloworld build/lib/.", shell=True)

在正常模式下安装时,一切正常。可执行文件

helloworld
出现在
venv/lib/python3.8/site-packages
中。 但是在开发模式下安装时,出现错误:

  subprocess.CalledProcessError: Command 'cp -r cpp/build/helloworld build/lib/.' returned non-zero exit status 1.

因为

build
文件夹不是在源文件夹
.
.开发模式下创建的

我能做些什么来修复它?

pip setuptools python-packaging pep517
© www.soinside.com 2019 - 2024. All rights reserved.