@@ -713,7 +713,8 @@ fn interpreter_satisfies_environment_preference(
713
713
/// This is useful as a pre-filtering step. Use of [`interpreter_satisfies_environment_preference`]
714
714
/// is required to determine if an [`Interpreter`] satisfies the preference.
715
715
///
716
- /// The interpreter path is only used for debug messages.
716
+ /// For [`PythonSource::SearchPath`], this uses the interpreter path to sniff if the executable is
717
+ /// present in a virtual environment by searching for a `pyvenv.cfg` file.
717
718
fn source_satisfies_environment_preference (
718
719
source : PythonSource ,
719
720
interpreter_path : & Path ,
@@ -723,7 +724,16 @@ fn source_satisfies_environment_preference(
723
724
EnvironmentPreference :: Any => true ,
724
725
EnvironmentPreference :: OnlyVirtual => {
725
726
if source. is_maybe_virtualenv ( ) {
726
- true
727
+ if matches ! ( source, PythonSource :: SearchPath ) {
728
+ interpreter_path
729
+ . parent ( )
730
+ . and_then ( Path :: parent)
731
+ . is_some_and ( |path| {
732
+ path. join ( "pyvenv.cfg" ) . try_exists ( ) . unwrap_or_default ( )
733
+ } )
734
+ } else {
735
+ true
736
+ }
727
737
} else {
728
738
debug ! (
729
739
"Ignoring Python interpreter at `{}`: only virtual environments allowed" ,
@@ -1624,21 +1634,16 @@ impl PythonSource {
1624
1634
}
1625
1635
1626
1636
/// Whether this source **could** be a virtual environment.
1627
- ///
1628
- /// This excludes the [`PythonSource::SearchPath`] although it could be in a virtual
1629
- /// environment; pragmatically, that's not common and saves us from querying a bunch of system
1630
- /// interpreters for no reason. It seems dubious to consider an interpreter in the `PATH` as a
1631
- /// target virtual environment if it's not discovered through our virtual environment-specific
1632
- /// patterns.
1633
1637
pub ( crate ) fn is_maybe_virtualenv ( self ) -> bool {
1634
1638
match self {
1635
1639
Self :: ProvidedPath
1636
1640
| Self :: ActiveEnvironment
1637
1641
| Self :: DiscoveredEnvironment
1638
1642
| Self :: CondaPrefix
1639
1643
| Self :: BaseCondaPrefix
1640
- | Self :: ParentInterpreter => true ,
1641
- Self :: Managed | Self :: SearchPath | Self :: Registry | Self :: MicrosoftStore => false ,
1644
+ | Self :: ParentInterpreter
1645
+ | Self :: SearchPath => true ,
1646
+ Self :: Managed | Self :: Registry | Self :: MicrosoftStore => false ,
1642
1647
}
1643
1648
}
1644
1649
0 commit comments