如何创建此软件包的安装文件?

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

我正在开发 CSXCAD python 模块。当前安装它的过程是运行:

python setup.py build_ext -I ~/opt/openEMS/include -L ~/opt/openEMS/lib -R ~/opt/openEMS/lib
python setup.py install

我想创建

pyproject.toml
以及其他需要的东西,以便能够简单地使用
pip install .
安装它。我创建了以下文件:

pyproject.toml

[build-system]
requires = ["setuptools","cython","numpy"]
build-backend = "setuptools.build_meta"

[tool.setuptools]
py-modules = ["CSXCAD"]

[project]
name = "CSXCAD"
version = "0.0.1"
authors = [
  { name="Example Author", email="[email protected]" },
]
description = "A small example package"
readme = "README.md"
requires-python = ">=3.8"
classifiers = [
    "Programming Language :: Python :: 3",
    "License :: OSI Approved :: MIT License",
    "Operating System :: OS Independent",
]

[tool.setuptools.cmdclass]
build_py = "_custom_build.build_py"

_custom_build.py

from setuptools import Extension
from setuptools.command.build_py import build_py as _build_py
from pathlib import Path

class build_py(_build_py):
    def run(self):
        self.run_command("build_ext")
        return super().run()

    def initialize_options(self):
        super().initialize_options()
        if self.distribution.ext_modules == None:
            self.distribution.ext_modules = []

        self.distribution.ext_modules.append(
            Extension(
                "*", 
                [Path(__file__).parent/"CSXCAD/*.pyx"],
                language = "c++",
                libraries = ['CSXCAD'],
            ),
        )

它失败了:

$ pip install .
Processing /home/crazyscientist/opt/openEMS/openEMS-Project/CSXCAD/python
  Installing build dependencies ... done
  Getting requirements to build wheel ... done
  Preparing metadata (pyproject.toml) ... done
Building wheels for collected packages: CSXCAD
  Building wheel for CSXCAD (pyproject.toml) ... error
  error: subprocess-exited-with-error
  
  × Building wheel for CSXCAD (pyproject.toml) did not run successfully.
  │ exit code: 1
  ╰─> [35 lines of output]
      running bdist_wheel
      running build
      running build_py
      running build_ext
      /tmp/pip-build-env-mx4miiwt/overlay/lib64/python3.12/site-packages/Cython/Compiler/Main.py:381: FutureWarning: Cython directive 'language_level' not set, using '3str' for now (Py3). This has changed from earlier releases! File: /home/crazyscientist/opt/openEMS/openEMS-Project/CSXCAD/python/CSXCAD/CSPrimitives.pxd
        tree = Parsing.p_module(s, pxd, full_module_name)
      /tmp/pip-build-env-mx4miiwt/overlay/lib64/python3.12/site-packages/Cython/Compiler/Main.py:381: FutureWarning: Cython directive 'language_level' not set, using '3str' for now (Py3). This has changed from earlier releases! File: /home/crazyscientist/opt/openEMS/openEMS-Project/CSXCAD/python/CSXCAD/CSProperties.pxd
        tree = Parsing.p_module(s, pxd, full_module_name)
      /tmp/pip-build-env-mx4miiwt/overlay/lib64/python3.12/site-packages/Cython/Compiler/Main.py:381: FutureWarning: Cython directive 'language_level' not set, using '3str' for now (Py3). This has changed from earlier releases! File: /home/crazyscientist/opt/openEMS/openEMS-Project/CSXCAD/python/CSXCAD/CSRectGrid.pxd
        tree = Parsing.p_module(s, pxd, full_module_name)
      /tmp/pip-build-env-mx4miiwt/overlay/lib64/python3.12/site-packages/Cython/Compiler/Main.py:381: FutureWarning: Cython directive 'language_level' not set, using '3str' for now (Py3). This has changed from earlier releases! File: /home/crazyscientist/opt/openEMS/openEMS-Project/CSXCAD/python/CSXCAD/CSTransform.pxd
        tree = Parsing.p_module(s, pxd, full_module_name)
      /tmp/pip-build-env-mx4miiwt/overlay/lib64/python3.12/site-packages/Cython/Compiler/Main.py:381: FutureWarning: Cython directive 'language_level' not set, using '3str' for now (Py3). This has changed from earlier releases! File: /home/crazyscientist/opt/openEMS/openEMS-Project/CSXCAD/python/CSXCAD/CSXCAD.pxd
        tree = Parsing.p_module(s, pxd, full_module_name)
      /tmp/pip-build-env-mx4miiwt/overlay/lib64/python3.12/site-packages/Cython/Compiler/Main.py:381: FutureWarning: Cython directive 'language_level' not set, using '3str' for now (Py3). This has changed from earlier releases! File: /home/crazyscientist/opt/openEMS/openEMS-Project/CSXCAD/python/CSXCAD/ParameterObjects.pxd
        tree = Parsing.p_module(s, pxd, full_module_name)
      Compiling /home/crazyscientist/opt/openEMS/openEMS-Project/CSXCAD/python/CSXCAD/CSPrimitives.pyx because it depends on /tmp/pip-build-env-mx4miiwt/overlay/lib64/python3.12/site-packages/Cython/Includes/libc/string.pxd.
      Compiling /home/crazyscientist/opt/openEMS/openEMS-Project/CSXCAD/python/CSXCAD/CSProperties.pyx because it depends on /tmp/pip-build-env-mx4miiwt/overlay/lib64/python3.12/site-packages/Cython/Includes/libc/string.pxd.
      Compiling /home/crazyscientist/opt/openEMS/openEMS-Project/CSXCAD/python/CSXCAD/CSRectGrid.pyx because it depends on /tmp/pip-build-env-mx4miiwt/overlay/lib64/python3.12/site-packages/Cython/Includes/libc/string.pxd.
      Compiling /home/crazyscientist/opt/openEMS/openEMS-Project/CSXCAD/python/CSXCAD/CSTransform.pyx because it depends on /tmp/pip-build-env-mx4miiwt/overlay/lib64/python3.12/site-packages/Cython/Includes/libc/string.pxd.
      Compiling /home/crazyscientist/opt/openEMS/openEMS-Project/CSXCAD/python/CSXCAD/CSXCAD.pyx because it depends on /tmp/pip-build-env-mx4miiwt/overlay/lib64/python3.12/site-packages/Cython/Includes/libc/string.pxd.
      Compiling /home/crazyscientist/opt/openEMS/openEMS-Project/CSXCAD/python/CSXCAD/ParameterObjects.pyx because it depends on /tmp/pip-build-env-mx4miiwt/overlay/lib64/python3.12/site-packages/Cython/Includes/libc/string.pxd.
      [1/6] Cythonizing /home/crazyscientist/opt/openEMS/openEMS-Project/CSXCAD/python/CSXCAD/CSPrimitives.pyx
      [2/6] Cythonizing /home/crazyscientist/opt/openEMS/openEMS-Project/CSXCAD/python/CSXCAD/CSProperties.pyx
      [3/6] Cythonizing /home/crazyscientist/opt/openEMS/openEMS-Project/CSXCAD/python/CSXCAD/CSRectGrid.pyx
      [4/6] Cythonizing /home/crazyscientist/opt/openEMS/openEMS-Project/CSXCAD/python/CSXCAD/CSTransform.pyx
      [5/6] Cythonizing /home/crazyscientist/opt/openEMS/openEMS-Project/CSXCAD/python/CSXCAD/CSXCAD.pyx
      [6/6] Cythonizing /home/crazyscientist/opt/openEMS/openEMS-Project/CSXCAD/python/CSXCAD/ParameterObjects.pyx
      building '*' extension
      g++ -fno-strict-overflow -Wsign-compare -DDYNAMIC_ANNOTATIONS_ENABLED=1 -DNDEBUG -fexceptions -fcf-protection -fexceptions -fcf-protection -fexceptions -fcf-protection -fPIC -I/home/crazyscientist/opt/openEMS/openEMS_venv/include -I/usr/include/python3.12 -c /home/crazyscientist/opt/openEMS/openEMS-Project/CSXCAD/python/CSXCAD/CSPrimitives.cpp -o build/temp.linux-x86_64-cpython-312/home/crazyscientist/opt/openEMS/openEMS-Project/CSXCAD/python/CSXCAD/CSPrimitives.o
      /home/crazyscientist/opt/openEMS/openEMS-Project/CSXCAD/python/CSXCAD/CSPrimitives.cpp:1257:10: fatal error: CSXCAD/ParameterObjects.h: No such file or directory
       1257 | #include "CSXCAD/ParameterObjects.h"
            |          ^~~~~~~~~~~~~~~~~~~~~~~~~~~
      compilation terminated.
      error: command '/usr/bin/g++' failed with exit code 1
      [end of output]
  
  note: This error originates from a subprocess, and is likely not a problem with pip.
  ERROR: Failed building wheel for CSXCAD
Failed to build CSXCAD
ERROR: ERROR: Failed to build installable wheels for some pyproject.toml based projects (CSXCAD)

我猜这是因为

-I ~/opt/openEMS/include -L ~/opt/openEMS/lib -R ~/opt/openEMS/lib
选项没有被使用,但我不知道把它们放在哪里。

python packaging
1个回答
0
投票

看起来编译器找不到必要的头文件,特别是“CSXCAD/ParameterObjects.h” 尝试像这样更新 your_custom_build.py

import os
from setuptools import Extension
from setuptools.command.build_py import build_py as _build_py
from pathlib import Path

class build_py(_build_py):
  def run(self):
      self.run_command("build_ext")
      return super().run()

  def initialize_options(self):
      super().initialize_options()
      if self.distribution.ext_modules == None:
          self.distribution.ext_modules = []

      openems_path = os.environ.get('OPENEMS_PATH', '~/opt/openEMS')
      openems_path = os.path.expanduser(openems_path)

      self.distribution.ext_modules.append(
          Extension(
              "*", 
              [Path(__file__).parent/"CSXCAD/*.pyx"],
              language="c++",
              libraries=['CSXCAD'],
              include_dirs=[f"{openems_path}/include"],
              library_dirs=[f"{openems_path}/lib"],
              runtime_library_dirs=[f"{openems_path}/lib"],
              extra_compile_args=['-std=c++11'],  # Add this if you need C++11 features
          ),
      )

并更新 pyproject.toml 文件以包含一些构建要求:

[build-system]
requires = ["setuptools", "cython", "numpy", "wheel"]
build-backend = "setuptools.build_meta"

[tool.setuptools]
py-modules = ["CSXCAD"]

[project]
name = "CSXCAD"
version = "0.0.1"
authors = [
  { name="Example Author", email="[email protected]" },
]
description = "A small example package"
readme = "README.md"
requires-python = ">=3.8"
classifiers = [
    "Programming Language :: Python :: 3",
    "License :: OSI Approved :: MIT License",
    "Operating System :: OS Independent",
]

[tool.setuptools.cmdclass]
build_py = "_custom_build.build_py"

您可以自定义特定的构建环境。希望有帮助。

© www.soinside.com 2019 - 2024. All rights reserved.