user3303504
user3303504

Reputation: 555

PyCall builds but nothing works

After building and precompiling PyCall, I tried out some of the example code to just verify its functionality. Unfortunately I'm met with errors straight away.

julia> import Pkg

julia> Pkg.build("PyCall")
  Building Conda ─→ `C:\Users\Student\.julia\packages\Conda\kLXeC\deps\build.log`
  Building PyCall → `C:\Users\Student\.julia\packages\PyCall\ttONZ\deps\build.log`

julia> import PyCall

julia> math = pyimport("math")
ERROR: UndefVarError: pyimport not defined
Stacktrace:
 [1] top-level scope at none:0

julia> py"""
       import math
       math.sin(math.pi / 4)
       """
ERROR: LoadError: UndefVarError: @py_str not defined
in expression starting at REPL[5]:1

julia> py"import math"
ERROR: LoadError: UndefVarError: @py_str not defined
in expression starting at REPL[6]:1

I'm using Julia 1.1.1 (2019-05-16) on Windows 10 x64. I'm not seeing anything online regards to this specific issue and am a touch on the frustrated side with it now.

Upvotes: 0

Views: 888

Answers (1)

DocDave
DocDave

Reputation: 146

As you are importing PyCall, you must specify the scope of the pyimport() function by prefixing it with the module name itself, i.e.

julia> import PyCall

julia> math = PyCall.pyimport("math")
PyObject <module 'math' from '/usr/lib/python3.7/lib-dynload/math.cpython-37m-x86_64-linux-gnu.so'>

julia> math.sin(math.pi/4)
0.7071067811865475

Alternatively, as Gomiero mentioned in discussion, your problem can also be resolved by bringing the functions into local scope by utilising using PyCall instead;

julia> using PyCall

julia> math = pyimport("math")
PyObject <module 'math' from '/usr/lib/python3.7/lib-dynload/math.cpython-37m-x86_64-linux-gnu.so'>

julia> math.sin(math.pi/4)
0.7071067811865475

There is more on this in the Julia documentation.

Upvotes: 3

Related Questions