`PyPlot`: imported in module vs imported in REPL

question
package
pyplot
#1

I am hoping to use PyPlot functions in a module MyTest. I create the MyTest project/package, add PyCall, PyPlot, and Conda to MyTest's Project.toml, and compare what happens when I try using the module vs using PyPlot in REPL.

(v1.1) pkg> generate MyTest
Generating project MyTest:
    MyTest/Project.toml
    MyTest/src/MyTest.jl

(v1.1) pkg> activate MyTest

(MyTest) pkg> add PyPlot PyCall Conda
 Updating registry at `~/.julia/registries/General`
  Updating git-repo `https://github.com/JuliaRegistries/General.git`
 Resolving package versions...
  Updating `/USER/Desktop/MyTest/Project.toml`
  [438e738f] + PyCall v1.18.5
  [d330b81b] + PyPlot v2.7.0
  [8f4d0f93] + Conda v1.2.0
  Updating `/USER/Desktop/MyTest/Manifest.toml`
  [3da002f7] + ColorTypes v0.7.5
  [5ae59095] + Colors v0.9.5
  [34da2185] + Compat v1.5.1
  [8f4d0f93] + Conda v1.2.0
  [53c48c17] + FixedPointNumbers v0.5.3
  [682c06a0] + JSON v0.20.0
  [b964fa9f] + LaTeXStrings v1.0.3
  [1914dd2f] + MacroTools v0.4.5
  [438e738f] + PyCall v1.18.5
  [d330b81b] + PyPlot v2.7.0
  [189a3867] + Reexport v0.2.0
  [81def892] + VersionParsing v1.1.3
  [2a0f44e3] + Base64 
  [ade2ca70] + Dates 
  [8bb1440f] + DelimitedFiles 
  [8ba89e20] + Distributed 
  [b77e0a4c] + InteractiveUtils 
  [76f85450] + LibGit2 
  [8f399da3] + Libdl 
  [37e2e46d] + LinearAlgebra 
  [56ddb016] + Logging 
  [d6f4376e] + Markdown 
  [a63ad114] + Mmap 
  [44cfe95a] + Pkg 
  [de0858da] + Printf 
  [3fa0cd96] + REPL 
  [9a3f8284] + Random 
  [ea8e919c] + SHA 
  [9e88b42a] + Serialization 
  [1a1011a3] + SharedArrays 
  [6462fe0b] + Sockets 
  [2f01184e] + SparseArrays 
  [10745b16] + Statistics 
  [8dfed614] + Test 
  [cf7118a7] + UUIDs 
  [4ec0a83e] + Unicode 

Then from REPL I try

  1. using PyPlot; this works fine
  2. using MyTest; (where JULIA_LOAD_PATH has been updated to include path_to_/MyTest/src/)

In the below, (1) works fine, but (2) gives the following error:

julia> using PyPlot

julia> using MyTest
[ Info: Precompiling MyTest [top-level]
┌ Warning: No working GUI backend found for matplotlib
└ @ PyPlot ~/.julia/packages/PyPlot/mQXSC/src/init.jl:160
ERROR: LoadError: InitError: PyError (PyImport_ImportModule

The Python package matplotlib.pyplot could not be found 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 'ImportError'>
ImportError("/lib/x86_64-linux-gnu/libz.so.1: version `ZLIB_1.2.9' not found (required by /USER/.julia/conda/3/lib/python3.7/site-packages/matplotlib/../../.././libpng16.so.16)")
  File "/USER/.julia/conda/3/lib/python3.7/site-packages/matplotlib/pyplot.py", line 32, in <module>
    import matplotlib.colorbar
  File "/USER/.julia/conda/3/lib/python3.7/site-packages/matplotlib/colorbar.py", line 32, in <module>
    import matplotlib.contour as contour
  File "/homes/jmroth/.julia/conda/3/lib/python3.7/site-packages/matplotlib/contour.py", line 18, in <module>
    import matplotlib.font_manager as font_manager
  File "/USER/.julia/conda/3/lib/python3.7/site-packages/matplotlib/font_manager.py", line 48, in <module>
    from matplotlib import afm, cbook, ft2font, rcParams, get_cachedir

Do I have to somehow adjust the ENV["PYTHONPATH"] in MyTest? I think I previously did that for REPL, but I’m confused about why it works from REPL and not inside my module.

0 Likes

#2

It seems that building the package works, for using but not plotting ie,

(MyTest) pkg> build PyPlot
  Building Conda ─→ `~/.julia/packages/Conda/CpuvI/deps/build.log`
  Building PyCall → `~/.julia/packages/PyCall/0jMpb/deps/build.log`

julia> using PyPlot

julia> using MyTest
[ Info: Precompiling MyTest [426238aa-3ae9-1julia> using PyPlot

julia> MyTest.plot(1:30)
QStandardPaths: XDG_RUNTIME_DIR not set, defaulting to '/tmp/runtime-jmroth'
libGL error: failed to open drm device: Permission denied
libGL error: failed to load driver: i965

signal (11): Segmentation fault
in expression starting at no file:0
1e9-3f50-5171f6c85314]
0 Likes

#3

On a Linux, the following process worked for me (somehow, the Conda build with ENV["PYTHON"]="" didn’t work):

ENV["PYTHON"] = "/USER/.conda/envs/python3.7/bin/python"  ## (ie, just set this variable to a path which can call python and successfully `import matplotlib.pyplot` after calling this version/path of python)
(v1.1) pkg> rm PyCall
(v1.1) pkg> rm PyPlot
(v1.1) pkg> rm Conda
(v1.1) pkg> add PyCall
(v1.1) pkg> build PyCall
(v1.1) pkg> add PyPlot
(v1.1) pkg> build PyPlot
julia> using PyPlot
julia> plot(1:10)
0 Likes