8
8
using Compat
9
9
import Conda
10
10
11
- PYTHONIOENCODING = get (ENV , " PYTHONIOENCODING" , nothing )
12
- PYTHONHOME = get (ENV , " PYTHONHOME" , nothing )
13
11
immutable UseCondaPython <: Exception end
14
12
15
- try # save/restore environment vars
16
-
17
- # set PYTHONIOENCODING when running python executable, so that
18
- # we get UTF-8 encoded text as output (this is not the default on Windows).
19
- ENV [" PYTHONIOENCODING" ] = " UTF-8"
13
+ try # make sure deps.jl file is removed on error
20
14
21
15
# ########################################################################
22
16
23
- pyconfigvar (python:: AbstractString , var:: AbstractString ) = chomp (readstring (` $python -c "import distutils.sysconfig; print(distutils.sysconfig.get_config_var('$var '))"` ))
17
+ # Fix the environment for running `python`, and setts IO encoding to UTF-8.
18
+ # If cmd is the Conda python, then additionally removes all PYTHON* and
19
+ # CONDA* environment variables.
20
+ function pythonenv (cmd:: Cmd )
21
+ env = copy (ENV )
22
+ if dirname (cmd. exec[1 ]) == abspath (Conda. PYTHONDIR)
23
+ pythonvars = Compat. UTF8String[]
24
+ for var in keys (env)
25
+ if startswith (var, " CONDA" ) || startswith (var, " PYTHON" )
26
+ push! (pythonvars, var)
27
+ end
28
+ end
29
+ for var in pythonvars
30
+ pop! (env, var)
31
+ end
32
+ end
33
+ # set PYTHONIOENCODING when running python executable, so that
34
+ # we get UTF-8 encoded text as output (this is not the default on Windows).
35
+ env[" PYTHONIOENCODING" ] = " UTF-8"
36
+ setenv (cmd, env)
37
+ end
38
+
39
+ pyconfigvar (python:: AbstractString , var:: AbstractString ) = chomp (readstring (pythonenv (` $python -c "import distutils.sysconfig; print(distutils.sysconfig.get_config_var('$var '))"` )))
24
40
pyconfigvar (python, var, default) = let v = pyconfigvar (python, var)
25
41
v == " None" ? default : v
26
42
end
27
43
28
- pysys (python:: AbstractString , var:: AbstractString ) = chomp (readstring (` $python -c "import sys; print(sys.$var )"` ))
44
+ pysys (python:: AbstractString , var:: AbstractString ) = chomp (readstring (pythonenv ( ` $python -c "import sys; print(sys.$var )"` ) ))
29
45
30
46
# ########################################################################
31
47
@@ -58,24 +74,6 @@ function find_libpython(python::AbstractString)
58
74
59
75
error_strings = Compat. String[]
60
76
61
- if ! haskey (ENV , " PYTHONHOME" )
62
- # PYTHONHOME tells python where to look for both pure python
63
- # and binary modules. When it is set, it replaces both
64
- # `prefix` and `exec_prefix` and we thus need to set it to
65
- # both in case they differ. This is also what the
66
- # documentation recommends. However, they are documented
67
- # to always be the same on Windows, where it causes
68
- # problems if we try to include both.
69
- ENV [" PYTHONHOME" ] = is_windows () ? exec_prefix : pysys (python, " prefix" ) * " :" * exec_prefix
70
- # Unfortunately, setting PYTHONHOME screws up Canopy's Python distro?
71
- try
72
- run (pipeline (` $python -c "import site"` , stdout = DevNull, stderr = DevNull))
73
- catch e
74
- push! (error_strings, string (" $python -c \" import site\" ==> " , e))
75
- pop! (ENV , " PYTHONHOME" )
76
- end
77
- end
78
-
79
77
# TODO : other paths? python-config output? pyconfigvar("LDFLAGS")?
80
78
81
79
# find libpython (we hope):
@@ -133,13 +131,13 @@ function find_libpython(python::AbstractString)
133
131
134
132
The python executable we tried was $python (= version $v );
135
133
the library names we tried were $libs
136
- and the library paths we tried were $libpaths
137
- """ )
134
+ and the library paths we tried were $libpaths """ )
138
135
end
139
136
140
137
# ########################################################################
141
138
142
139
include (" depsutils.jl" )
140
+
143
141
# ########################################################################
144
142
145
143
const python = try
179
177
const (libpython, libpy_name) = find_libpython (python)
180
178
const programname = pysys (python, " executable" )
181
179
180
+ # Get PYTHONHOME, either from the environment or from Python
181
+ # itself (if it is not in the environment or if we are using Conda)
182
+ PYTHONHOME = if ! haskey (ENV , " PYTHONHOME" ) || use_conda
183
+ # PYTHONHOME tells python where to look for both pure python
184
+ # and binary modules. When it is set, it replaces both
185
+ # `prefix` and `exec_prefix` and we thus need to set it to
186
+ # both in case they differ. This is also what the
187
+ # documentation recommends. However, they are documented
188
+ # to always be the same on Windows, where it causes
189
+ # problems if we try to include both.
190
+ exec_prefix = pysys (python, " exec_prefix" )
191
+ is_windows () ? exec_prefix : pysys (python, " prefix" ) * " :" * exec_prefix
192
+ else
193
+ ENV [" PYTHONHOME" ]
194
+ end
195
+
182
196
# cache the Python version as a Julia VersionNumber
183
- const pyversion = convert (VersionNumber, split (Py_GetVersion (libpython))[1 ])
197
+ const pyversion = withenv (" PYTHONHOME" => PYTHONHOME) do
198
+ convert (VersionNumber, split (Py_GetVersion (libpython))[1 ])
199
+ end
184
200
185
201
info (" PyCall is using $python (Python $pyversion ) at $programname , libpython = $libpy_name " )
186
202
@@ -196,8 +212,6 @@ wstringconst(s) =
196
212
string (" wstring(\" " , escape_string (s), " \" )" ) :
197
213
string (" Base.cconvert(Cwstring, \" " , escape_string (s), " \" )" )
198
214
199
- PYTHONHOMEENV = get (ENV , " PYTHONHOME" , " " )
200
-
201
215
# we write configuration files only if they change, both
202
216
# to prevent unnecessary recompilation and to minimize
203
217
# problems in the unlikely event of read-only directories.
@@ -216,8 +230,8 @@ writeifchanged("deps.jl", """
216
230
const pyprogramname = "$(escape_string (programname)) "
217
231
const wpyprogramname = $(wstringconst (programname))
218
232
const pyversion_build = $(repr (pyversion))
219
- const PYTHONHOME = "$(escape_string (PYTHONHOMEENV )) "
220
- const wPYTHONHOME = $(wstringconst (PYTHONHOMEENV ))
233
+ const PYTHONHOME = "$(escape_string (PYTHONHOME )) "
234
+ const wPYTHONHOME = $(wstringconst (PYTHONHOME ))
221
235
222
236
"True if we are using the Python distribution in the Conda package."
223
237
const conda = $use_conda
@@ -233,10 +247,6 @@ catch
233
247
# remove deps.jl (if it exists) on an error, so that PyCall will
234
248
# not load until it is properly configured.
235
249
isfile (" deps.jl" ) && rm (" deps.jl" )
236
-
237
- finally # restore env vars
238
-
239
- PYTHONIOENCODING != nothing ? (ENV [" PYTHONIOENCODING" ] = PYTHONIOENCODING) : pop! (ENV , " PYTHONIOENCODING" )
240
- PYTHONHOME != nothing ? (ENV [" PYTHONHOME" ] = PYTHONHOME) : pop! (ENV , " PYTHONHOME" , " " )
250
+ rethrow ()
241
251
242
252
end
0 commit comments