安装/加载github :: KMeansRex Python库

时间:2016-05-22 12:12:12

标签: python python-2.7

我是一个python noob,试图在the KmeansRex library from github中运行示例。我用的是ubuntu机器。这是一个没有setup.py文件的库。尽管经过多次尝试,我还是无法运行项目的github页面的README文件中显示的短代码示例。

要尝试安装此库,请执行以下操作:

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') )

这样做,一切正常;)

1 个答案:

答案 0 :(得分:2)

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' )