66import os
77import sys
88
9+ try :
10+ NotADirectoryError
11+ except NameError :
12+ class NotADirectoryError (Exception ):
13+ pass
914
1015PY3 = sys .version_info [0 ] == 3
1116system = sys .platform
1419if 'ironpython' in sys .version .lower () and os .name == 'nt' :
1520 system = 'win32'
1621
22+ try :
23+ from compas_bootstrapper import PYTHON_DIRECTORY
24+ except :
25+ # We re-map CONDA_PREFIX for backwards compatibility reasons
26+ # In a few releases down the line, we can get rid of this bit
27+ try :
28+ from compas_bootstrapper import CONDA_PREFIX as PYTHON_DIRECTORY
29+ except :
30+ PYTHON_DIRECTORY = None
31+
1732
1833def select_python (python_executable ):
1934 """Selects the most likely python interpreter to run.
@@ -29,16 +44,6 @@ def select_python(python_executable):
2944 """
3045 python_executable = python_executable or 'pythonw'
3146
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-
4247 if PYTHON_DIRECTORY and os .path .exists (PYTHON_DIRECTORY ):
4348 python = os .path .join (PYTHON_DIRECTORY , python_executable )
4449 if os .path .exists (python ):
@@ -63,6 +68,26 @@ def select_python(python_executable):
6368 return python_executable
6469
6570
71+ def prepare_environment ():
72+ """Prepares an environment context to run Python on.
73+
74+ If Python is being used from a conda environment, this is roughly equivalent
75+ to activating the conda environment by setting up the correct environment
76+ variables.
77+ """
78+ env = os .environ .copy ()
79+
80+ if PYTHON_DIRECTORY :
81+ lib_bin = os .path .join (PYTHON_DIRECTORY , 'Library' , 'bin' )
82+ if os .path .exists (lib_bin ):
83+ env ['PATH' ] += os .pathsep + lib_bin
84+
85+ lib_bin = os .path .join (PYTHON_DIRECTORY , 'lib' )
86+ if os .path .exists (lib_bin ):
87+ env ['PATH' ] += os .pathsep + lib_bin
88+
89+ return env
90+
6691def absjoin (* parts ):
6792 return os .path .abspath (os .path .join (* parts ))
6893
0 commit comments