简体   繁体   English

TA-Lib的Python包装:导入失败

[英]Python Wrapper for TA-Lib: Import Failure

I've come across the excellent piece of work of @mrjbq7 who wrote a Python wrapper for TA-Lib - an open source technical analysis library written in C. I've installed both TA-Lib (easy as the usual configure , make , make install ) as well as the wrapper (using the included setup.py script) per the instructions but I am receiving an import error when attempting to import the module or run the example script (same import error). 我遇到了@ mrjbq7的优秀作品,他为TA-Lib编写了一个Python 包装器 - 一个用C语言编写的开源技术分析库。我已经安装了TA-Lib(很容易像通常的configuremakemake install )以及包装器(使用包含的setup.py脚本),但是我在尝试导入模块或运行示例脚本时遇到导入错误(相同的导入错误)。

I'm on a Linux machine ( sys.platform == 'linux2' ) and using the Enthought distribution of Python 2.7. 我在Linux机器上( sys.platform == 'linux2' )并使用Python 2.7的Enthought发行版。

I've installed TA-Lib to its default location which is pointed to by the wrapper setuptools script (you can see the source of setup.py here ): 我已经将TA-Lib安装到它的默认位置,这是由包装器setuptools脚本指向的(你可以在这里看到setup.py的来源):

bash$ ls -la /usr/local/include/ta-lib/
total 240
drwxr-xr-x 2 root root   4096 2012-08-04 08:54 .
drwxr-xr-x 3 root root   4096 2012-08-04 08:54 ..
-rw-r--r-- 1 root root  19930 2012-08-04 08:54 ta_abstract.h
-rw-r--r-- 1 root root   4792 2012-08-04 08:54 ta_common.h
-rw-r--r-- 1 root root  12391 2012-08-04 08:54 ta_defs.h
-rw-r--r-- 1 root root 181115 2012-08-04 08:54 ta_func.h
-rw-r--r-- 1 root root   1849 2012-08-04 08:54 ta_libc.h

bash$ ls -la /usr/local/lib
total 4956
drwxr-xr-x  3 root root    4096 2012-08-04 08:54 .
drwxr-xr-x 12 root root    4096 2011-04-06 23:47 ..
-rw-r--r--  1 root root 3063180 2012-08-04 08:54 libta_lib.a
-rwxr-xr-x  1 root root     837 2012-08-04 08:54 libta_lib.la
lrwxrwxrwx  1 root root      18 2012-08-04 08:54 libta_lib.so -> libta_lib.so.0.0.0
lrwxrwxrwx  1 root root      18 2012-08-04 08:54 libta_lib.so.0 -> libta_lib.so.0.0.0
-rwxr-xr-x  1 root root 1983243 2012-08-04 08:54 libta_lib.so.0.0.0
drwxr-xr-x 11 root root    4096 2011-04-07 17:10 pythonEPD

Then I successfully build the Cython code using python setup.py install : 然后我使用python setup.py install成功构建了Cython代码:

bash$ python setup.py install
running install
running build
running build_ext
building 'talib' extension
gcc -pthread -fno-strict-aliasing -g -O2 -DNDEBUG -O2 -I/usr/local/lib/pythonEPD/include -fPIC -I/usr/local/lib/pythonEPD/lib/python2.7/site-packages/numpy/core/include -I/usr/local/include/ta-lib/ -I/usr/local/lib/pythonEPD/include/python2.7 -c talib.c -o build/temp.linux-i686-2.7/talib.o

...
[truncated - lots of warnings regarding pointer types]
---

gcc -pthread -shared -g -L/usr/local/lib/pythonEPD/lib build/temp.linux-i686-2.7/talib.o -L/usr/local/lib/ -L. -lta_lib -lpython2.7 -o build/lib.linux-i686-2.7/talib.so
running install_lib
copying build/lib.linux-i686-2.7/talib.so -> /usr/local/lib/pythonEPD/lib/python2.7/site-packages
running install_egg_info
Writing /usr/local/lib/pythonEPD/lib/python2.7/site-packages/TA_Lib-0.4.0-py2.7.egg-info

Yet when I attempt to run the example scripts or import talib, I get the following import eror: 然而,当我尝试运行示例脚本或导入talib时,我得到以下导入错误:

bash$ python
Enthought Python Distribution -- www.enthought.com
Version: 7.0-2 (32-bit)

Python 2.7.1 |EPD 7.0-2 (32-bit)| (r271:86832, Nov 29 2010, 13:52:51) 
[GCC 4.1.2 20080704 (Red Hat 4.1.2-44)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import talib
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: libta_lib.so.0: cannot open shared object file: No such file or directory

The setup.py script clearly includes the appropriate paths to the compiled TA-Lib code in the Extension class but I do notice that the gcc commands in the build of the Cython code do not include reference to the /usr/local/lib path... (not sure if this is the issue?). setup.py脚本清楚地包含Extension类中编译的TA-Lib代码的相应路径,但我注意到Cython代码构建中的gcc命令不包含对/usr/local/lib路径的引用。 ..(不确定这是否是问题?)。

I'm hoping someone has come across this with this module or a similar situation? 我希望有人遇到过这个模块或类似的情况吗?

Close @rcovre, I posted an issue on @mrjbq's github site and he responded with the very simple solution: 关闭@rcovre,我在@ mrjbq的github网站上发布了一个问题,他回答了一个非常简单的解决方案:

export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH

For the permanent solution, have to add /usr/local/lib to /etc/ld.so.conf as root then run /sbin/ldconfig (also as root). 对于永久解决方案,必须以root身份将/usr/local/lib添加到/etc/ld.so.conf然后运行/sbin/ldconfig (也作为root)。

Works like a charm. 奇迹般有效。

https://github.com/mrjbq7/ta-lib/issues/6 https://github.com/mrjbq7/ta-lib/issues/6

What about your python path? 你的python路径怎么样?

Maybe all the compiled libs may be stored in some directory of sys.path to be seen by your python interpreter. 也许所有已编译的库可能存储在sys.path的某个目录中,以供python解释器查看。 For example in '/usr/local/lib/python2.7/dist-packages', not just in '/usr/local/lib' (as in setup script). 例如在'/usr/local/lib/python2.7/dist-packages'中,而不仅仅是'/ usr / local / lib'(如在安装脚本中)。

你应该首先安装ta-lib ,python lib ta-lib基于这个c lib。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM