Skip to content

Commit c7b0f80

Browse files
committed
Fix xfunc and rpc environment activation when in conda
1 parent 4069392 commit c7b0f80

File tree

4 files changed

+31
-12
lines changed

4 files changed

+31
-12
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111

1212
### Changed
1313

14+
- Fix `XFunc` and `RPC` environment activation.
1415
- Fix exception on Rhino Mac.
1516
- Fix missing import on `compas_rhino.geometry`.
1617

src/compas/_os.py

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,16 @@
1414
if 'ironpython' in sys.version.lower() and os.name == 'nt':
1515
system = 'win32'
1616

17+
try:
18+
from compas_bootstrapper import PYTHON_DIRECTORY
19+
except:
20+
# We re-map CONDA_PREFIX for backwards compatibility reasons
21+
# In a few releases down the line, we can get rid of this bit
22+
try:
23+
from compas_bootstrapper import CONDA_PREFIX as PYTHON_DIRECTORY
24+
except:
25+
PYTHON_DIRECTORY = None
26+
1727

1828
def select_python(python_executable):
1929
"""Selects the most likely python interpreter to run.
@@ -29,16 +39,6 @@ def select_python(python_executable):
2939
"""
3040
python_executable = python_executable or 'pythonw'
3141

32-
try:
33-
from compas_bootstrapper import PYTHON_DIRECTORY
34-
except:
35-
# We re-map CONDA_PREFIX for backwards compatibility reasons
36-
# In a few releases down the line, we can get rid of this bit
37-
try:
38-
from compas_bootstrapper import CONDA_PREFIX as PYTHON_DIRECTORY
39-
except:
40-
PYTHON_DIRECTORY = None
41-
4242
if PYTHON_DIRECTORY and os.path.exists(PYTHON_DIRECTORY):
4343
python = os.path.join(PYTHON_DIRECTORY, python_executable)
4444
if os.path.exists(python):
@@ -63,6 +63,22 @@ def select_python(python_executable):
6363
return python_executable
6464

6565

66+
def prepare_environment():
67+
"""Prepares an environment context to run Python on.
68+
69+
If Python is being used from a conda environment, this is roughly equivalent
70+
to activating the conda environment by setting up the correct environment
71+
variables.
72+
"""
73+
env = os.environ.copy()
74+
75+
if PYTHON_DIRECTORY:
76+
lib_bin = os.path.join(PYTHON_DIRECTORY, 'Library', 'bin')
77+
if os.path.exists(lib_bin):
78+
env['PATH'] += ';' + lib_bin
79+
80+
return env
81+
6682
def absjoin(*parts):
6783
return os.path.abspath(os.path.join(*parts))
6884

src/compas/rpc/proxy.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,8 @@ def start_server(self):
236236
self._process.Start()
237237
else:
238238
args = [python, '-m', self.service, str(self._port)]
239-
self._process = Popen(args, stdout=PIPE, stderr=STDOUT)
239+
env = compas._os.prepare_environment()
240+
self._process = Popen(args, stdout=PIPE, stderr=STDOUT, env=env)
240241

241242
server = ServerProxy(self.address)
242243

src/compas/utilities/xfunc.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,8 @@ def __call__(self, *args, **kwargs):
376376
self.opath,
377377
self.serializer]
378378

379-
process = Popen(process_args, stderr=PIPE, stdout=PIPE)
379+
env = compas._os.prepare_environment()
380+
process = Popen(process_args, stderr=PIPE, stdout=PIPE, env=env)
380381

381382
while process.poll() is None:
382383
line = process.stdout.readline().strip()

0 commit comments

Comments
 (0)