Skip to content

Commit e1b1670

Browse files
author
Christopher Doris
committed
path fix on windows
1 parent f2c1fd1 commit e1b1670

File tree

1 file changed

+60
-56
lines changed

1 file changed

+60
-56
lines changed

pysrc/juliacall/__init__.py

Lines changed: 60 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -151,63 +151,67 @@ def args_from_config():
151151
assert os.path.exists(default_bindir)
152152
CONFIG['libpath'] = libpath
153153

154-
# Initialise Julia
155-
d = os.getcwd()
154+
# Add the Julia library directory to the PATH on Windows so Julia's system libraries can
155+
# be found. They are normally found because they are in the same directory as julia.exe,
156+
# but python.exe is somewhere else!
157+
if os.name == 'nt':
158+
libdir = os.path.dirname(libpath)
159+
if 'PATH' in os.environ:
160+
os.environ['PATH'] = libdir + ';' + os.environ['PATH']
161+
else:
162+
os.environ['PATH'] = libdir
163+
164+
# Open the library
165+
CONFIG['lib'] = lib = c.CDLL(libpath, mode=c.RTLD_GLOBAL)
166+
167+
# parse options
168+
argc, argv = args_from_config()
169+
jl_parse_opts = lib.jl_parse_opts
170+
jl_parse_opts.argtypes = [c.c_void_p, c.c_void_p]
171+
jl_parse_opts.restype = None
172+
jl_parse_opts(c.pointer(argc), c.pointer(argv))
173+
assert argc.value == 0
174+
175+
# initialise julia
156176
try:
157-
# Open the library
158-
os.chdir(os.path.dirname(libpath))
159-
CONFIG['lib'] = lib = c.CDLL(libpath, mode=c.RTLD_GLOBAL)
160-
161-
# parse options
162-
argc, argv = args_from_config()
163-
jl_parse_opts = lib.jl_parse_opts
164-
jl_parse_opts.argtypes = [c.c_void_p, c.c_void_p]
165-
jl_parse_opts.restype = None
166-
jl_parse_opts(c.pointer(argc), c.pointer(argv))
167-
assert argc.value == 0
168-
169-
# initialise julia
170-
try:
171-
jl_init = lib.jl_init_with_image__threading
172-
except AttributeError:
173-
jl_init = lib.jl_init_with_image
174-
jl_init.argtypes = [c.c_char_p, c.c_char_p]
175-
jl_init.restype = None
176-
jl_init(
177-
(default_bindir if bindir is None else bindir).encode('utf8'),
178-
None if sysimg is None else sysimg.encode('utf8'),
179-
)
180-
181-
# initialise PythonCall
182-
jl_eval = lib.jl_eval_string
183-
jl_eval.argtypes = [c.c_char_p]
184-
jl_eval.restype = c.c_void_p
185-
def jlstr(x):
186-
return 'raw"' + x.replace('"', '\\"').replace('\\', '\\\\') + '"'
187-
script = '''
188-
try
189-
Base.require(Main, :CompilerSupportLibraries_jll)
190-
import Pkg
191-
ENV["JULIA_PYTHONCALL_LIBPTR"] = {}
192-
ENV["JULIA_PYTHONCALL_EXE"] = {}
193-
Pkg.activate({}, io=devnull)
194-
import PythonCall
195-
catch err
196-
print(stderr, "ERROR: ")
197-
showerror(stderr, err, catch_backtrace())
198-
flush(stderr)
199-
rethrow()
200-
end
201-
'''.format(
202-
jlstr(str(c.pythonapi._handle)),
203-
jlstr(sys.executable or ''),
204-
jlstr(project),
205-
)
206-
res = jl_eval(script.encode('utf8'))
207-
if res is None:
208-
raise Exception('PythonCall.jl did not start properly')
209-
finally:
210-
os.chdir(d)
177+
jl_init = lib.jl_init_with_image__threading
178+
except AttributeError:
179+
jl_init = lib.jl_init_with_image
180+
jl_init.argtypes = [c.c_char_p, c.c_char_p]
181+
jl_init.restype = None
182+
jl_init(
183+
(default_bindir if bindir is None else bindir).encode('utf8'),
184+
None if sysimg is None else sysimg.encode('utf8'),
185+
)
186+
187+
# initialise PythonCall
188+
jl_eval = lib.jl_eval_string
189+
jl_eval.argtypes = [c.c_char_p]
190+
jl_eval.restype = c.c_void_p
191+
def jlstr(x):
192+
return 'raw"' + x.replace('"', '\\"').replace('\\', '\\\\') + '"'
193+
script = '''
194+
try
195+
Base.require(Main, :CompilerSupportLibraries_jll)
196+
import Pkg
197+
ENV["JULIA_PYTHONCALL_LIBPTR"] = {}
198+
ENV["JULIA_PYTHONCALL_EXE"] = {}
199+
Pkg.activate({}, io=devnull)
200+
import PythonCall
201+
catch err
202+
print(stderr, "ERROR: ")
203+
showerror(stderr, err, catch_backtrace())
204+
flush(stderr)
205+
rethrow()
206+
end
207+
'''.format(
208+
jlstr(str(c.pythonapi._handle)),
209+
jlstr(sys.executable or ''),
210+
jlstr(project),
211+
)
212+
res = jl_eval(script.encode('utf8'))
213+
if res is None:
214+
raise Exception('PythonCall.jl did not start properly')
211215

212216
CONFIG['inited'] = True
213217

0 commit comments

Comments
 (0)