Skip to content
Advertisement

PyCall can’t find scipy in Julia

I’m currently rewriting a bunch of matlab code into julia. These codes envolves a lot of math and, particularly, interpolation functions for a 3D mesh. It is easy to deal with this in matlab: all I need to do is to use interp3 function. Once I coundn’t find any simple way to do similar in Julia, I’m trying to use some Scipy features through PyCall. Now, the problem: I’ve already installed PyCall, changed ENV[PYTHON] to the path of my own installed anaconda. No metter what, and I extensively looked for solutions, I still get the following error message:

julia> pyimport("scipy")
ERROR: PyError (PyImport_ImportModule

The Python package scipy could not be found by pyimport. Usually this means
that you did not install scipy in the Python version being used by PyCall.

PyCall is currently configured to use the Python version at:

/usr/bin/python3

and you should use whatever mechanism you usually use (apt-get, pip, conda,
etcetera) to install the Python package containing the scipy module.

One alternative is to re-configure PyCall to use a different Python
version on your system: set ENV["PYTHON"] to the path/name of the python
executable you want to use, run Pkg.build("PyCall"), and re-launch Julia.

Another alternative is to configure PyCall to use a Julia-specific Python
distribution via the Conda.jl package (which installs a private Anaconda
Python distribution), which has the advantage that packages can be installed
and kept up-to-date via Julia.  As explained in the PyCall documentation,
set ENV["PYTHON"]="", run Pkg.build("PyCall"), and re-launch Julia. Then,
To install the scipy module, you can use `pyimport_conda("scipy", PKG)`,
where PKG is the Anaconda package the contains the module scipy,
or alternatively you can use the Conda package directly (via
`using Conda` followed by `Conda.add` etcetera).

) <class 'ModuleNotFoundError'>
ModuleNotFoundError("No module named 'scipy'",)

Stacktrace:
 [1] pyimport(::String) at /home/gabriel/.julia/packages/PyCall/zqDXB/src/PyCall.jl:536
 [2] top-level scope at none:0

Also, everything I tried, I tried both on windows 10 and Linux. I don’t know what to do anymore! I would much appreciate the help! Thanks in advance!

Advertisement

Answer

Install scipy with Conda – Julia’s interface to Python’s packages.

using Conda
Conda.add("scipy")

now pyimport("scipy") will work like charm.

Note that with a custom Python installation various things can happen (and you are left on your own with managing that), hence I recommend you to use Python built-into Julia.

This is how you switch back to in-built Python:

using Pkg
ENV["PYTHON"]=""
Pkg.build("PyCall")
Advertisement