You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The version constraints in your pyproject.toml do get packaged and published to PyPI as part of your project's metadata. However, when you do pip install --upgrade <package> after installation, pip doesn't automatically respect these constraints from your package's dependencies.
Here are a few approaches to handle this:
If you're installing additional packages programmatically, you can use the package metadata to access your pinned versions:
fromimportlib.metadataimportrequirespackage_requirements=requires('your-package-name')
# This will give you the requirements as specified in pyproject.toml
A more robust solution would be to define these additional installations as "extras" in your pyproject.toml:
If you must install packages programmatically, you can enforce version constraints using pip's constraint file:
importsubprocessimportpkg_resourcesdefsafe_install_package(package_name):
# Get the pinned version from your package's requirementsdist=pkg_resources.get_distribution('your-package-name')
constraints= [str(r) forrindist.requires()]
# Write constraints to a temporary filewithopen('constraints.txt', 'w') asf:
f.write('\n'.join(constraints))
# Install using constraintssubprocess.check_call([
'pip', 'install',
'-c', 'constraints.txt',
package_name
])
Could you share more about why you need to install additional packages at runtime? There might be better architectural solutions depending on your specific use case.
Also, consider using install_requires vs extras_require in your package setup - this way users can choose to install these additional dependencies when they first install your package:
pipinstallyour-package[additional]
This would help avoid the version conflicts you're experiencing by making all dependencies explicit at install time.
The text was updated successfully, but these errors were encountered:
The version constraints in your
pyproject.toml
do get packaged and published to PyPI as part of your project's metadata. However, when you dopip install --upgrade <package>
after installation, pip doesn't automatically respect these constraints from your package's dependencies.Here are a few approaches to handle this:
pyproject.toml
:Could you share more about why you need to install additional packages at runtime? There might be better architectural solutions depending on your specific use case.
Also, consider using
install_requires
vsextras_require
in your package setup - this way users can choose to install these additional dependencies when they first install your package:This would help avoid the version conflicts you're experiencing by making all dependencies explicit at install time.
The text was updated successfully, but these errors were encountered: