pythonjuliapycall

PyJulia build fails; can't find package 'Pkg' in current path during import?


I am trying to build PyJulia on my system but get the following error when trying to install via Python:

>>> import julia
>>> julia.install()
ERROR: LoadError: ArgumentError: Package Pkg not found in current path:
- Run `import Pkg; Pkg.add("Pkg")` to install the Pkg package.

Stacktrace:
 [1] require(::Module, ::Symbol) at .\loading.jl:823
 [2] include at .\boot.jl:326 [inlined]
 [3] include_relative(::Module, ::String) at .\loading.jl:1038
 [4] include(::Module, ::String) at .\sysimg.jl:29
 [5] exec_options(::Base.JLOptions) at .\client.jl:267
 [6] _start() at .\client.jl:436
in expression starting at C:\Users\wgorman\AppData\Local\Continuum\anaconda3\lib\site-packages\julia\install.jl:12
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\wgorman\AppData\Local\Continuum\anaconda3\lib\site-packages\julia\tools.py", line 102, in install
    raise PyCallInstallError("Installing", output)
julia.tools.PyCallInstallError: Installing PyCall failed.

When I install via Julia, I make sure to point the build installation to the correct Python interpreter and libpython, however, when I try to make a simple call within Python it says that those variables haven't been defined.

>>> import julia
>>> from julia import Main
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 668, in _load_unlocked
  File "<frozen importlib._bootstrap>", line 638, in _load_backward_compatible
  File "C:\Users\wgorman\AppData\Local\Continuum\anaconda3\lib\site-packages\julia\core.py", line 246, in load_module
    JuliaMainModule(self, fullname))
  File "C:\Users\wgorman\AppData\Local\Continuum\anaconda3\lib\site-packages\julia\core.py", line 148, in __init__
    self._julia = loader.julia
  File "C:\Users\wgorman\AppData\Local\Continuum\anaconda3\lib\site-packages\julia\core.py", line 238, in julia
    self.__class__.julia = julia = Julia()
  File "C:\Users\wgorman\AppData\Local\Continuum\anaconda3\lib\site-packages\julia\core.py", line 482, in __init__
    raise UnsupportedPythonError(jlinfo)
julia.core.UnsupportedPythonError: It seems your Julia and PyJulia setup are not supported.

Julia executable:
    julia
Python interpreter and libpython used by PyCall.jl:
    None
    None
Python interpreter used to import PyJulia and its libpython.
    C:\Users\wgorman\AppData\Local\Continuum\anaconda3\python.exe
    C:\Users\wgorman\AppData\Local\Continuum\anaconda3\python37.dll

julia> versioninfo()
Julia Version 1.0.5
Commit 3af96bcefc (2019-09-09 19:06 UTC)
Platform Info:
  OS: Windows (x86_64-w64-mingw32)
  CPU: Intel(R) Core(TM) i7-2600 CPU @ 3.40GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-6.0.0 (ORCJIT, sandybridge)
Python 3.7.3 (default, Mar 27 2019, 17:13:21) [MSC v.1915 64 bit (AMD64)]
Windows-10-10.0.18362-SP0
8 logical CPU cores, Intel64 Family 6 Model 42 Stepping 7, GenuineIntel

Issue posted on github here: https://github.com/JuliaPy/pyjulia/issues/382


Solution

  • I'm guessing PyJulia can't find your desired Julia environment. I too have had difficulties with getting PyJulia to work in the past. This was what worked for me:

    pip install julia

    Then in a python file or Jupyter notebook:

    # Import Julia
    from julia.api import Julia
    jpath = "julia-1.3.1/bin/julia" # path to Julia, from current directory (your path may be slightly different)
    jl = Julia(runtime=jpath, compiled_modules=False) # compiled_modules=True may work for you; it didn't for me
    
    
    # Import Julia Modules
    from julia import Main
    Main.include("path/to/some_julia_file.jl")
    jl.eval("using .some_julia_module") # use a module from some_julia_file.jl
    
    
    # Evaluate Stuff
    this_equals_three = jl.eval("1+2")
    
    # eval works with Python f-strings, so you can easily use python variables
    a = 1
    b = 2
    also_three = jl.eval(f"{a}+{b}")
    
    result = jl.eval(f"myfunc({a},{b})") # call custom functions imported from some_julia_file.jl
    
    from julia import Base
    equals_one = Base.sind(90)