'importing requirements from file to use in project

I would like to know if there's an existing approach/package/method for importing the requirements specified in a requirements.txt file into a python script. My naive idea would be to just read the file and have some logic to split each package with its version, but specifics add quickly (no version, == vs >= vs > vs ..., git packages, comments in file, etc.) and I was wondering if there's a "canonical" way of doing it. I know that the various package manager tools (pipenv, poetry, ...) need to have a way of doing it themselves, but the projects are so vast I'm unable to find the particular sections that do it.

Edit: to clarify, I need a way to use the requirements in my python script, not install them. Something like:

requirements = magical_function("requirements.txt")
for requirement in requirements:
  print(requirement.name, requirement.version)


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source