如何在Julia中使用Python函数?

时间:2018-06-07 08:27:49

标签: python julia

我想在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 个答案:

答案 0 :(得分:0)

我找到了解决方案:

我通过将/lib/添加到ENV[PYTHONHOME]

来正确导入python

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

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

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

lay.batch_norm
PyObject <function batch_norm at 0x7f4738791938>