@@ -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
@@ -179,6 +179,8 @@ pub enum PythonSource {
179179 ActiveEnvironment ,
180180 /// A conda environment was active e.g. via `CONDA_PREFIX`
181181 CondaPrefix ,
182+ /// A base conda environment was active e.g. via `CONDA_PREFIX`
183+ BaseCondaPrefix ,
182184 /// An environment was discovered e.g. via `.venv`
183185 DiscoveredEnvironment ,
184186 /// An executable was found in the search path i.e. `PATH`
@@ -227,27 +229,27 @@ pub enum Error {
227229 SourceNotAllowed ( PythonRequest , PythonSource , PythonPreference ) ,
228230}
229231
230- /// Lazily iterate over Python executables in mutable environments.
232+ /// Lazily iterate over Python executables in mutable virtual environments.
231233///
232234/// The following sources are supported:
233235///
234236/// - Active virtual environment (via `VIRTUAL_ENV`)
235- /// - Active conda environment (via `CONDA_PREFIX`)
236237/// - Discovered virtual environment (e.g. `.venv` in a parent directory)
237238///
238239/// Notably, "system" environments are excluded. See [`python_executables_from_installed`].
239- fn python_executables_from_environments < ' a > (
240+ fn python_executables_from_virtual_environments < ' a > (
240241) -> impl Iterator < Item = Result < ( PythonSource , PathBuf ) , Error > > + ' a {
241- let from_virtual_environment = std:: iter:: once_with ( || {
242+ let from_active_environment = std:: iter:: once_with ( || {
242243 virtualenv_from_env ( )
243244 . into_iter ( )
244245 . map ( virtualenv_python_executable)
245246 . map ( |path| Ok ( ( PythonSource :: ActiveEnvironment , path) ) )
246247 } )
247248 . flatten ( ) ;
248249
250+ // N.B. we prefer the conda environment over discovered virtual environments
249251 let from_conda_environment = std:: iter:: once_with ( || {
250- conda_prefix_from_env ( )
252+ conda_environment_from_env ( CondaEnvironmentKind :: Child )
251253 . into_iter ( )
252254 . map ( virtualenv_python_executable)
253255 . map ( |path| Ok ( ( PythonSource :: CondaPrefix , path) ) )
@@ -265,7 +267,7 @@ fn python_executables_from_environments<'a>(
265267 } )
266268 . flatten_ok ( ) ;
267269
268- from_virtual_environment
270+ from_active_environment
269271 . chain ( from_conda_environment)
270272 . chain ( from_discovered_environment)
271273}
@@ -400,23 +402,35 @@ fn python_executables<'a>(
400402 } )
401403 . flatten ( ) ;
402404
403- let from_environments = python_executables_from_environments ( ) ;
405+ // Check if the the base conda environment is active
406+ let from_base_conda_environment = std:: iter:: once_with ( || {
407+ conda_environment_from_env ( CondaEnvironmentKind :: Base )
408+ . into_iter ( )
409+ . map ( virtualenv_python_executable)
410+ . map ( |path| Ok ( ( PythonSource :: BaseCondaPrefix , path) ) )
411+ } )
412+ . flatten ( ) ;
413+
414+ let from_virtual_environments = python_executables_from_virtual_environments ( ) ;
404415 let from_installed = python_executables_from_installed ( version, implementation, preference) ;
405416
406417 // Limit the search to the relevant environment preference; we later validate that they match
407418 // the preference but queries are expensive and we query less interpreters this way.
408419 match environments {
409420 EnvironmentPreference :: OnlyVirtual => {
410- Box :: new ( from_parent_interpreter. chain ( from_environments ) )
421+ Box :: new ( from_parent_interpreter. chain ( from_virtual_environments ) )
411422 }
412423 EnvironmentPreference :: ExplicitSystem | EnvironmentPreference :: Any => Box :: new (
413424 from_parent_interpreter
414- . chain ( from_environments)
425+ . chain ( from_virtual_environments)
426+ . chain ( from_base_conda_environment)
427+ . chain ( from_installed) ,
428+ ) ,
429+ EnvironmentPreference :: OnlySystem => Box :: new (
430+ from_parent_interpreter
431+ . chain ( from_base_conda_environment)
415432 . chain ( from_installed) ,
416433 ) ,
417- EnvironmentPreference :: OnlySystem => {
418- Box :: new ( from_parent_interpreter. chain ( from_installed) )
419- }
420434 }
421435}
422436
@@ -611,8 +625,8 @@ fn satisfies_environment_preference(
611625) -> bool {
612626 match (
613627 preference,
614- // Conda environments are not conformant virtual environments but we treat them as such
615- interpreter. is_virtualenv ( ) || matches ! ( source, PythonSource :: CondaPrefix ) ,
628+ // Conda environments are not conformant virtual environments but we treat them as such.
629+ interpreter. is_virtualenv ( ) || ( matches ! ( source, PythonSource :: CondaPrefix ) ) ,
616630 ) {
617631 ( EnvironmentPreference :: Any , _) => true ,
618632 ( EnvironmentPreference :: OnlyVirtual , true ) => true ,
@@ -1493,6 +1507,7 @@ impl PythonSource {
14931507 Self :: Managed | Self :: Registry | Self :: MicrosoftStore => false ,
14941508 Self :: SearchPath
14951509 | Self :: CondaPrefix
1510+ | Self :: BaseCondaPrefix
14961511 | Self :: ProvidedPath
14971512 | Self :: ParentInterpreter
14981513 | Self :: ActiveEnvironment
@@ -1505,6 +1520,7 @@ impl PythonSource {
15051520 match self {
15061521 Self :: Managed | Self :: Registry | Self :: SearchPath | Self :: MicrosoftStore => false ,
15071522 Self :: CondaPrefix
1523+ | Self :: BaseCondaPrefix
15081524 | Self :: ProvidedPath
15091525 | Self :: ParentInterpreter
15101526 | Self :: ActiveEnvironment
@@ -1826,6 +1842,7 @@ impl VersionRequest {
18261842 Self :: Default => match source {
18271843 PythonSource :: ParentInterpreter
18281844 | PythonSource :: CondaPrefix
1845+ | PythonSource :: BaseCondaPrefix
18291846 | PythonSource :: ProvidedPath
18301847 | PythonSource :: DiscoveredEnvironment
18311848 | PythonSource :: ActiveEnvironment => Self :: Any ,
@@ -2236,7 +2253,7 @@ impl fmt::Display for PythonSource {
22362253 match self {
22372254 Self :: ProvidedPath => f. write_str ( "provided path" ) ,
22382255 Self :: ActiveEnvironment => f. write_str ( "active virtual environment" ) ,
2239- Self :: CondaPrefix => f. write_str ( "conda prefix" ) ,
2256+ Self :: CondaPrefix | Self :: BaseCondaPrefix => f. write_str ( "conda prefix" ) ,
22402257 Self :: DiscoveredEnvironment => f. write_str ( "virtual environment" ) ,
22412258 Self :: SearchPath => f. write_str ( "search path" ) ,
22422259 Self :: Registry => f. write_str ( "registry" ) ,
0 commit comments