【问题标题】:Installing/loading github::KMeansRex Python library安装/加载 github::KMeansRex Python 库
【发布时间】:2016-05-22 12:12:11
【问题描述】:

我是一个 python 菜鸟,正在尝试运行 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') )

这样做,一切正常;)

【问题讨论】:

  • @renemilk:我不明白。您能否更好地解释如何更改dlopen 调用?
  • 只需打开KMeansRex.py 文件并在print os.path.join(parentdir,'libkmeansrex64.so') 之前插入print os.path.join(parentdir,'libkmeansrex64.so') 行并将结果添加到您的问题中
  • @kvorobiev:这解决了问题!我应该删除这个问题吗?
  • 我的意思是我用 `lib = ctypes.cdll.LoadLibrary(os.path .join(parentdir,'/path/to/libkmeansrex64.so') )`
  • 我添加了完整的答案和解释。如果有帮助可以标记为正确答案)

标签: python python-2.7


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2020-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-12
    • 1970-01-01
    相关资源
    最近更新 更多