Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.

Expand Down
45 changes: 35 additions & 10 deletions src/compas/_os.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand All @@ -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):
Expand All @@ -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))

Expand Down
4 changes: 3 additions & 1 deletion src/compas/rpc/proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)

Expand Down
3 changes: 2 additions & 1 deletion src/compas/utilities/xfunc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down