Thanks Steve, That is a good solution. I think I will probably end up doing this, or maybe even a combination of what you and Peter said and instead of throwing an error if it isn't defined, I will just define it for them.
On Friday, August 22, 2014 12:16:14 PM UTC-4, Steve Kelly wrote: > > What I've been doing is very similar. I leave the plotting package up to > the user, but they are first required to do one of: using PyPlot or using > Gadfly inside their script. > > I then have a function similar to yours (named plot(x::MyType), to take > advantage of multiple dispatch) that checks for isdefined(:PyPlot) or > isdefined(:Gadfly). If neither is defined I throw an error telling the user > to include a plotting package in their script. This keeps load times quick > if they are not using plotting functionality. > > To answer you question, conditional import inside functions is not > possible. > > > > On Fri, Aug 22, 2014 at 11:56 AM, Spencer Lyon <[email protected] > <javascript:>> wrote: > >> I am working on a library that defines various types as well as a few >> “helper” functions to plot those types with PyPlot. >> >> If I do [import|using] PyPlot at the top level of any file in my >> package, PyPlot is loaded when I do [using|import] MyPackage. This makes >> the startup time for my package much much longer. >> >> What I would like to do is instead of having to load it when my package >> loads, I could load it when someone calls one of the functions that needs >> it. >> >> Here is an example of what I would like to do: >> >> function plot_my_type(x::MyType) >> if !isdefined(:PyPlot) >> using PyPlot >> end >> # finish the function by plotting with PyPlot >> end >> >> I haven’t been able to get a solution that works for this. Does anyone >> know if it is possible? >> >> > >
