@pyimport creates a new Julia module, which you also can’t do inside a function:
julia> function foo()
module Bar
x = 1
end
end
ERROR: syntax: module expression not at top level
Instead, you can use the pyimport() function, which does not create a Julia module. You’ll just have to access functions and classes with the indexing notation instead of dot notation:
julia> function foo()
sys = pyimport("sys")
println(sys[:executable]) # instead of sys.executable
end
foo (generic function with 1 method)
julia> foo()
/Users/rdeits/.julia/v0.6/Conda/deps/usr/bin/python