Module not reading properly

The use of modules in Julia is painful for me, after using many languages. However, I am persevering.

In a file pickle.jl, I have the following code:

module Pickle
export mypickle, myunpickle

using PyCall
@pyimport pickle

function mypickle(filename, obj)
    out = open(filename,"w")
    pickle.dump(obj, out)
    close(out)
end

function myunpickle(filename)
    r = nothing
    @pywith pybuiltin("open")(filename,"rb") as f begin
        r = pickle.load(f)
    end
    return r
end

end

I include this file from main.jl`:

include("pickle.jl")

and get the error:

julia> include("pickle.jl")
ERROR: LoadError: syntax: extra token "Pickle" after end of expression
Stacktrace:
 [1] top-level scope at /Users/erlebach/covid_modeling_julia/julia_code/sir-julia/notebook/SIRsims/gordon_julia_code/household_workplaces_leon/pickle.jl:1
 [2] include(::String) at ./client.jl:439
 [3] top-level scope at none:0
in expression starting at /Users/erlebach/covid_modeling_julia/julia_code/sir-julia/notebook/SIRsims/gordon_julia_code/household_workplaces_leon/pickle.jl:1

julia> 

Am I doing something obviously wrong? I do not see it.

I don’t think this is correct:

    @pywith pybuiltin("open")(filename,"rb") as f begin
        r = pickle.load(f)
    end

I think it should be either:

    @pywith pybuiltin("open")(filename,"rb") as f
        r = pickle.load(f)
    end

Or maybe:

    @pywith begin 
        pybuiltin("open")(filename,"rb") as f
            r = pickle.load(f)
       end
    end

I ran exactly the code you posted, both by pasting it into the REPL and using include(), and I cannot reproduce the error. Are you sure you’ve posted exactly the code that you are running? Can you try starting julia and then pasting your exact module definition from this post?

1 Like

I am sure. After fixing some problems with Atom/Juno, I am back in business. I did as you suggested, and it worked! So the problem must have been a hidden character. I had copy/pasted the code from some website. Thank you for the advice.