1616from pip ._internal .build_env import SubprocessBuildEnvironmentInstaller
1717from pip ._internal .cache import WheelCache
1818from pip ._internal .cli import cmdoptions
19+ from pip ._internal .cli .cmdoptions import make_target_python
1920from pip ._internal .cli .index_command import IndexGroupCommand
2021from pip ._internal .cli .index_command import SessionCommandMixin as SessionCommandMixin
21- from pip ._internal .exceptions import CommandError , PreviousBuildDirError
22+ from pip ._internal .exceptions import (
23+ CommandError ,
24+ PreviousBuildDirError ,
25+ UnsupportedPythonVersion ,
26+ )
2227from pip ._internal .index .collector import LinkCollector
2328from pip ._internal .index .package_finder import PackageFinder
2429from pip ._internal .models .selection_prefs import SelectionPreferences
3237 install_req_from_parsed_requirement ,
3338 install_req_from_req_string ,
3439)
40+ from pip ._internal .req .pep723 import PEP723Exception , pep723_metadata
3541from pip ._internal .req .req_dependency_group import parse_dependency_groups
3642from pip ._internal .req .req_file import parse_requirements
3743from pip ._internal .req .req_install import InstallRequirement
3844from pip ._internal .resolution .base import BaseResolver
45+ from pip ._internal .utils .packaging import check_requires_python
3946from pip ._internal .utils .temp_dir import (
4047 TempDirectory ,
4148 TempDirectoryTypeRegistry ,
@@ -305,6 +312,38 @@ def get_requirements(
305312 )
306313 requirements .append (req_to_add )
307314
315+ if options .requirements_from_scripts :
316+ if len (options .requirements_from_scripts ) > 1 :
317+ raise CommandError ("--requirements-from-script can only be given once" )
318+
319+ script = options .requirements_from_scripts [0 ]
320+ try :
321+ script_metadata = pep723_metadata (script )
322+ except PEP723Exception as exc :
323+ raise CommandError (exc .msg )
324+
325+ script_requires_python = script_metadata .get ("requires-python" , "" )
326+
327+ if script_requires_python and not options .ignore_requires_python :
328+ target_python = make_target_python (options )
329+
330+ if not check_requires_python (
331+ requires_python = script_requires_python ,
332+ version_info = target_python .py_version_info ,
333+ ):
334+ raise UnsupportedPythonVersion (
335+ f"Script { script !r} requires a different Python: "
336+ f"{ target_python .py_version } not in { script_requires_python !r} "
337+ )
338+
339+ for req in script_metadata .get ("dependencies" , []):
340+ req_to_add = install_req_from_req_string (
341+ req ,
342+ isolated = options .isolated_mode ,
343+ user_supplied = True ,
344+ )
345+ requirements .append (req_to_add )
346+
308347 # If any requirement has hash options, enable hash checking.
309348 if any (req .has_hash_options for req in requirements ):
310349 options .require_hashes = True
@@ -314,6 +353,7 @@ def get_requirements(
314353 or options .editables
315354 or options .requirements
316355 or options .dependency_groups
356+ or options .requirements_from_scripts
317357 ):
318358 opts = {"name" : self .name }
319359 if options .find_links :
0 commit comments