我一直在尝试使用以下代码从Julia调用python函数:
using PyCall
unshift!(PyVector(pyimport("sys")["path"]), "")
@pyimport testing
print(testing.add())
测试是在同一条道路上。
测试python文件:
def add():
return 5 + 5
我从Julia那里得到了这个错误:
LoadError: PyError (ccall(@pysym(:PyImport_ImportModule), PyPtr, (Cstring,),
name)) <class 'ImportError'> ImportError("No module named 'testing'",)
while loading C:\Users\Benjamin\AppData\Local\JuliaPro-0.6.0.1\test.jl, in expression starting on line 410
pyerr_check at exception.jl:56 [inlined]
pyerr_check at exception.jl:61 [inlined]
macro expansion at exception.jl:81 [inlined]
pyimport(::String) at PyCall.jl:370
include_string(::String, ::String) at loading.jl:515
include_string(::Module, ::String, ::String) at Compat.jl:577
(::Atom.##55#58{String,String})() at eval.jl:73
withpath(::Atom.##55#58{String,String}, ::String) at utils.jl:30
withpath(::Function, ::String) at eval.jl:38
macro expansion at eval.jl:71 [inlined]
(::Atom.##54#57{Dict{String,Any}})() at task.jl:80
我该如何解决这个问题?
另外我该怎么做: (朱):
using PyCall
unshift!(PyVector(pyimport("sys")["path"]), "")
@pyimport testing
print(testing.add(5, 5))
(Python)的:
def add(a, b):
return a + b
答案 0 :(得分:1)
如果我将testing.py
文件重命名为其他内容(例如mytests.py
:
julia> using PyCall
julia> unshift!(PyVector(pyimport("sys")["path"]), "")
PyObject ['', … ]
julia> @pyimport mytests
julia> mytests.add(5,5)
10
我的site-packages文件夹中有一个testing
包,可能会造成麻烦:
julia> @pyimport testing
julia> testing.__path__
1-element Array{String,1}:
"/.../lib/python2.7/site-packages/testing"