在我的 Python 2.7 项目中,我需要安装 ta-lib 库。在目标环境中,我没有 root 或 sudo 权限,因此 Python 应用程序在虚拟环境中运行。
由于未知原因,当 libta_lib.so 驻留在用户目录结构(而不是系统的 /usr 文件夹)中时,使用 pip 安装 ta-lib 库失败。
我做了什么:
我正在使用 Python 2.7 创建一个全新的虚拟环境:
ec2-user:~/environment $ python -V
Python 2.7.14
ec2-user:~/environment $ virtualenv -p /usr/bin/python27 my_env
Running virtualenv with interpreter /usr/bin/python27
New python executable in /home/ec2-user/environment/my_env/bin/python27
Also creating executable in /home/ec2-user/environment/my_env/bin/python
Installing setuptools, pip, wheel...done.
ec2-user:~/environment $ source my_env/bin/activate
(my_env) ec2-user:~/environment $
在 my_env 中,我下载并解压最新的ta-lib。我使用前缀标志将安装路径配置为在虚拟环境中。
(my_env) ec2-user:~/environment/my_env $ ./configure --prefix=/home/ec2-user/environment/my_env
(my_env) ec2-user:~/environment/my_env $ make
(my_env) ec2-user:~/environment/my_env $ make install
配置、制作和安装工作都很好。最后系统告诉我类似:
Libraries have been installed in:
/home/ec2-user/environment/my_env/lib
If you ever happen to want to link against installed libraries
in a given directory, LIBDIR, you must either use libtool, and
specify the full pathname of the library, or use the `-LLIBDIR'
flag during linking and do at least one of the following:
- add LIBDIR to the `LD_LIBRARY_PATH' environment variable
during execution
- add LIBDIR to the `LD_RUN_PATH' environment variable
during linking
[...]
所以我将 /home/ec2-user/environment/my_env/lib 添加到 $LD_LIBRARY_PATH 和 $LD_RUN_PATH。到目前为止一切看起来都很好,但是当我
(my_env) ec2-user:~/environment/my_env $ pip install ta-lib
Collecting ta-lib
Using cached https://files.pythonhosted.org/packages/[...]/
TA-Lib-0.4.17.tar.gz
Requirement already satisfied: numpy in ./lib/python2.7/dist-packages
(from ta-lib) (1.15.1)
Building wheels for collected packages: ta-lib
Running setup.py bdist_wheel for ta-lib ... error
Complete output from command /home/ec2-user/environment/my_env/bin/python27
-u -c "import setuptools, tokenize;__file__='/tmp/pip-install-MD3Ds7/ta-
lib/setup.py';f=getattr(tokenize, 'open', open)
(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code,
__file__, 'exec'))" bdist_wheel -d /tmp/pip-wheel-dtdhyb --python-tag cp27:
/tmp/pip-install-MD3Ds7/ta-lib/setup.py:79: UserWarning: Cannot find ta-lib
library, installation may fail.
warnings.warn('Cannot find ta-lib library, installation may fail.')
和 ...
creating build/temp.linux-x86_64-2.7/talib
gcc -pthread -fno-strict-aliasing -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic -D_GNU_SOURCE -fPIC -fwrapv -DNDEBUG -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic -D_GNU_SOURCE -fPIC -fwrapv -fPIC -I/home/ec2-user/environment/my_env/local/lib/python2.7/dist-packages/numpy/core/include -I/usr/include -I/usr/local/include -I/opt/include -I/opt/local/include -I/usr/include/python2.7 -c talib/_ta_lib.c -o build/temp.linux-x86_64-2.7/talib/_ta_lib.o
talib/_ta_lib.c:526:28: fatal error: ta-lib/ta_defs.h: No such file or directory
#include "ta-lib/ta_defs.h"
^
compilation terminated.
error: command 'gcc' failed with exit status 1
----------------------------------------
Command "/home/ec2-user/environment/my_env/bin/python27 -u -c "import setuptools, tokenize;__file__='/tmp/pip-install-MD3Ds7/ta-lib/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record /tmp/pip-record-YqZ5hO/install-record.txt --single-version-externally-managed --compile --install-headers /home/ec2-user/environment/my_env/include/site/python2.7/ta-lib" failed with error code 1 in /tmp/pip-install-MD3Ds7/ta-lib/
[...]
我还尝试将“/home/ec2-user/environment/my_env/lib”添加到 $PATH,但结果是一样的。
为什么 pip 找不到位于虚拟环境中 lib 文件夹中的库?
当我将 ta-lib 二进制文件安装到默认的 /usr/lib 文件夹中时(当然使用 sudo), pip install ta-lib 会找到它并安装得很好。不幸的是,这不是目标系统的选项。
我做错了什么,还是 pip 忽略了虚拟环境中的文件夹和路径变量?
问候, 啤酒