创建使用 Cython 的 Python 包:ValueError

cmb*_*200 5 python installation setuptools cython setup.py

我的问题:

我正在尝试创建一个 python 包并将其上传到 PyPI。

然而,当我跑步时

python -m build
Run Code Online (Sandbox Code Playgroud)

在项目目录 ( example_package) 中,然后我得到以下输出和错误:

WARNING: Skipping example_package as it is not installed.
* Creating venv isolated environment...
* Installing packages in isolated environment... (Cython, numpy, setuptools, wheel)
* Getting dependencies for sdist...
running egg_info
creating example_package.egg-info
writing example_package.egg-info/PKG-INFO
writing dependency_links to example_package.egg-info/dependency_links.txt
writing top-level names to example_package.egg-info/top_level.txt
writing manifest file 'example_package.egg-info/SOURCES.txt'
reading manifest file 'example_package.egg-info/SOURCES.txt'
adding license file 'LICENSE'
writing manifest file 'example_package.egg-info/SOURCES.txt'
* Building sdist...
running sdist
running egg_info
writing example_package.egg-info/PKG-INFO
writing dependency_links to example_package.egg-info/dependency_links.txt
writing top-level names to example_package.egg-info/top_level.txt
reading manifest file 'example_package.egg-info/SOURCES.txt'
adding license file 'LICENSE'
writing manifest file 'example_package.egg-info/SOURCES.txt'
running check
creating example_package-0.1.0
creating example_package-0.1.0/example_package
creating example_package-0.1.0/example_package.egg-info
creating example_package-0.1.0/example_package/cython_files
copying files to example_package-0.1.0...
copying LICENSE -> example_package-0.1.0
copying README.md -> example_package-0.1.0
copying pyproject.toml -> example_package-0.1.0
copying setup.py -> example_package-0.1.0
copying example_package/__init__.py -> example_package-0.1.0/example_package
copying example_package/example.py -> example_package-0.1.0/example_package
copying example_package.egg-info/PKG-INFO -> example_package-0.1.0/example_package.egg-info
copying example_package.egg-info/SOURCES.txt -> example_package-0.1.0/example_package.egg-info
copying example_package.egg-info/dependency_links.txt -> example_package-0.1.0/example_package.egg-info
copying example_package.egg-info/top_level.txt -> example_package-0.1.0/example_package.egg-info
copying example_package/cython_files/file.c -> example_package-0.1.0/example_package/cython_files
copying example_package/cython_files/file2.c -> example_package-0.1.0/example_package/cython_files
Writing example_package-0.1.0/setup.cfg
Creating tar archive
removing 'example_package-0.1.0' (and everything under it)
* Building wheel from sdist
* Creating venv isolated environment...
* Installing packages in isolated environment... (Cython, numpy, setuptools, wheel)
* Getting dependencies for wheel...
Traceback (most recent call last):
  File "/venv/lib/python3.9/site-packages/pep517/in_process/_in_process.py", line 351, in <module>
    main()
  File "/venv/lib/python3.9/site-packages/pep517/in_process/_in_process.py", line 333, in main
    json_out['return_val'] = hook(**hook_input['kwargs'])
  File "/venv/lib/python3.9/site-packages/pep517/in_process/_in_process.py", line 118, in get_requires_for_build_wheel
    return hook(config_settings)
  File "/private/var/folders/nm/x6s726c95qnc5hzvhgy7pzpm0000gn/T/build-env-j__lzyqr/lib/python3.9/site-packages/setuptools/build_meta.py", line 338, in get_requires_for_build_wheel
    return self._get_build_requires(config_settings, requirements=['wheel'])
  File "/private/var/folders/nm/x6s726c95qnc5hzvhgy7pzpm0000gn/T/build-env-j__lzyqr/lib/python3.9/site-packages/setuptools/build_meta.py", line 320, in _get_build_requires
    self.run_setup()
  File "/private/var/folders/nm/x6s726c95qnc5hzvhgy7pzpm0000gn/T/build-env-j__lzyqr/lib/python3.9/site-packages/setuptools/build_meta.py", line 335, in run_setup
    exec(code, locals())
  File "<string>", line 10, in <module>
  File "/private/var/folders/nm/x6s726c95qnc5hzvhgy7pzpm0000gn/T/build-env-j__lzyqr/lib/python3.9/site-packages/Cython/Build/Dependencies.py", line 970, in cythonize
    module_list, module_metadata = create_extension_list(
  File "/private/var/folders/nm/x6s726c95qnc5hzvhgy7pzpm0000gn/T/build-env-j__lzyqr/lib/python3.9/site-packages/Cython/Build/Dependencies.py", line 816, in create_extension_list
    for file in nonempty(sorted(extended_iglob(filepattern)), "'%s' doesn't match any files" % filepattern):
  File "/private/var/folders/nm/x6s726c95qnc5hzvhgy7pzpm0000gn/T/build-env-j__lzyqr/lib/python3.9/site-packages/Cython/Build/Dependencies.py", line 114, in nonempty
    raise ValueError(error_msg)
ValueError: 'example_package/cython_files/file.pyx' doesn't match any files

ERROR Backend subprocess exited when trying to invoke get_requires_for_build_wheel

Run Code Online (Sandbox Code Playgroud)

项目结构:

example_package
|- pyproject.toml
|- setup.py
|- LICENSE, MANIFEST, README.md
|- example_package
   |- __init__.py
   |- example.py
   |- cython_files
      |- file.pyx
      |- file2.pyx
      |- file2.pxd
Run Code Online (Sandbox Code Playgroud)

__init__.py(注意:目录中没有文件cython_files,因为它会在 cythonizing 和编译时导致崩溃。相反,我手动添加目录cython_filessys.path

安装程序.py

from setuptools import Extension, setup
from Cython.Build import cythonize
from Cython.Distutils import build_ext

extensions = [Extension('example_package.cython_files.file',
                        ['example_package/cython_files/file.pyx']),
              Extension('example_package.cython_files.file2',
                        ['example_package/cython_files/file2.pyx'])]

extensions = cythonize(extensions)

requirements = ['Cython', 'numpy',]

setup(
    name='example_package',
    version='0.1.0',
    ext_modules=extensions,
    packages=['example_package', 'example_package.cython_files'],
    cmdclass={'build_ext': build_ext},
    package_data = {
        'example_package/cython_files': ['file2.pxd'],
    }
)
Run Code Online (Sandbox Code Playgroud)

pyproject.toml

[build-system]
requires = ['setuptools',
            'wheel',
            'Cython',
            'numpy']
build-backend = "setuptools.build_meta"
Run Code Online (Sandbox Code Playgroud)

示例.py

class Example:
    def add_one(self, number):
        return number + 1

    def use_cython(self, number):
        from file import Functions
        f = Functions()
        return f.f(1.)
Run Code Online (Sandbox Code Playgroud)

__初始化__.py

# Manually add 'cython_files' to the path
import sys
from pathlib import Path
sys.path.insert(1, str(Path(__file__).parent / 'cython_files'))
Run Code Online (Sandbox Code Playgroud)

file.pyx(一些不重要的计算)

from file2 cimport File2

cdef class Functions:
    cdef object file2

    def __init__(self):
        self.file2 = File2(5.0)

    cpdef double f(self, double x):
        return x + 1.0

    cpdef double f2(self, double x):
        return self.f(self.file2.add(x))
Run Code Online (Sandbox Code Playgroud)

file2.pyx(一些不重要的计算)

cdef class File2:
    cdef double a

    def __init__(self, a):
        self.a = a

    cpdef double add(self, double b):
        return self.a + b
Run Code Online (Sandbox Code Playgroud)

file2.pxd(一些不重要的计算)

cdef class File2:
    cdef double a

    cpdef double add(self, double b)
Run Code Online (Sandbox Code Playgroud)

有趣的是,当我运行以下命令序列时,我可以安装该软件包:

  1. 删除该extensions = cythonize(extensions)setup.py
  2. 运行命令python -m build。这会输出一个错误。
  3. 将该行放extensions = cythonize(extensions)回到文件中setup.py原来的位置。
  4. 运行命令python -m build。这样就成功了。

但是,这不是永久解决方案,因为我只想运行python -m build.

G. *_*ron 0

我刚刚设法解决了同样的错误。当构建系统调用 setup.py 从 sdist 生成轮子时,会发生错误。问题,这些*.pyx文件不在 sdist 中,因此错误中包含这一行:

ValueError: 'example_package/cython_files/file.pyx' doesn't match any files
Run Code Online (Sandbox Code Playgroud)

*.pyx我通过将文件添加到 sdist解决了这个问题。这对应于package_data中的论点setup.py

具体来说,就您的情况而言,我认为将其更改为以下内容可以解决问题:

    package_data = {
        'example_package/cython_files': ['file2.pxd','file.pyx','file2.pyx'],
    }
Run Code Online (Sandbox Code Playgroud)

如果这不起作用,那么一些变化肯定会起作用。