ImportError when calling a Julia module function from PyJulia
Asked Answered
Y

1

7

I'm trying to include a Julia function (PowerModelsDistribution.solve_mc_opf) in some Python code. The package works in Julia but I'm getting stuck on including it with PyJulia. I've tried a few things:

1.

from julia import Pkg
Pkg.activate("C:/Users/Aisling/.julia/environments/pmd") # pmd is my Julia env but I've also tried this in my base
from julia.PowerModelsDistribution import solve_mc_opf

Results in

ImportError                               Traceback (most recent call last)
<ipython-input-33-6e83bbbb609c> in <module>
----> 1 from julia.PowerModelsDistribution import solve_mc_opf

C:\ProgramData\Anaconda3\lib\site-packages\julia\core.py in load_module(self, fullname)
    258                                               JuliaModule(self, fullname))
    259 
--> 260         raise ImportError("{} not found".format(juliapath))
    261 
    262 

ImportError: PowerModelsDistribution.solve_mc_opf not found
  1. And then I tried:
import julia
jl = julia.Julia()
res = jl.run(PowerModelsDistribution.solve_mc_opf("documents/powermodelsdistribution/test/data/opendss/case3_unbalanced.dss", ACPUPowerModel, Ipopt.Optimizer))

Results in:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-29-2af5367df821> in <module>
----> 1 res = jl.run(PowerModelsDistribution.solve_mc_opf("documents/powermodelsdistribution/test/data/opendss/case3_unbalanced.dss", ACPUPowerModel, Ipopt.Optimizer))

C:\ProgramData\Anaconda3\lib\site-packages\julia\core.py in __getattr__(self, name)
    174     def __getattr__(self, name):
    175         try:
--> 176             return self.__try_getattr(name)
    177         except AttributeError:
    178             if name.endswith("_b"):

C:\ProgramData\Anaconda3\lib\site-packages\julia\core.py in __try_getattr(self, name)
    197             return self._julia.eval(jl_fullname)
    198 
--> 199         raise AttributeError(name)
    200 
    201 

AttributeError: solve_mc_opf

I'm stumped.

Yetty answered 13/7, 2022 at 14:57 Comment(0)
T
2

I think what you want to do is:

from julia import PowerModelsDistribution
PowerModelsDistribution.solve_mc_opf("...")

So you should import a Julia module to Python not function.

Transship answered 13/7, 2022 at 16:56 Comment(1)
thanks for the reply! I actually think the issue is that the exports for the PowerModelsDistribution lib are messed up. I can call dir(PowerModelsDistribution) after from julia import PowerModelsDistribution (returns no errors). The devs on PMD said it might be because they loop the export call (`for sym in names... @eval export $sym).Yetty

© 2022 - 2024 — McMap. All rights reserved.