@@ -26,8 +26,8 @@ use crate::microsoft_store::find_microsoft_store_pythons;
2626#[ cfg( windows) ]
2727use crate :: py_launcher:: { registry_pythons, WindowsPython } ;
2828use crate :: virtualenv:: {
29- conda_prefix_from_env , virtualenv_from_env, virtualenv_from_working_dir,
30- virtualenv_python_executable,
29+ conda_environment_from_env , virtualenv_from_env, virtualenv_from_working_dir,
30+ virtualenv_python_executable, CondaEnvironmentKind ,
3131} ;
3232use crate :: { Interpreter , PythonVersion } ;
3333
@@ -185,6 +185,8 @@ pub enum PythonSource {
185185 ActiveEnvironment ,
186186 /// A conda environment was active e.g. via `CONDA_PREFIX`
187187 CondaPrefix ,
188+ /// A base conda environment was active e.g. via `CONDA_PREFIX`
189+ BaseCondaPrefix ,
188190 /// An environment was discovered e.g. via `.venv`
189191 DiscoveredEnvironment ,
190192 /// An executable was found in the search path i.e. `PATH`
@@ -233,27 +235,27 @@ pub enum Error {
233235 SourceNotAllowed ( PythonRequest , PythonSource , PythonPreference ) ,
234236}
235237
236- /// Lazily iterate over Python executables in mutable environments.
238+ /// Lazily iterate over Python executables in mutable virtual environments.
237239///
238240/// The following sources are supported:
239241///
240242/// - Active virtual environment (via `VIRTUAL_ENV`)
241- /// - Active conda environment (via `CONDA_PREFIX`)
242243/// - Discovered virtual environment (e.g. `.venv` in a parent directory)
243244///
244245/// Notably, "system" environments are excluded. See [`python_executables_from_installed`].
245- fn python_executables_from_environments < ' a > (
246+ fn python_executables_from_virtual_environments < ' a > (
246247) -> impl Iterator < Item = Result < ( PythonSource , PathBuf ) , Error > > + ' a {
247- let from_virtual_environment = std:: iter:: once_with ( || {
248+ let from_active_environment = std:: iter:: once_with ( || {
248249 virtualenv_from_env ( )
249250 . into_iter ( )
250251 . map ( virtualenv_python_executable)
251252 . map ( |path| Ok ( ( PythonSource :: ActiveEnvironment , path) ) )
252253 } )
253254 . flatten ( ) ;
254255
256+ // N.B. we prefer the conda environment over discovered virtual environments
255257 let from_conda_environment = std:: iter:: once_with ( || {
256- conda_prefix_from_env ( )
258+ conda_environment_from_env ( CondaEnvironmentKind :: Child )
257259 . into_iter ( )
258260 . map ( virtualenv_python_executable)
259261 . map ( |path| Ok ( ( PythonSource :: CondaPrefix , path) ) )
@@ -271,7 +273,7 @@ fn python_executables_from_environments<'a>(
271273 } )
272274 . flatten_ok ( ) ;
273275
274- from_virtual_environment
276+ from_active_environment
275277 . chain ( from_conda_environment)
276278 . chain ( from_discovered_environment)
277279}
@@ -406,23 +408,35 @@ fn python_executables<'a>(
406408 } )
407409 . flatten ( ) ;
408410
409- let from_environments = python_executables_from_environments ( ) ;
411+ // Check if the the base conda environment is active
412+ let from_base_conda_environment = std:: iter:: once_with ( || {
413+ conda_environment_from_env ( CondaEnvironmentKind :: Base )
414+ . into_iter ( )
415+ . map ( virtualenv_python_executable)
416+ . map ( |path| Ok ( ( PythonSource :: BaseCondaPrefix , path) ) )
417+ } )
418+ . flatten ( ) ;
419+
420+ let from_virtual_environments = python_executables_from_virtual_environments ( ) ;
410421 let from_installed = python_executables_from_installed ( version, implementation, preference) ;
411422
412423 // Limit the search to the relevant environment preference; we later validate that they match
413424 // the preference but queries are expensive and we query less interpreters this way.
414425 match environments {
415426 EnvironmentPreference :: OnlyVirtual => {
416- Box :: new ( from_parent_interpreter. chain ( from_environments ) )
427+ Box :: new ( from_parent_interpreter. chain ( from_virtual_environments ) )
417428 }
418429 EnvironmentPreference :: ExplicitSystem | EnvironmentPreference :: Any => Box :: new (
419430 from_parent_interpreter
420- . chain ( from_environments)
431+ . chain ( from_virtual_environments)
432+ . chain ( from_base_conda_environment)
433+ . chain ( from_installed) ,
434+ ) ,
435+ EnvironmentPreference :: OnlySystem => Box :: new (
436+ from_parent_interpreter
437+ . chain ( from_base_conda_environment)
421438 . chain ( from_installed) ,
422439 ) ,
423- EnvironmentPreference :: OnlySystem => {
424- Box :: new ( from_parent_interpreter. chain ( from_installed) )
425- }
426440 }
427441}
428442
@@ -617,8 +631,8 @@ fn satisfies_environment_preference(
617631) -> bool {
618632 match (
619633 preference,
620- // Conda environments are not conformant virtual environments but we treat them as such
621- interpreter. is_virtualenv ( ) || matches ! ( source, PythonSource :: CondaPrefix ) ,
634+ // Conda environments are not conformant virtual environments but we treat them as such.
635+ interpreter. is_virtualenv ( ) || ( matches ! ( source, PythonSource :: CondaPrefix ) ) ,
622636 ) {
623637 ( EnvironmentPreference :: Any , _) => true ,
624638 ( EnvironmentPreference :: OnlyVirtual , true ) => true ,
@@ -1515,6 +1529,7 @@ impl PythonSource {
15151529 Self :: Managed | Self :: Registry | Self :: MicrosoftStore => false ,
15161530 Self :: SearchPath
15171531 | Self :: CondaPrefix
1532+ | Self :: BaseCondaPrefix
15181533 | Self :: ProvidedPath
15191534 | Self :: ParentInterpreter
15201535 | Self :: ActiveEnvironment
@@ -1527,6 +1542,7 @@ impl PythonSource {
15271542 match self {
15281543 Self :: Managed | Self :: Registry | Self :: SearchPath | Self :: MicrosoftStore => false ,
15291544 Self :: CondaPrefix
1545+ | Self :: BaseCondaPrefix
15301546 | Self :: ProvidedPath
15311547 | Self :: ParentInterpreter
15321548 | Self :: ActiveEnvironment
@@ -1846,6 +1862,7 @@ impl VersionRequest {
18461862 Self :: Default => match source {
18471863 PythonSource :: ParentInterpreter
18481864 | PythonSource :: CondaPrefix
1865+ | PythonSource :: BaseCondaPrefix
18491866 | PythonSource :: ProvidedPath
18501867 | PythonSource :: DiscoveredEnvironment
18511868 | PythonSource :: ActiveEnvironment => Self :: Any ,
@@ -2256,7 +2273,7 @@ impl fmt::Display for PythonSource {
22562273 match self {
22572274 Self :: ProvidedPath => f. write_str ( "provided path" ) ,
22582275 Self :: ActiveEnvironment => f. write_str ( "active virtual environment" ) ,
2259- Self :: CondaPrefix => f. write_str ( "conda prefix" ) ,
2276+ Self :: CondaPrefix | Self :: BaseCondaPrefix => f. write_str ( "conda prefix" ) ,
22602277 Self :: DiscoveredEnvironment => f. write_str ( "virtual environment" ) ,
22612278 Self :: SearchPath => f. write_str ( "search path" ) ,
22622279 Self :: Registry => f. write_str ( "registry" ) ,
0 commit comments