Skip to content

Commit 2592c3d

Browse files
author
Christopher Doris
committed
work if PyCall already running with the same libpython
1 parent cb10de4 commit 2592c3d

File tree

2 files changed

+43
-39
lines changed

2 files changed

+43
-39
lines changed

src/Python.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ include("utils.jl")
4141
isembedded::Bool = false
4242
"True if the Python interpreter is currently initialized."
4343
isinitialized::Bool = false
44+
"True if the Python interpreter was already initialized."
45+
preinitialized::Bool = false
4446
"The running Python version."
4547
version::VersionNumber = VersionNumber(0)
4648
"True if this is the Python in some Conda environment."

src/init.jl

Lines changed: 41 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
CONFIG.libptr = Ptr{Cvoid}(parse(UInt, ENV["PYTHONJL_LIBPTR"]))
88
# Check Python is initialized
99
C.Py_IsInitialized() == 0 && error("Python is not already initialized.")
10+
CONFIG.isinitialized = CONFIG.preinitialized = true
1011
else
1112
# Find Python executable
1213
exepath = something(
@@ -86,57 +87,58 @@
8687
""")
8788
end
8889

89-
# Check we are not already initialized
90-
C.Py_IsInitialized() == 0 || error("Python is already initialized.")
91-
9290
# Initialize
9391
with_gil() do
94-
# Find ProgramName and PythonHome
95-
script = if Sys.iswindows()
96-
"""
97-
import sys
98-
print(sys.executable)
99-
if hasattr(sys, "base_exec_prefix"):
100-
sys.stdout.write(sys.base_exec_prefix)
101-
else:
102-
sys.stdout.write(sys.exec_prefix)
103-
"""
92+
if C.Py_IsInitialized() != 0
93+
# Already initialized (maybe you're using PyCall as well)
10494
else
105-
"""
106-
import sys
107-
print(sys.executable)
108-
if hasattr(sys, "base_exec_prefix"):
109-
sys.stdout.write(sys.base_prefix)
110-
sys.stdout.write(":")
111-
sys.stdout.write(sys.base_exec_prefix)
112-
else:
113-
sys.stdout.write(sys.prefix)
114-
sys.stdout.write(":")
115-
sys.stdout.write(sys.exec_prefix)
116-
"""
117-
end
118-
CONFIG.pyprogname, CONFIG.pyhome = readlines(python_cmd(["-c", script]))
95+
# Find ProgramName and PythonHome
96+
script = if Sys.iswindows()
97+
"""
98+
import sys
99+
print(sys.executable)
100+
if hasattr(sys, "base_exec_prefix"):
101+
sys.stdout.write(sys.base_exec_prefix)
102+
else:
103+
sys.stdout.write(sys.exec_prefix)
104+
"""
105+
else
106+
"""
107+
import sys
108+
print(sys.executable)
109+
if hasattr(sys, "base_exec_prefix"):
110+
sys.stdout.write(sys.base_prefix)
111+
sys.stdout.write(":")
112+
sys.stdout.write(sys.base_exec_prefix)
113+
else:
114+
sys.stdout.write(sys.prefix)
115+
sys.stdout.write(":")
116+
sys.stdout.write(sys.exec_prefix)
117+
"""
118+
end
119+
CONFIG.pyprogname, CONFIG.pyhome = readlines(python_cmd(["-c", script]))
119120

120-
# Set PythonHome
121-
CONFIG.pyhome_w = Base.cconvert(Cwstring, CONFIG.pyhome)
122-
C.Py_SetPythonHome(pointer(CONFIG.pyhome_w))
121+
# Set PythonHome
122+
CONFIG.pyhome_w = Base.cconvert(Cwstring, CONFIG.pyhome)
123+
C.Py_SetPythonHome(pointer(CONFIG.pyhome_w))
123124

124-
# Set ProgramName
125-
CONFIG.pyprogname_w = Base.cconvert(Cwstring, CONFIG.pyprogname)
126-
C.Py_SetProgramName(pointer(CONFIG.pyprogname_w))
125+
# Set ProgramName
126+
CONFIG.pyprogname_w = Base.cconvert(Cwstring, CONFIG.pyprogname)
127+
C.Py_SetProgramName(pointer(CONFIG.pyprogname_w))
127128

128-
# Start the interpreter and register exit hooks
129-
C.Py_InitializeEx(0)
129+
# Start the interpreter and register exit hooks
130+
C.Py_InitializeEx(0)
131+
atexit() do
132+
CONFIG.isinitialized = false
133+
CONFIG.version < v"3.6" ? C.Py_Finalize() : checkm1(C.Py_FinalizeEx())
134+
end
135+
end
130136
CONFIG.isinitialized = true
131137
check(
132138
C.Py_AtExit(
133139
@cfunction(() -> (CONFIG.isinitialized = false; nothing), Cvoid, ())
134140
),
135141
)
136-
atexit() do
137-
CONFIG.isinitialized = false
138-
CONFIG.version < v"3.6" ? C.Py_Finalize() : checkm1(C.Py_FinalizeEx())
139-
end
140142
end
141143
end
142144

0 commit comments

Comments
 (0)