Ok thank you for working on it.

I will just use PyPlot.plot for now and see if anyone can help us 
understand what is happening here.

On Friday, August 22, 2014 1:24:35 PM UTC-4, Steve Kelly wrote:

Changing plot() to PyPlot.plot() fixes it for me. 
>
> type Foo
>
>
>     x::Int
> end
>
> function check_defined_err(x::Symbol)
>     if !isdefined(x)
>         error("Module $x not defined. run `using $x` to fix the problem")
>     end
> end
>
> function check_defined_eval(x::Symbol)
>     if !isdefined(x)
>         eval(Expr(:using, x))
>     end
> end
>
> function plot_err(x::Foo)
>     check_defined_err(:PyPlot)
>     d = 1:x.x
>     PyPlot.plot(d, d.^2)
> end
>
> function plot_eval(x::Foo)
>
>
>     check_defined_eval(:PyPlot)
>     d = 1:x.x
>     PyPlot.plot(d, d.^2)
> end
>
> a = Foo(10)
> try
>     plot_err(a)
> catch err
>     println(err)
> end
> plot_eval(a)
>
> This also eliminates the warning:
>
> Warning: using PyPlot.plot in module Main conflicts with an existing 
> identifier.
>
> Which makes me think you analysis is correct. When Julia evaluates the 
> function it must be binding the name "plot" to something. Maybe somebody 
> more knowledgeable about the pipeline can chime in. I don't think this is a 
> scoping issue, as much as it is intrinsic to the eval and compilation steps.
>
> On Fri, Aug 22, 2014 at 12:49 PM, Spencer Lyon <[email protected] 
> <javascript:>> wrote:
>
>> I’m actually still having issues with both of these options — I’ll try to 
>> enumerate what I think the problem is here. 
>>
>>    - When I do using MyPackage and my code is loaded (including the 
>>    plotting routines). 
>>    - Then when I call plot(x<:MyType) (sorry for the shorthand), the 
>>    function is compiled. 
>>    - If PyPlot is not defined I try to either define it for them or emit 
>>    an error message telling them to define it. 
>>    - After PyPlot is defined I try to run the file again, and I get 
>>    errors telling me the functions from PyPlot that I use in the 
>>    plot(x::MyType) function are not defined. 
>>
>> I think the reason they are not defined within the function is that it 
>> was compiled the first time I called it, when the functions weren’t 
>> actually available. 
>>
>> Does my analysis seem correct?
>>
>> Does anyone know a way to accomplish this?
>> ------------------------------
>>
>> I will provide a quick usable example so it is easy for people to 
>> experiment
>>
>> type Foo
>>     x::Int
>> end
>>
>> function check_defined_err(x::Symbol)
>>     if !isdefined(x)
>>         error("Module $x not defined. run `using $x` to fix the problem")
>>     end
>> end
>>
>> function check_defined_eval(x::Symbol)
>>     if !isdefined(x)
>>         eval(Expr(:using, x))
>>     end
>> end
>>
>> function plot_err(x::Foo)
>>     check_defined_err(:PyPlot)
>>     d = 1:x
>>     plot(d, d.^2)
>> end
>>
>> function plot_eval(x::Foo)
>>     check_defined_eval(:PyPlot)
>>     d = 1:x
>>     plot(d, d.^2)
>> end
>>
>> a = Foo(10)
>> # plot_err(a)
>> # plot_eval(a)
>>
>> Then when I have run the code above, I get the following at the console 
>> (it says string because I :
>>
>> julia> plot_err(a)
>> ERROR: Module PyPlot not defined. run `using PyPlot` to fix the problem
>>  in error at /usr/local/julia/usr/lib/julia/sys.dylib
>>  in check_defined_err at none:7
>>  in plot_err at string:18
>>
>> julia> using PyPlot
>> INFO: Loading help data...
>> Warning: using PyPlot.plot in module Main conflicts with an existing 
>> identifier.
>>
>> julia> plot_err(a)
>> ERROR: plot not defined
>>  in plot_err at none:20
>>
>> In another session to see plot_eval(a):
>>
>> julia> plot_eval(a)
>> INFO: Loading help data...
>> Warning: using PyPlot.plot in module Main conflicts with an existing 
>> identifier.
>> ERROR: plot not defined
>>  in plot_eval at none:4
>>
>> Thank you for your help.
>>
>> On Friday, August 22, 2014 12:18:14 PM UTC-4, Steve Kelly wrote:
>>
>> Peter Simon, very cool. When I ran hit this problem I saw it as an 
>>> opportunity to make my code more Julian. :P Eval FTW.
>>>
>>>
>>> On Fri, Aug 22, 2014 at 12:16 PM, Peter Simon <[email protected]> 
>>> wrote:
>>>
>>>> From https://groups.google.com/d/topic/julia-users/
>>>> AWCerAdDLQo/discussion :
>>>>
>>>>  eval(Expr(:using,:PyPlot))
>>>>
>>>> can be used inside a conditional.
>>>>
>>>> --Peter
>>>>
>>>>
>>>> On Friday, August 22, 2014 8:56:53 AM UTC-7, Spencer Lyon 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?
>>>>> ​
>>>>>
>>>>
>>>  ​
>>
>
>  ​

Reply via email to