diff --git a/CHANGELOG.md b/CHANGELOG.md index 3aa974e7fd88..8024554e7442 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed +- Fix `XFunc` and `RPC` environment activation. - Fix exception on Rhino Mac. - Fix missing import on `compas_rhino.geometry`. diff --git a/src/compas/_os.py b/src/compas/_os.py index bfae7528f7cd..04094c70aa36 100644 --- a/src/compas/_os.py +++ b/src/compas/_os.py @@ -6,6 +6,11 @@ import os import sys +try: + NotADirectoryError +except NameError: + class NotADirectoryError(Exception): + pass PY3 = sys.version_info[0] == 3 system = sys.platform @@ -14,6 +19,16 @@ if 'ironpython' in sys.version.lower() and os.name == 'nt': system = 'win32' +try: + from compas_bootstrapper import PYTHON_DIRECTORY +except: + # We re-map CONDA_PREFIX for backwards compatibility reasons + # In a few releases down the line, we can get rid of this bit + try: + from compas_bootstrapper import CONDA_PREFIX as PYTHON_DIRECTORY + except: + PYTHON_DIRECTORY = None + def select_python(python_executable): """Selects the most likely python interpreter to run. @@ -29,16 +44,6 @@ def select_python(python_executable): """ python_executable = python_executable or 'pythonw' - try: - from compas_bootstrapper import PYTHON_DIRECTORY - except: - # We re-map CONDA_PREFIX for backwards compatibility reasons - # In a few releases down the line, we can get rid of this bit - try: - from compas_bootstrapper import CONDA_PREFIX as PYTHON_DIRECTORY - except: - PYTHON_DIRECTORY = None - if PYTHON_DIRECTORY and os.path.exists(PYTHON_DIRECTORY): python = os.path.join(PYTHON_DIRECTORY, python_executable) if os.path.exists(python): @@ -63,6 +68,26 @@ def select_python(python_executable): return python_executable +def prepare_environment(): + """Prepares an environment context to run Python on. + + If Python is being used from a conda environment, this is roughly equivalent + to activating the conda environment by setting up the correct environment + variables. + """ + env = os.environ.copy() + + if PYTHON_DIRECTORY: + lib_bin = os.path.join(PYTHON_DIRECTORY, 'Library', 'bin') + if os.path.exists(lib_bin): + env['PATH'] += os.pathsep + lib_bin + + lib_bin = os.path.join(PYTHON_DIRECTORY, 'lib') + if os.path.exists(lib_bin): + env['PATH'] += os.pathsep + lib_bin + + return env + def absjoin(*parts): return os.path.abspath(os.path.join(*parts)) diff --git a/src/compas/rpc/proxy.py b/src/compas/rpc/proxy.py index daf92146c0f8..f0e257a6aba1 100644 --- a/src/compas/rpc/proxy.py +++ b/src/compas/rpc/proxy.py @@ -223,11 +223,13 @@ def start_server(self): """ python = self.python + env = compas._os.prepare_environment() try: Popen except NameError: self._process = Process() + self._process.StartInfo.EvironmentVariables = env self._process.StartInfo.UseShellExecute = False self._process.StartInfo.RedirectStandardOutput = True self._process.StartInfo.RedirectStandardError = True @@ -236,7 +238,7 @@ def start_server(self): self._process.Start() else: args = [python, '-m', self.service, str(self._port)] - self._process = Popen(args, stdout=PIPE, stderr=STDOUT) + self._process = Popen(args, stdout=PIPE, stderr=STDOUT, env=env) server = ServerProxy(self.address) diff --git a/src/compas/utilities/xfunc.py b/src/compas/utilities/xfunc.py index de0e72cfa2a7..e39506204b8d 100644 --- a/src/compas/utilities/xfunc.py +++ b/src/compas/utilities/xfunc.py @@ -376,7 +376,8 @@ def __call__(self, *args, **kwargs): self.opath, self.serializer] - process = Popen(process_args, stderr=PIPE, stdout=PIPE) + env = compas._os.prepare_environment() + process = Popen(process_args, stderr=PIPE, stdout=PIPE, env=env) while process.poll() is None: line = process.stdout.readline().strip()