Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiline evaluation #210

Closed
charstnut opened this issue Aug 8, 2022 · 6 comments
Closed

Multiline evaluation #210

charstnut opened this issue Aug 8, 2022 · 6 comments

Comments

@charstnut
Copy link

Hi,
I am wondering if there is any API for multiline python code evaluation? I have gone through the documentation but was not able to find a clear example of this use case. Thank you for the help!

@cjdoris
Copy link
Collaborator

cjdoris commented Aug 9, 2022

You can do

pyeval("""
x = 2
y = 3
print(x + y)
""")

and similar with @pyeval etc. Is this what you mean?

@charstnut
Copy link
Author

Yes, thanks that's exactly what I meant. I was using the with open(...) as f: statements and that gives syntax error when evaluated. In that case do I have to use the pywith function?

@charstnut
Copy link
Author

In fact I am getting MethodError and SyntaxError from python with the following two scenarios:

using PythonCall

pyeval("""
x = 2
y = 3
print(x + y)
""")
ERROR: LoadError: MethodError: no method matching pyeval(::String)
Closest candidates are:
  pyeval(::Any, ::Any) at ~/.julia/packages/PythonCall/d8Xcl/src/concrete/code.jl:47
  pyeval(::Any, ::Any, ::Any) at ~/.julia/packages/PythonCall/d8Xcl/src/concrete/code.jl:47
  pyeval(::Type{T}, ::Any, ::Any) where T at ~/.julia/packages/PythonCall/d8Xcl/src/concrete/code.jl:41

using PythonCall

pyeval("""
x = 2
y = 3
print(x + y)
""", Main)
ERROR: LoadError: Python: SyntaxError: invalid syntax (<string>, line 1)
Python stacktrace: none
Stacktrace:
  [1] pythrow()
    @ PythonCall ~/.julia/packages/PythonCall/d8Xcl/src/err.jl:94
  [2] errcheck
    @ ~/.julia/packages/PythonCall/d8Xcl/src/err.jl:10 [inlined]
  [3] pycallargs(f::Py, args::Py)
    @ PythonCall ~/.julia/packages/PythonCall/d8Xcl/src/abstract/object.jl:210
  [4] pycall(::Py, ::String, ::Vararg{Any}; kwargs::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
    @ PythonCall ~/.julia/packages/PythonCall/d8Xcl/src/abstract/object.jl:228
  [5] pycall
    @ ~/.julia/packages/PythonCall/d8Xcl/src/abstract/object.jl:218 [inlined]
  [6] #_#13
    @ ~/.julia/packages/PythonCall/d8Xcl/src/Py.jl:358 [inlined]
  [7] Py
    @ ~/.julia/packages/PythonCall/d8Xcl/src/Py.jl:358 [inlined]
  [8] pyeval(::Type{Py}, code::String, globals::Module, locals::Nothing)
    @ PythonCall ~/.julia/packages/PythonCall/d8Xcl/src/concrete/code.jl:43
  [9] pyeval (repeats 2 times)
    @ ~/.julia/packages/PythonCall/d8Xcl/src/concrete/code.jl:47 [inlined]

@cjdoris
Copy link
Collaborator

cjdoris commented Aug 10, 2022

Sorry I was mistaken. It's pyexec you need, which executes arbitrary Python code, whereas pyeval evaluates a single expression. And as you have discovered, you also need to specify the global scope (and optionally a local scope).

@cjdoris
Copy link
Collaborator

cjdoris commented Aug 20, 2022

I think this is resolved.

@cjdoris cjdoris closed this as completed Aug 20, 2022
@charstnut
Copy link
Author

Thanks @cjdoris it works as intended!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants