我是一个 python 菜鸟,正在尝试运行 the KmeansRex library from github 中的示例.我使用的是 ubuntu 机器。这是一个没有 setup.py 文件的库。尽管进行了多次尝试,但我无法运行项目 github 页面的自述文件中显示的简短代码示例。
要尝试安装这个库,我会这样做:
git clone https://github.com/michaelchughes/KMeansRex.git
然后我这样做(根据 github 页面上的说明,只需将 .so 文件命名为 libkmeansrex64.so 而不是 libkmeansrex.so 因为我在 64 位机器上):
g++ --shared -o libkmeansrex64.so KMeansRexCore.cpp -I/home/path/to/eigen/ -O3 -DNDEBUG
/usr/bin/ld: /tmp/ccdmUbg9.o: relocation R_X86_64_32S against `.bss' can not be used when making a shared object; recompile with -fPIC
/tmp/ccdmUbg9.o: error adding symbols: Bad value
collect2: error: ld returned 1 exit status
我也是这样的:
g++ --shared -o libkmeansrex64.so KMeansRexCore.cpp -I/home/path/to/eigen/ -O3 -fpic -DNDEBUG
(并且编译没有错误)。然后我做:
cd KMeansRex/
然后我做:
python
我按照 github 上的说明进行示例:
Python 2.7.11+ (default, Apr 17 2016, 14:00:29)
[GCC 5.3.1 20160413] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy as np;
(到目前为止一切顺利)。但是,我会:
import KMeansRex
只为得到:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "KMeansRex/__init__.py", line 1, in <module>
from KMeansRex import RunKMeans
File "KMeansRex/KMeansRex.py", line 30, in <module>
lib = ctypes.cdll.LoadLibrary( os.path.join(parentdir,'libkmeansrex64.so') )
File "/usr/lib/python2.7/ctypes/__init__.py", line 443, in LoadLibrary
return self._dlltype(name)
File "/usr/lib/python2.7/ctypes/__init__.py", line 365, in __init__
self._handle = _dlopen(self._name, mode)
OSError: libkmeansrex64.so: cannot open shared object file: No such file or directory
这很奇怪,因为编译器确实生成了 libkmeansrex64.so:
/KMeansRex$ ls -n
total 84
drwxrwxr-x 2 1000 1000 4096 Mai 22 13:24 demonumpyctypes
drwxrwxr-x 3 1000 1000 4096 Mai 22 14:00 KMeansRex
-rw-rw-r-- 1 1000 1000 7003 Mai 22 13:24 KMeansRexCore.cpp
-rw-rw-r-- 1 1000 1000 635 Mai 22 13:24 KMeansRexCore.h
-rw-rw-r-- 1 1000 1000 2214 Mai 22 13:24 KMeansRex.cpp
-rwxrwxr-x 1 1000 1000 41792 Mai 22 13:24 libkmeansrex64.so
-rw-rw-r-- 1 1000 1000 1541 Mai 22 13:24 LICENSE
-rw-rw-r-- 1 1000 1000 7899 Mai 22 13:24 mersenneTwister2002.c
-rw-rw-r-- 1 1000 1000 1396 Mai 22 13:24 README
编辑:
问题已解决(感谢用户 kvorobiev)。所以在行之前
lib = ctypes.cdll.LoadLibrary( os.path.join(parentdir,'libkmeansrex64.so') )
我加了
print os.path.join(parentdir,'libkmeansrex64.so')
并且提示打印:
libkmeansrex64.so
所以很明显我应该替换
lib = ctypes.cdll.LoadLibrary( os.path.join(parentdir,'libkmeansrex64.so') )
通过:
lib = ctypes.cdll.LoadLibrary( os.path.join(parentdir,'/path/to/libkmeansrex64.so') )
这样做,一切正常;)
请您参考如下方法:
KMeansRex.py 中的路径有问题。
import os
...
curdir = os.path.split( __file__ )[0]
parentdir = os.path.split( curdir)[0]
...
在 Python3 中,这段代码可以正常工作,因为 __file__ 包含文件的绝对路径。但在 Python2 中,如果 you aren't inside the part of sys.path that contains the module, you'll get an absolute path. If you are inside the part of sys.path that contains the module, you'll get a relative path.
你可以使用
import os
...
curdir = os.path.split( os.path.abspath(__file__) )[0]
parentdir = os.path.split( curdir)[0]
...
或者只是硬编码你的库的绝对路径
lib = ctypes.cdll.LoadLibrary( '/abs/path/to/lib/libkmeansrex64.so' )
