簡體   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