@@ -25,12 +25,15 @@ use uv_distribution_types::{
2525 IndexUrl , Name , NameRequirementSpecification , Requirement , RequirementSource ,
2626 UnresolvedRequirement , UnresolvedRequirementSpecification ,
2727} ;
28+ use uv_fs:: CWD ;
2829use uv_fs:: Simplified ;
2930use uv_installer:: { InstallationStrategy , SatisfiesResult , SitePackages } ;
3031use uv_normalize:: PackageName ;
3132use uv_pep440:: { VersionSpecifier , VersionSpecifiers } ;
3233use uv_pep508:: MarkerTree ;
3334use uv_preview:: Preview ;
35+ use uv_python:: PythonVersionFile ;
36+ use uv_python:: VersionFileDiscoveryOptions ;
3437use uv_python:: {
3538 EnvironmentPreference , PythonDownloads , PythonEnvironment , PythonInstallation ,
3639 PythonPreference , PythonRequest ,
@@ -699,43 +702,38 @@ async fn get_or_create_environment(
699702) -> Result < ( ToolRequirement , PythonEnvironment ) , ProjectError > {
700703 let reporter = PythonDownloadReporter :: single ( printer) ;
701704
702- // Figure out what Python we're targeting, either explicitly like `uvx python@3`, or via the
703- // -p/--python flag.
704- let python_request = match request {
705- ToolRequest :: Python {
706- request : tool_python_request,
707- ..
708- } => {
709- match python {
710- None => Some ( tool_python_request. clone ( ) ) ,
711-
712- // The user is both invoking a python interpreter directly and also supplying the
713- // -p/--python flag. Cases like `uvx -p pypy python` are allowed, for two reasons:
714- // 1) Previously this was the only way to invoke e.g. PyPy via `uvx`, and it's nice
715- // to remain compatible with that. 2) A script might define an alias like `uvx
716- // --python $MY_PYTHON ...`, and it's nice to be able to run the interpreter
717- // directly while sticking to that alias.
718- //
719- // However, we want to error out if we see conflicting or redundant versions like
720- // `uvx -p python38 python39`.
721- //
722- // Note that a command like `uvx default` doesn't bring us here. ToolRequest::parse
723- // returns ToolRequest::Package rather than ToolRequest::Python in that case. See
724- // PythonRequest::try_from_tool_name.
725- Some ( python_flag) => {
726- if tool_python_request != & PythonRequest :: Default {
727- return Err ( anyhow:: anyhow!(
728- "Received multiple Python version requests: `{}` and `{}`" ,
729- python_flag. to_string( ) . cyan( ) ,
730- tool_python_request. to_canonical_string( ) . cyan( )
731- )
732- . into ( ) ) ;
733- }
734- Some ( PythonRequest :: parse ( python_flag) )
735- }
736- }
705+ // Determine explicit Python version requests
706+ let explicit_python_request = python. map ( PythonRequest :: parse) ;
707+ let tool_python_request = match request {
708+ ToolRequest :: Python { request, .. } => Some ( request. clone ( ) ) ,
709+ ToolRequest :: Package { .. } => None ,
710+ } ;
711+
712+ // Resolve Python request with version file lookup when no explicit request
713+ let python_request = match ( explicit_python_request, tool_python_request) {
714+ // e.g., `uvx --python 3.10 python3.12`
715+ ( Some ( explicit) , Some ( tool_request) ) if tool_request != PythonRequest :: Default => {
716+ // Conflict: both --python flag and versioned tool name
717+ return Err ( anyhow:: anyhow!(
718+ "Received multiple Python version requests: `{}` and `{}`" ,
719+ explicit. to_canonical_string( ) . cyan( ) ,
720+ tool_request. to_canonical_string( ) . cyan( )
721+ )
722+ . into ( ) ) ;
737723 }
738- ToolRequest :: Package { .. } => python. map ( PythonRequest :: parse) ,
724+ // e.g, `uvx --python 3.10 ...`
725+ ( Some ( explicit) , _) => Some ( explicit) ,
726+ // e.g., `uvx python` or `uvx <tool>`
727+ ( None , Some ( PythonRequest :: Default ) | None ) => PythonVersionFile :: discover (
728+ & * CWD ,
729+ & VersionFileDiscoveryOptions :: default ( )
730+ . with_no_config ( false )
731+ . with_no_local ( true ) ,
732+ )
733+ . await ?
734+ . and_then ( PythonVersionFile :: into_version) ,
735+ // e.g., `uvx python3.12`
736+ ( None , Some ( tool_request) ) => Some ( tool_request) ,
739737 } ;
740738
741739 // Discover an interpreter.
0 commit comments