如何在Julia中使用Python函数?

3
我可以为您进行翻译。以下是需要翻译的内容:

我希望在Julia中使用this Python function

因此,我使用了以下方式:

using PyCall
@pyimport tensorflow

但是出现了以下错误:
ERROR: PyError (ccall(@pysym(:PyImport_ImportModule), PyPtr, (Cstring,), name)

The Python package tensorflow could not be found by pyimport. Usually this means
that you did not install tensorflow in the Python version being used by PyCall.

PyCall is currently configured to use the Julia-specific Python distribution
installed by the Conda.jl package.  To install the tensorflow module, you can
use `pyimport_conda("tensorflow", PKG)`, where PKG is the Anaconda
package the contains the module tensorflow, or alternatively you can use the
Conda package directly (via `using Conda` followed by `Conda.add` etcetera).

Alternatively, if you want to use a different Python distribution on your
system, such as a system-wide Python (as opposed to the Julia-specific Python),
you can re-configure PyCall with that Python.   As explained in the PyCall
documentation, set ENV["PYTHON"] to the path/name of the python executable
you want to use, run Pkg.build("PyCall"), and re-launch Julia.

) <type 'exceptions.ImportError'>
ImportError('Traceback (most recent call last):\n  File "/home/jabou/.julia/v0.6/Conda/deps/usr/lib/python2.7/site-packages/tensorflow/python/pywrap_tensorflow.py", line 58, in <module>\n    from tensorflow.python.pywrap_tensorflow_internal import *\n  File "/home/jabou/.julia/v0.6/Conda/deps/usr/lib/python2.7/site-packages/tensorflow/python/pywrap_tensorflow_internal.py", line 28, in<module>\n    _pywrap_tensorflow_internal = swig_import_helper()\n  File "/home/jabou/.julia/v0.6/Conda/deps/usr/lib/python2.7/site-packages/tensorflow/python/pywrap_tensorflow_internal.py", line 24, in swig_import_helper\n    _mod = imp.load_module(\'_pywrap_tensorflow_internal\', fp, pathname, description)\nImportError: /home/jabou/.julia/v0.6/Conda/deps/usr/lib/python2.7/site-packages/tensorflow/python/_pywrap_tensorflow_internal.so: undefined symbol: _ZN10tensorflow10FileSystem16GetMatchingPathsERKSsPSt6vectorISsSaISsEE\n\n\nFailed to load the native TensorFlow runtime.\n\nSee https://www.tensorflow.org/install/install_sources#common_installation_problems\n\nfor some common reasons and solutions.  Include the entire stack trace\nabove this error message when asking for help.',)
  File "/home/jabou/.julia/v0.6/Conda/deps/usr/lib/python2.7/site-packages/tensorflow/__init__.py", line 24, in <module>
    from tensorflow.python import *
  File "/home/jabou/.julia/v0.6/Conda/deps/usr/lib/python2.7/site-packages/tensorflow/python/__init__.py", line 49, in <module>
    from tensorflow.python import pywrap_tensorflow
  File "/home/jabou/.julia/v0.6/Conda/deps/usr/lib/python2.7/site-packages/tensorflow/python/pywrap_tensorflow.py", line 72, in <module>
    raise ImportError(msg)

我测试了 PyCall 包,并获得了以下信息:

INFO: Testing PyCall
INFO: Python version 2.7.14 from /home/jabou/.julia/v0.6/Conda/deps/usr/lib/libpython2.7, PYTHONHOME=/home/jabou/.julia/v0.6/Conda/deps/usr:/home/jabou/.julia/v0.6/Conda/deps/usr
ENV[PYTHONPATH]=
ENV[PYTHONHOME]=
ENV[PYTHONEXECUTABLE]=
INFO: PyCall tests passed

此段话的英译中文如下:

此外,我定义了环境变量:

ENV["PYTHONPATH"] = "/home/jabou/.julia/v0.6/Conda/deps/usr/lib/libpython2.7"

ENV["PYTHONHOME"]= "/home/jabou/.julia/v0.6/Conda/deps/usr:/home/jabou/.julia/v0.6/Conda/deps/usr"

但是什么都没变...


1
ENV 字典的键应该是一个字符串,你正在尝试使用一个名为 PYTHONPATH 的未定义变量对其进行索引。请尝试 ENV["PYTHONPATH"] = "/home/.../libpython2.7" - fredrikekre
它对于 PYTHONPATH 有效,但我仍然遇到了相同的第一个错误... - Julien
难怪你不能在Julia中使用Python代码,除非声明所有路径并安装所需的依赖项。 - Gsk
当然,但我认为所有的依赖项都已经安装了...你知道我该如何安装它们,或者有没有一个命令可以查看依赖项? - Julien
顺便提一下,你可以在Julia中使用TensorFlow:https://github.com/malmaud/TensorFlow.jl - juliohm
为了确保,请尝试在您的Python版本2.7.14中直接运行“import tensorflow”?如果您也在那里遇到导入错误,则需要“pip install”tensorflow库。 - Sundar R
1个回答

0

我找到了解决方案:

我通过将/lib/添加到ENV[PYTHONHOME]中正确导入了Python。

之后,我使用以下命令导入了layers模块:

 @pyimport  tensorflow
 @pyimport tensorflow.contrib.layers as lay

现在,我可以使用这些函数:

lay.batch_norm
PyObject <function batch_norm at 0x7f4738791938>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接