JuliaPy / PythonCall.jl

Python and Julia in harmony.
https://juliapy.github.io/PythonCall.jl/stable/
MIT License
776 stars 63 forks source link

Add example using import numpy in with pyexec or @pyexec. Can't get it to work. #387

Closed hubert-associates closed 11 months ago

hubert-associates commented 11 months ago

I'm trying to use pyexec or @pyexec as shown here , but I need some imports (numpy, azure.cosmos, os...). Anywhere/Anyway to document how that works? Thanks for the great PythonCall.jl.

https://discourse.julialang.org/t/define-python-function-in-julia-file-with-pythoncall/98593/5

cjdoris commented 11 months ago

You can do imports in the code:

julia> @pyexec """
           import numpy
           x = numpy.zeros((2,3))
           """ => x;

julia> x
Python:
array([[0., 0., 0.],
       [0., 0., 0.]])

Or you can do the import once in a separate command:

julia> @pyexec """
           global numpy
           import numpy
           """

julia> @pyexec """
           x = numpy.zeros((2,3))
           """ => x;

julia> x
Python:
array([[0., 0., 0.],
       [0., 0., 0.]])
hubert-associates commented 11 months ago

Sincere thanks for the promt answer "user's guide" content. I think I was missing the "global". I'll close once I'm sure...