Specify setup time dependency with `--global-option` for a python package

落花浮王杯 提交于 2019-12-11 15:34:33

问题


I'm trying to package a python library that has setup-time (and also run-time) dependencies: it imports the modules so that the modules can inform the setup process of the location of some provided C headers:

from distutils.extension import Extension
from pybedtools.helpers import get_includes as pybedtools_get_includes
from pysam import get_include as pysam_get_include
# [...]
extensions = [
    Extension(
        "bam25prime.libcollapsesam", ["bam25prime/libcollapsesam.pyx"],
        include_dirs=pysam_get_include()),
    Extension(
        "bam25prime.libcollapsebed", ["bam25prime/libcollapsebed.pyx"],
        include_dirs=pybedtools_get_includes(),
        language="c++"),
    ]
# [...]

However, one of the dependencies (pybedtools) needs to be installed with a specific --global-option pip option (see at the end of the post what happens when the option is not provided).

If I understand correctly, the currently up-to-date way to automatically have some dependencies available before setup.py is used is to indicate them in the [build-system] section of a pyproject.toml file.

I tried the following pyproject.toml:

[build-system]
requires = [
    "pysam",
    "pybedtools @ git+https://github.com/blaiseli/pybedtools.git@fix_missing_headers --global-option='cythonize'",
]
build-backend = "setuptools.build_meta"

(By the way, it took me quite some time to figure out how to specify the build-backend, the documentation is not easily discoverable.)

However, this generates the following error upon pip install:

  ERROR: Invalid requirement: "pybedtools @ git+https://github.com/blaiseli/pybedtools.git@fix_missing_headers --global-option='cythonize'"
  Hint: It looks like a path. File 'pybedtools @ git+https://github.com/blaiseli/pybedtools.git@fix_missing_headers --global-option='cythonize'' does not exist.

How can I correctly specify options for dependencies ?

If I simply specify the package and its URL ("pybedtools @ git+https://github.com/blaiseli/pybedtools.git@fix_missing_headers), the install fails as follows:

    Exception:
                            Cython-generated file 'pybedtools/cbedtools.cpp' not found.

                            Please install Cython and run

                                python setup.py cythonize

It was while trying to tackle the above error that I found out about the --global-option pip option. I can manually run pip install --global-option="cythonize" git+https://github.com/blaiseli/pybedtools.git@fix_missing_headers, and the install works, provided the dependencies of that package are already installed, otherwise their install fails because of an unrecognized "cythonize" option (which is another issue...).

Note that this option is only needed when installing "from source" (for instance when installing from a fork on github, as is my case here).


回答1:


Same thing as in your other question, I suspect cythonize is a setuptools command and not a global option.

If it's indeed the case, then you would be better off setting an alias in your setup.cfg. If you run python setup.py alias install cythonize install, this should add the following to your setup.cfg:

[aliases]
install = cythonize install

When running pip install later, pip will honor this alias and the cythonize command will be executed right before the install command.



来源:https://stackoverflow.com/questions/58379905/specify-setup-time-dependency-with-global-option-for-a-python-package

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!