Skip to content

Commit

Permalink
Integrate with pyenv
Browse files Browse the repository at this point in the history
  • Loading branch information
frostming committed Feb 14, 2020
1 parent 96374c5 commit 01005ed
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 13 deletions.
1 change: 1 addition & 0 deletions news/36.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Use the pyenv interperter value if pyenv is installed.
2 changes: 1 addition & 1 deletion pdm/cli/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ def do_use(project: Project, python: str) -> None:
)
)

project.config["python"] = Path(python_path).as_posix()
project.config["python.path"] = Path(python_path).as_posix()
project.config.save_config()


Expand Down
5 changes: 1 addition & 4 deletions pdm/context.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

import hashlib
import os
from pathlib import Path
from typing import TYPE_CHECKING

Expand All @@ -27,10 +28,6 @@ def __init__(self):
def init(self, project):
self.project = project

@property
def initialized(self) -> bool:
return self._initialized

@property
def cache_dir(self) -> Path:
return Path(self.project.config.get("cache_dir"))
Expand Down
9 changes: 6 additions & 3 deletions pdm/models/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
get_python_version,
)
from pythonfinder import Finder
from pythonfinder.environment import PYENV_INSTALLED, PYENV_ROOT
from vistir.contextmanagers import temp_environ
from vistir.path import normalize_path

Expand Down Expand Up @@ -84,13 +85,15 @@ def __init__(self, python_requires: PySpecSet, config: Config) -> None:
@cached_property
def python_executable(self) -> str:
"""Get the Python interpreter path."""
if self.config["python"]:
path = self.config["python"]
if self.config.get("python.path"):
path = self.config["python.path"]
try:
get_python_version(path)
return path
except Exception:
pass
if PYENV_INSTALLED and self.config.get("python.use_pyenv", True):
return os.path.join(PYENV_ROOT, "shims", "python")

# First try what `python` refers to.
path = shutil.which("python")
Expand All @@ -113,7 +116,7 @@ def python_executable(self) -> str:
context.io.green(path), version
)
)
self.config["python"] = Path(path).as_posix()
self.config["python.path"] = Path(path).as_posix()
self.config.save_config()
return path
raise NoPythonVersion(
Expand Down
6 changes: 1 addition & 5 deletions pdm/project/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
class Config(MutableMapping):
DEFAULT_CONFIG = {
"cache_dir": appdirs.user_cache_dir("pdm"),
"python": None,
"packages_path": None,
}

def __init__(self, project_root: Path):
Expand All @@ -20,7 +18,7 @@ def __init__(self, project_root: Path):
self._dirty = {}

self._project_config_file = self.project_root / ".pdm.toml"
self._global_config_file = Path(appdirs.user_config_dir("pdm")) / ".pdm.toml"
self._global_config_file = Path(appdirs.user_config_dir("pdm")) / "config.toml"
self._project_config = self.load_config(self._project_config_file)
self._global_config = self.load_config(self._global_config_file)
# First load user config, then project config
Expand Down Expand Up @@ -67,8 +65,6 @@ def __getitem__(self, key: str) -> Any:
raise NoConfigError(key) from None

def __setitem__(self, key: str, value: Any) -> None:
if key not in self.DEFAULT_CONFIG:
raise NoConfigError(key)
self._dirty[key] = value
self._data[key] = value

Expand Down
13 changes: 13 additions & 0 deletions tests/test_project.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import os


def test_project_python_with_pyenv_support(project, mocker):
from pythonfinder.environment import PYENV_ROOT

pyenv_python = os.path.join(PYENV_ROOT, "shims", "python")

mocker.patch("pdm.models.environment.PYENV_INSTALLED", True)
assert project.environment.python_executable == pyenv_python

project.config["python.use_pyenv"] = False
assert project.environment.python_executable != pyenv_python

0 comments on commit 01005ed

Please sign in to comment.