JuliaPy / PythonCall.jl

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

Using `seval` in a new module created from Python #170

Closed dingraha closed 2 years ago

dingraha commented 2 years ago

It looks like the prefered way of using or importing a Julia module from Python is to use seval. The docs also recommend creating a new module using the newmodule command to avoid polluting the Main namespace. But when I try to do anything with seval and a new module, I get an error saying that eval doesn't exist in the new module. It appears that newmodule creates a baremodule. Is this expected?

Here's an example:

import juliacall; jl = juliacall.newmodule("Foo")
# import juliacall; jl = juliacall.Main
print(jl.seval("1 + 2"))

The error I get:

(venv) dingraha@GRLRL2021060743 ~/p/p/test3> python simple_seval.py
[juliapkg] Locating Julia ^1.6
[juliapkg] Using Julia 1.7.2 at /home/dingraha/.gpfx/home/dingraha/local/julia/1.7.2/bin/julia
[juliapkg] Using Julia project at /home/dingraha/projects/pythoncall_openmdao
[juliapkg] Installing packages:
           julia> import Pkg
           julia> Pkg.add([Pkg.PackageSpec(name="PythonCall", uuid="6099a3de-0909-46bc-b1f4-468b9a2dfc0d")])
           julia> Pkg.resolve()
    Updating registry at `~/.julia/registries/DanielIngrahamRegistry`
    Updating git-repo `git@github.com:dingraha/DanielIngrahamRegistry.git`
    Updating registry at `~/.julia/registries/General.toml`
   Resolving package versions...
  No Changes to `~/projects/pythoncall_openmdao/Project.toml`
  No Changes to `~/projects/pythoncall_openmdao/Manifest.toml`
  No Changes to `~/projects/pythoncall_openmdao/Project.toml`
  No Changes to `~/projects/pythoncall_openmdao/Manifest.toml`
Traceback (most recent call last):
  File "/home/dingraha/projects/pythoncall_openmdao/test3/simple_seval.py", line 3, in <module>
    print(jl.seval("1 + 2"))
  File "/home/dingraha/.julia/packages/PythonCall/XgP8G/src/jlwrap/module.jl:19", line 7, in seval
juliacall.JuliaError: MethodError: no method matching eval(::Expr)
You may have intended to import Base.eval
Closest candidates are:
  eval(!Matched::Module, !Matched::Any) at ~/.gpfx/home/dingraha/local/julia/1.7.2/share/julia/base/boot.jl:373
Stacktrace:
 [1] pyjlmodule_seval(self::Module, expr::PythonCall.Py)
   @ PythonCall ~/.julia/packages/PythonCall/XgP8G/src/jlwrap/module.jl:13
 [2] _pyjl_callmethod(f::Any, self_::Ptr{PythonCall.C.PyObject}, args_::Ptr{PythonCall.C.PyObject}, nargs::Int64)
   @ PythonCall ~/.julia/packages/PythonCall/XgP8G/src/jlwrap/base.jl:62
 [3] _pyjl_callmethod(o::Ptr{PythonCall.C.PyObject}, args::Ptr{PythonCall.C.PyObject})
   @ PythonCall.C ~/.julia/packages/PythonCall/XgP8G/src/cpython/jlwrap.jl:47
(venv) dingraha@GRLRL2021060743 ~/p/p/test3 [1]>
cjdoris commented 2 years ago

Ah thanks yeah that's a bug. Easy to fix, but just use juliacall.Base for now.

cjdoris commented 2 years ago

Fixed on main, will be in the next release.

dingraha commented 2 years ago

Works great for me, thanks!