JuliaPy / PythonCall.jl

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

`UndefVarError` when using `include` in a new module #176

Closed dingraha closed 2 years ago

dingraha commented 2 years ago

I'm trying to include a Julia file from a Python script. Works fine when I'm using the Main module, but not when I use juliacall.newmodule to create a new module. Here's an example:

with open("include_test.jl", "w") as f:
    print("x = 8", file=f)
# from juliacall import Main as jl
import juliacall; jl = juliacall.newmodule("IncludeTest")
jl.include("include_test.jl")
print(f"jl.x = {jl.x}")

When I try to run that, I get this:

(venv) dingraha@GRLRL2021060743 ~/p/p/include_test> python include_test.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/dev/venv/julia_env
[juliapkg] Installing packages:
           julia> import Pkg
           julia> Pkg.add([Pkg.PackageSpec(name="PythonCall", uuid="6099a3de-0909-46bc-b1f4-468b9a2dfc0d"), Pkg.PackageSpec(name="OpenMDAOCore", uuid="24d19c10-6eee-420f-95df-4537264b2753"), Pkg.PackageSpec(name="LinearAlgebra", uuid="37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
)])
           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/dev/venv/julia_env/Project.toml`
  No Changes to `~/projects/pythoncall_openmdao/dev/venv/julia_env/Manifest.toml`
  No Changes to `~/projects/pythoncall_openmdao/dev/venv/julia_env/Project.toml`
  No Changes to `~/projects/pythoncall_openmdao/dev/venv/julia_env/Manifest.toml`
Traceback (most recent call last):
  File "/home/dingraha/projects/pythoncall_openmdao/include_test/include_test.py", line 5, in <module>
    jl.include("include_test.jl")
  File "/home/dingraha/.julia/packages/PythonCall/mw9sU/src/jlwrap/any.jl", line 190, in __getattr__
    return self._jl_callmethod($(pyjl_methodnum(pyjlany_getattr)), k)
AttributeError: Julia: UndefVarError: include not defined
(venv) dingraha@GRLRL2021060743 ~/p/p/include_test [1]>
cjdoris commented 2 years ago

Ahh include is no ordinary function. I've changed the way newmodule works internally and it works now. I've made a release.

dingraha commented 2 years ago

Works great, thank you!