仅使用Pyproject.toml的Setuptools Python项目如何指定替代依赖源?
Got it, since you're relying solely on pyproject.toml (ditching setup.py and setup.cfg), setting up your custom Artifactory repository as a dependency source depends on which build/dependency manager you're using. Let's break down the two most common setups:
1. If you're using Poetry
Poetry leans heavily on pyproject.toml for all config, so adding your custom repo is straightforward. Just add a [[tool.poetry.source]] section to your file:
[[tool.poetry.source]] name = "my-artifactory" # A unique name for your repo url = "https://artifactory.mypypy.com/simple/" # Make sure it ends with /simple/ default = true # Set this if you want this repo to be the primary source (instead of PyPI) # Or use secondary = true if you want to use it alongside PyPI
- If you set
default = true, Poetry will look here first for all dependencies. - If you only need specific packages from this repo, you can mark them when adding dependencies:
poetry add my-package --source my-artifactory
2. If you're using Setuptools (with pyproject.toml)
For setuptools, you can configure the repository directly in the tool.setuptools section. If this is your primary index, use:
[tool.setuptools] index-url = "https://artifactory.mypypy.com/simple/"
If you need to add this as an extra index alongside PyPI, use:
[tool.setuptools] index-url = "https://pypi.org/simple/" # Keep PyPI as primary extra-index-url = ["https://artifactory.mypypy.com/simple/"]
Alternatively, use the newer, more flexible index list syntax for multiple repos:
[[tool.setuptools.indexes]] url = "https://artifactory.mypypy.com/simple/" name = "my-artifactory" priority = "high" # Use "low" if you want PyPI to take precedence
3. For direct pip usage (without a dedicated manager)
If you're just using pip to install from your pyproject.toml, add pip-specific config to point to your repo:
[[tool.pip.indexes]] url = "https://artifactory.mypypy.com/simple/"
Or, if you need to combine it with PyPI:
[[tool.pip.indexes]] url = "https://pypi.org/simple/" name = "pypi" [[tool.pip.indexes]] url = "https://artifactory.mypypy.com/simple/" name = "my-artifactory"
Quick Note on Authentication
If your Artifactory repo requires credentials, you can handle this via:
- A
~/.pypircfile with your repo name and credentials - Environment variables (check your tool's docs for exact variable names, e.g.,
MY_ARTIFACTORY_USERNAMEandMY_ARTIFACTORY_PASSWORD)
内容的提问来源于stack exchange,提问作者Milan

