4

I'm wanting to use a range of Python libraries in my Julia code. I've been trying to use PyCall to access the Python libraries I have installed on my (windows) PC already; however, I have been unable to reconfigure PyCall away from its default Python distribution which is private to Julia. Following the steps in the documentation (https://github.com/JuliaPy/PyCall.jl), I attempted the following in Julia:

using Pkg

ENV["PYTHON"] = raw"C:\Users\catod\AppData\Local\Programs\Python\Python37-32\python.exe"

Pkg.build(PyCall)

After exiting and reentering Julia, I attempted to run

using PyCall

plt = pyimport("matplotlib.pyplot")

but got the following error message:

ERROR: LoadError: PyError (PyImport_ImportModule

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

PyCall is currently configured to use the Julia-specific Python distribution
installed by the Conda.jl package.  To install the matplotlib.pyplot module, you can      
use `pyimport_conda("matplotlib.pyplot", PKG)`, where PKG is the Anaconda
package the contains the module matplotlib.pyplot, or alternatively you can use the
Conda package directly (via `using Conda` followed by `Conda.add` etcetera).

Alternatively, if you want to use a different Python distribution on your
system, such as a system-wide Python (as opposed to the Julia-specific Python),
you can re-configure PyCall with that Python.   As explained in the PyCall
documentation, set ENV["PYTHON"] to the path/name of the python executable
you want to use, run Pkg.build("PyCall"), and re-launch Julia.

) <class 'ModuleNotFoundError'>
ModuleNotFoundError("No module named 'matplotlib'")

Can anyone point out where I have gone wrong?

Caleb
  • 65
  • 5
  • Did you try adding `ENV["PYTHON"] = raw"C:\Users\catod\AppData\Local\Programs\Python\Python37-32\python.exe"` to your julia [startup file](https://stackoverflow.com/a/52175490/11213215). – rashid Oct 10 '20 at 06:24
  • @rashid I have, and that doesn't work. – Caleb Oct 10 '20 at 08:54
  • It seems there are some similar [github issues](https://github.com/JuliaPy/PyCall.jl/issues?q=windows) with PyCall on windows. This [issue](https://github.com/JuliaPy/PyCall.jl/issues/730) in particular seems to be similar to what you are facing. But it seems that it has been solved. If looking at it doesn't solve your issue, it might be worth posting a new issue there. – rashid Oct 11 '20 at 04:22

1 Answers1

1

There are two problems here:

  1. Matplotlib is not installed in your Python installation. You need to run (however I definitely recommend Anaconda instead - see notes at the end)

    pip install matplotlib
    
  2. Even if you manage to install matplolib your code will very ugly crash where you try to show the first plot because it is not possible to embed matlplotlib directly to Julia without loading PyPlot. Hence your code should be:

    using PyCall
    using PyPlot
    plt = pyimport("matplotlib.pyplot")
    

now another problem is that normally PyPlot is installing all packages it needs via conda. Since you try to use a plain Python you will need to manually install those packages.

Note

Basically, one should need to use Anaconda to have a good Julia-Python experience because in this scenario there are many tools that automate the integration.

Using the Julia-inbuilt Python is the easiest option. I definitely recommend:

ENV["PYTHON"] = ""
pkg"build PyCall"
using Conda
Conda.add("matplotlib")

Another relatively easy option is Anaconda. Install your own Anaconda and set ENV["PYTHON"] to it, build it and enjoy. Using a non-Anaconda Python will never be nice because you will still be unable to use Conda.jl to manage installation.

Przemyslaw Szufel
  • 40,002
  • 3
  • 32
  • 62
  • 1
    This is a valid solution, and one I'll try out. However, I really would like to use the (fairly extensive) list of libraries I already have; doing it with Julia's inbuilt Python feels like unnecessary duplication. Actually, the main thing I want to know is why my method hasn't worked, since it is taken directly from the PyCall documentation. – Caleb Oct 10 '20 at 10:34
  • 1
    Your method did not work because `matplotlib` is not installed. Now this is quite complicated what *not installed* means in Python - basically the Python context that was loaded from Julia could not find `matplotlib` library. You need to install `matplotlib` and make sure `PYTHONPATH` is properly configured. – Przemyslaw Szufel Oct 10 '20 at 12:35
  • If you have actually installed `matplotlib` you can try to evaluate your Python configuration by running `dump(PyCall.python_cmd())` and take appropriate actions depending on what you see. unfortunately there is no one-fits-all solution other than using Anaconda for this type of integration – Przemyslaw Szufel Oct 10 '20 at 12:45
  • I definitely have matplotlib installed in the python distribution which I was attempting to get Julia to use. The part of my failure which confuses me is that the error message tells me I am using the Julia-specific distribution, even though I followed PyCall's instructions to change said distribution. You've convinced me that I should probably just use the Julia-specific distribution anyway, but I am still wondering what went wrong with my process. – Caleb Oct 11 '20 at 01:18
  • It's tricky how Python finds which instance is being used at a given second. What did `dump(PyCall.python_cmd())` show? – Przemyslaw Szufel Oct 11 '20 at 01:34