I'm looking for a way to mimic pyproject.toml
behavior for pip install
.
pyproject.toml
can specify build-time dependencies, eg if a setup.py
needs extra packages to build:
setup.py
from setuptools import setup
import colorama # e.g. - non-standard package
setup(...)
This can be handled by a 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/)
which will allow python -m build
to run.
However, this doesn't solve the issue for pip install <path>
, due to pyproject.toml only handling build.
Is there a way to do so for installation as well?
My only thoughts are
pip install
)subprocess.run(f'pip install {pkg}'); importlib.reload(pkg)
subprocess.run(f'pip install {pkg}'); importlib.reload(pkg)
which is hacky Is there a better way?
I have already posted the asnswer in the comments, but since that's depricated here's the official answer:
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
Note that this is the original function, and it's probably safer to call al those functions
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)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.