Esempio n. 1
0
File: core.py Progetto: pcskys/pdm
 def environment(self) -> Environment:
     if self.is_global:
         return GlobalEnvironment(self)
     if self.config["use_venv"] and "VIRTUAL_ENV" in os.environ:
         self.project_config["python.path"] = get_venv_python()
         return GlobalEnvironment(self)
     return Environment(self)
Esempio n. 2
0
    def python_executable(self) -> str:
        """Get the Python interpreter path."""
        config = self.config
        if self.project_config.get(
                "python.path") and not os.getenv("PDM_IGNORE_SAVED_PYTHON"):
            return self.project_config["python.path"]
        path = None
        if config["use_venv"]:
            path = get_venv_python(self.root)
            if path:
                stream.echo(
                    f"Virtualenv interpreter {stream.green(path)} is detected.",
                    err=True,
                    verbosity=stream.DETAIL,
                )
        if not path and PYENV_INSTALLED and config.get("python.use_pyenv",
                                                       True):
            path = Path(PYENV_ROOT, "shims", "python").as_posix()
        if not path:
            path = shutil.which("python")

        version = None
        if path:
            try:
                version, _ = get_python_version(path, True)
            except (FileNotFoundError, subprocess.CalledProcessError):
                version = None
        if not version or not self.python_requires.contains(version):
            finder = Finder()
            for python in finder.find_all_python_versions():
                version, _ = get_python_version(python.path.as_posix(), True)
                if self.python_requires.contains(version):
                    path = python.path.as_posix()
                    break
            else:
                version = ".".join(map(str, sys.version_info[:3]))
                if self.python_requires.contains(version):
                    path = sys.executable
        if path:
            if os.path.normcase(path) == os.path.normcase(sys.executable):
                # Refer to the base interpreter to allow for venvs
                path = getattr(sys, "_base_executable", sys.executable)
            stream.echo(
                "Using Python interpreter: {} ({})".format(
                    stream.green(path), version),
                err=True,
            )
            if not os.getenv("PDM_IGNORE_SAVED_PYTHON"):
                self.project_config["python.path"] = Path(path).as_posix()
            return path
        raise NoPythonVersion(
            "No Python that satisfies {} is found on the system.".format(
                self.python_requires))
Esempio n. 3
0
 def environment(self) -> Environment:
     if self.is_global:
         env = GlobalEnvironment(self)
         # Rewrite global project's python requires to be
         # compatible with the exact version
         env.python_requires = PySpecSet(
             "==" + get_python_version(env.python_executable, True))
         return env
     if self.config["use_venv"]:
         venv_python = get_venv_python(self.root)
         if venv_python:
             self.project_config["python.path"] = venv_python
             return GlobalEnvironment(self)
     return Environment(self)
Esempio n. 4
0
 def environment(self) -> Environment:
     if self.is_global:
         env = GlobalEnvironment(self)
         # Rewrite global project's python requires to be
         # compatible with the exact version
         env.python_requires = PySpecSet(
             "==" + get_python_version(env.python_executable, True)[0])
         return env
     if not self.project_config.get(
             "python.path") and self.config["use_venv"]:
         venv_python = get_venv_python(self.root)
         if venv_python:
             self.project_config["python.path"] = venv_python
     if (self.config["use_venv"] and self.project_config.get("python.path")
             and Path(self.project_config.get("python.path")
                      ).parent.parent.joinpath("pyvenv.cfg").exists()):
         # Only recognize venv created by python -m venv and virtualenv>20
         return GlobalEnvironment(self)
     return Environment(self)
Esempio n. 5
0
    def python_executable(self) -> str:
        """Get the Python interpreter path."""
        config = self.project.config
        if config.get("python.path"):
            return config["python.path"]
        if PYENV_INSTALLED and config.get("python.use_pyenv", True):
            return os.path.join(PYENV_ROOT, "shims", "python")
        if "VIRTUAL_ENV" in os.environ:
            stream.echo(
                "An activated virtualenv is detected, reuse the interpreter now.",
                err=True,
                verbosity=stream.DETAIL,
            )
            return get_venv_python(self.project.root)

        # First try what `python` refers to.
        path = shutil.which("python")
        version = None
        if path:
            version, _ = get_python_version(path, True)
        if not version or not self.python_requires.contains(version):
            finder = Finder()
            for python in finder.find_all_python_versions():
                version, _ = get_python_version(python.path.as_posix(), True)
                if self.python_requires.contains(version):
                    path = python.path.as_posix()
                    break
            else:
                version = ".".join(map(str, sys.version_info[:3]))
                if self.python_requires.contains(version):
                    path = sys.executable
        if path:
            stream.echo(
                "Using Python interpreter: {} ({})".format(stream.green(path), version)
            )
            self.project.project_config["python.path"] = Path(path).as_posix()
            return path
        raise NoPythonVersion(
            "No Python that satisfies {} is found on the system.".format(
                self.python_requires
            )
        )