python - 如何在 Julia 中使用 Python 函数?

标签 python julia

我想使用 this Python function在 Julia 。

这样,我使用了:

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"

但是什么都没有改变...

最佳答案

我找到了一个解决方案:

我通过将 /lib/ 添加到 ENV[PYTHONHOME] 来正确导入 python

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

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

现在,我可以使用这些功能了:

lay.batch_norm
PyObject <function batch_norm at 0x7f4738791938>

关于python - 如何在 Julia 中使用 Python 函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50736394/

相关文章:

Redis 在 julia 脚本中订阅不起作用

julia - 解决庞大的 Julia 解释器启动时间

python - 在升级到新版本之前我需要卸载 Python 包吗?

python - Tkinter 获取图像以跟随光标并进行先前的移动

python + linux + 不终止子进程问题

julia - 如何将可能为 `nothing` 的变量插入 Julia 中的字符串中?

plot - Julia 组按名称和总结计数

python - 如何将 python 站点迁移到另一台机器?

python - 乙状结肠运行时警告 : overflow encountered in exp

julia - 使用 Julia HDF5 以 numpy 轴顺序读取 HDF5 数据