Skip to content

Commit 719c1c3

Browse files
minrkjasongroutblink1073
authored
Don't treat the conda root env as an env (#324)
Co-authored-by: Jason Grout <[email protected]> Co-authored-by: Steven Silvester <[email protected]>
1 parent f0ea095 commit 719c1c3

File tree

4 files changed

+27
-7
lines changed

4 files changed

+27
-7
lines changed

jupyter_core/application.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
if isinstance(Application.flags, dict):
4646
# traitlets 5
4747
base_flags.update(Application.flags)
48-
_jupyter_flags = {
48+
_jupyter_flags: dict = {
4949
"debug": (
5050
{"Application": {"log_level": logging.DEBUG}},
5151
"set log level to logging.DEBUG (maximize logging output)",

jupyter_core/paths.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,12 @@ def prefer_environment_over_user() -> bool:
7878
if sys.prefix != sys.base_prefix:
7979
return True
8080

81-
# If sys.prefix indicates Python comes from a conda/mamba environment, default to True
82-
if "CONDA_PREFIX" in os.environ and sys.prefix.startswith(os.environ["CONDA_PREFIX"]):
81+
# If sys.prefix indicates Python comes from a conda/mamba environment that is not the root environment, default to True
82+
if (
83+
"CONDA_PREFIX" in os.environ
84+
and sys.prefix.startswith(os.environ["CONDA_PREFIX"])
85+
and os.environ.get("CONDA_DEFAULT_ENV", "base") != "base"
86+
):
8387
return True
8488

8589
return False

jupyter_core/tests/test_paths.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -400,10 +400,23 @@ def test_prefer_environment_over_user():
400400

401401
# Test default if environment variable is not set, and try to determine if we are in a virtual environment
402402
os.environ.pop("JUPYTER_PREFER_ENV_PATH", None)
403-
in_venv = sys.prefix != sys.base_prefix or (
404-
"CONDA_PREFIX" in os.environ and sys.prefix.startswith(os.environ["CONDA_PREFIX"])
405-
)
406-
assert prefer_environment_over_user() is in_venv
403+
# base prefix differs, venv
404+
with patch.object(sys, "base_prefix", "notthesame"):
405+
assert prefer_environment_over_user()
406+
407+
# conda
408+
with patch.object(sys, "base_prefix", sys.prefix):
409+
# in base env, don't prefer it
410+
with patch.dict(os.environ, {"CONDA_PREFIX": sys.prefix, "CONDA_DEFAULT_ENV": "base"}):
411+
assert not prefer_environment_over_user()
412+
# in non-base env, prefer it
413+
with patch.dict(os.environ, {"CONDA_PREFIX": sys.prefix, "CONDA_DEFAULT_ENV": "/tmp"}):
414+
assert prefer_environment_over_user()
415+
# conda env defined, but we aren't using it
416+
with patch.dict(
417+
os.environ, {"CONDA_PREFIX": "/somewherelese", "CONDA_DEFAULT_ENV": "/tmp"}
418+
):
419+
assert not prefer_environment_over_user()
407420

408421

409422
def test_is_hidden():

pyproject.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,3 +207,6 @@ unfixable = [
207207
"tests/*" = ["B011", "F841", "C408", "E402", "T201", "B007", "N802"]
208208
# F821 Undefined name `get_config`
209209
"jupyter_core/tests/**/profile_default/*_config.py" = ["F821"]
210+
211+
[tool.check-wheel-contents]
212+
toplevel = ["jupyter_core/", "jupyter.py"]

0 commit comments

Comments
 (0)