Importing a julia variable into python using PyCall

Why does the following code produce an error?

A = 2+2
py"""
B = 2
C = A + B
"""

The error produced is:

NameError(“name ‘A’ is not defined”)

I guess that I need to pass A into the pycall, but how?

You need to interpolate with $, as is explained in the documentation:

julia> A = 2+2
4

julia> py"""
       B = 2
       C = $A + B
       """

julia> py"C"
6
1 Like