繁体   English   中英

我可以为 pip 指定安装时间依赖项吗?

[英]Can I specify install time dependencies for pip?

我正在寻找一种方法来模仿 pip pip installpyproject.toml行为。

pyproject.toml可以指定构建时依赖项,例如,如果setup.py需要额外的包来构建:

安装程序.py

from setuptools import setup
import colorama # e.g. - non-standard package
setup(...)

这可以由pyproject.toml处理

[build-system]
requires = [
    "colorama",  # required for running custom code in setup.py
    "setuptools>=40.8.0",  # required by doc (https://pip.pypa.io/en/stable/reference/build-system/pyproject-toml/)
    "wheel", # required by doc (https://pip.pypa.io/en/stable/reference/build-system/pyproject-toml/)
]
build-backend = "setuptools.build_meta:__legacy__" # required by doc (https://pip.pypa.io/en/stable/reference/build-system/pyproject-toml/)

这将允许python -m build运行。

但是,这并不能解决pip install <path>的问题,因为 pyproject.toml 仅处理构建。

有没有办法安装呢?

我唯一的想法是

  1. 添加一个包装器来安装我不想安装的所需 pkg(希望只坚持pip install
  2. 调用subprocess.run(f'pip install {pkg}'); importlib.reload(pkg) subprocess.run(f'pip install {pkg}'); importlib.reload(pkg)这是hacky

有没有更好的办法?

我已经在评论中发布了答案,但是由于这已被贬低,因此这是官方答案:

from pip._internal.commands import create_command

package_names = ['colorama']  # packages to install
arguments = ['--upgrade'] # extra arguments
command = create_command("install", isolated=True) # don't know what isolated does, so maybe that needs to change
command.main(package_names + arguments) # execute it

请注意,这是原始的 function,调用所有这些函数可能更安全

def main(args=None):
    # type: (Optional[List[str]]) -> int
    if args is None:
        args = sys.argv[1:]

    # Configure our deprecation warnings to be sent through loggers
    # deprecation.install_warning_logger() # don't call this

    autocomplete()

    try:
        cmd_name, cmd_args = parse_command(args)
    except PipError as exc:
        sys.stderr.write(f"ERROR: {exc}")
        sys.stderr.write(os.linesep)
        sys.exit(1)

    # Needed for locale.getpreferredencoding(False) to work
    # in pip._internal.utils.encoding.auto_decode
    try:
        locale.setlocale(locale.LC_ALL, "")
    except locale.Error as e:
        # setlocale can apparently crash if locale are uninitialized
        logger.debug("Ignoring error %s when setting locale", e)
    command = create_command(cmd_name, isolated=("--isolated" in cmd_args))

    return command.main(cmd_args)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM