【问题标题】:PyCall builds but nothing worksPyCall 构建但没有任何效果
【发布时间】:2019-07-18 22:25:07
【问题描述】:

在构建和预编译 PyCall 之后,我尝试了一些 example code 来验证它的功能。不幸的是,我马上就遇到了错误。

julia> import Pkg

julia> Pkg.build("PyCall")
  Building Conda ─→ `C:\Users\Student\.julia\packages\Conda\kLXeC\deps\build.log`
  Building PyCall → `C:\Users\Student\.julia\packages\PyCall\ttONZ\deps\build.log`

julia> import PyCall

julia> math = pyimport("math")
ERROR: UndefVarError: pyimport not defined
Stacktrace:
 [1] top-level scope at none:0

julia> py"""
       import math
       math.sin(math.pi / 4)
       """
ERROR: LoadError: UndefVarError: @py_str not defined
in expression starting at REPL[5]:1

julia> py"import math"
ERROR: LoadError: UndefVarError: @py_str not defined
in expression starting at REPL[6]:1

我在 Windows 10 x64 上使用 Julia 1.1.1 (2019-05-16)。我在网上没有看到任何关于这个特定问题的信息,现在对此感到沮丧。

【问题讨论】:

  • 你试过using PyCall而不是import PyCall吗?

标签: python julia pycall


【解决方案1】:

由于你是importing PyCall,你必须通过在pyimport()函数前面加上模块名称本身来指定它的范围,即

julia> import PyCall

julia> math = PyCall.pyimport("math")
PyObject <module 'math' from '/usr/lib/python3.7/lib-dynload/math.cpython-37m-x86_64-linux-gnu.so'>

julia> math.sin(math.pi/4)
0.7071067811865475

或者,正如 Gomiero 在讨论中提到的那样,您的问题也可以通过使用 using PyCall 将函数带入本地范围来解决;

julia> using PyCall

julia> math = pyimport("math")
PyObject <module 'math' from '/usr/lib/python3.7/lib-dynload/math.cpython-37m-x86_64-linux-gnu.so'>

julia> math.sin(math.pi/4)
0.7071067811865475

Julia documentation 中有更多相关内容。

【讨论】:

  • 回到你正在学习的语言的那一刻,它让你得到了简单的东西......感谢你指出明显的缺陷。
猜你喜欢
  • 2022-07-18
  • 2012-12-15
  • 2022-11-23
  • 1970-01-01
  • 2018-06-14
  • 2019-06-19
  • 2017-07-18
  • 2010-10-17
  • 2012-11-25
相关资源
最近更新 更多