PyJulia 构建失败;导入期间在当前路径中找不到包 'Pkg'?

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

我正在尝试在我的系统上构建 PyJulia,但在尝试通过 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.

当我通过 Julia 安装时,我确保将构建安装指向正确的 Python 解释器和 libpython,但是,当我尝试在 Python 中进行简单调用时,它说这些变量尚未定义。

>>> 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

问题已发布在 github 此处:https://github.com/JuliaPy/pyjulia/issues/382

我猜 PyJulia 找不到您想要的 Julia 环境。我过去也很难让 PyJulia 工作。这对我有用:

pip install julia

然后在 python 文件或 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)