Maybe someone can give advice how to remove PyPlot from the distribution of a Julia package in which it is used, in particular from package ModiaMath. So, this means “PyPlot” should not be within the Project.toml file of ModiaMath. The reasons for this requirement are explained here. The behaviour should be:
- If PyPlot is present in the actual environment, ModiaMath.plot(…) makes a PyPlot plot.
- If PyPlot is not in the actual environment, ModiaMath.plot(…) shall give the information message that PyPlot is not installed.
The following test package demonstrates a solution in Julia 1.0.0:
module TestPlot1 export myplot const PyPlotAvailable = fill(false,1) function __init__() @eval Main begin try import PyPlot catch println("... PyPlot not installed (plot commands will be ignored)") end end PyPlotAvailable = isdefined(Main, :PyPlot) end function myplot(x,y) if PyPlotAvailable Main.PyPlot.plot(x,y) else println("no plot, because PyPlot not installed") end end
Once this package is added to the actual environment (]add TestPlot1), then it can be used as:
using TestPlot1 t=collect(0:0.01:10); y=sin.(t) myplot(t,y)
and either a plot appears (if PyPlot is in the current environment) or an information message is printed.
@ChrisRackauckas adviced to not use this unsafe implementation but use one that is based on package
Requires.jl. I tried it, but until now failed to come up with a solution. It should be possible, because package Plots.jl has the feature above and uses
Requires.jl (but I did not manage to understand how this is done in Plots.jl).
Here is one of my trials:
module TestPlot2 using Requires export myplot const PyPlotAvailable = fill(false,1) function __init__() @require PyPlot = "d330b81b-6aea-500a-939a-2ce795aea3ee" begin println("... PyPlot will be loaded.") @eval begin import PyPlot global PyPlotAvailable = true pyplot_plot(x,y) = PyPlot.plot(x,y) end end end function myplot(x,y) if PyPlotAvailable pyplot_plot(x,y) else println("no plot, because PyPlot not installed") end end end
However, this solution requires that
using PyPlot has to be explicitely given in the REPL. Once this is done a warning message appears:
julia> using PyPlot ... PyPlot will be loaded. ┌ Warning: Package TestPlot2 does not have PyPlot in its dependencies: │ - If you have TestPlot2 checked out for development and have │ added PyPlot as a dependency but haven't updated your primary │ environment's manifest file, try `Pkg.resolve()`. │ - Otherwise you may need to report an issue with TestPlot2 └ Loading PyPlot into TestPlot2 from project dependency, future warnings for TestPlot2 are suppressed.
To summarize, can someone give advice for a safer implementation and without requiring that the user has to define “using PyPlot” in the REPL and without getting this warning message.