Skip to content

Commit 899518b

Browse files
committed
Add an API to safely add conda packages
If a package installed via `Conda.add` is not available for installed Python version, `conda` may downgrade Python: https://discourse.julialang.org/t/help-understanding-pycall-related-travis-failure/15957 This brakes PyCall because it stores the path to libpython etc. This patch provides a workaround to the issue by adding an API that wraps `Conda.add` and re-build PyCall if Python version is changed during installation of conda packages. fix JuliaPy/Conda.jl#127
1 parent fe67ef2 commit 899518b

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

src/PyCall.jl

+28
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ VERSION < v"0.7.0-beta2.199" && __precompile__()
33
module PyCall
44

55
using Compat, VersionParsing
6+
using Compat: Pkg, @info, @debug
67

78
export pycall, pycall!, pyimport, pyimport_e, pybuiltin, PyObject, PyReverseDims,
89
PyPtr, pyincref, pydecref, pyversion, PyArray, PyArray_Info,
@@ -686,6 +687,33 @@ function pyimport_conda(modulename::AbstractString, condapkg::AbstractString,
686687
end
687688
end
688689

690+
"""
691+
conda_add(packages::AbstractVector{<: AbstractString})
692+
693+
Install conda `pakcages` _if_ PyCall is configured to use Conda.jl;
694+
otherwise this function does nothing. It is intended to be used from
695+
`deps/build.jl` of the packages depending on PyCall, for safely
696+
installing Python packages via conda. More precisely, this function
697+
detects if Python executable is re-installed during installing
698+
`packages` and then re-build PyCall if it happens.
699+
"""
700+
function conda_add(packages::AbstractVector{<: AbstractString})
701+
if !conda
702+
@debug "PyCall is not configured to use Conda.jl. Skip installing $packages."
703+
return
704+
end
705+
706+
before = Conda.version("python")
707+
for pkg in packages
708+
Conda.add(pkg)
709+
end
710+
after = Conda.version("python")
711+
if before != after
712+
@info "Python version is changed from $before to $after. Re-building PyCall.jl"
713+
Pkg.build("PyCall")
714+
end
715+
end
716+
689717
#########################################################################
690718

691719
# look up a global builtin

0 commit comments

Comments
 (0)