[英]ammend sys.path but still can't find a module
I'm working with an API which has the following code it it: 我正在使用具有以下代码的API:
# aardvark_py.py
import os
import sys
try:
import aardvark as api
except ImportError, ex1:
import imp, platform
ext = platform.system() in ('Windows', 'Microsoft') and '.dll' or '.so'
try:
api = imp.load_dynamic('aardvark', 'aardvark' + ext)
except ImportError, ex2:
import_err_msg = 'Error importing aardvark%s\n' % ext
import_err_msg += ' Architecture of aardvark%s may be wrong\n' % ext
import_err_msg += '%s\n%s' % (ex1, ex2)
raise ImportError(import_err_msg)
Needless to say that I can't modify this code. 不用说我不能修改此代码。
My problem is in this line of code: 我的问题是在这一行代码中:
api = imp.load_dynamic('aardvark', 'aardvark' + ext)
The second argument species a path to the .dll
, ie it is set to aarvark.dll
, which is in the same location where aardvark_py.py
is located. 第二个参数指定
.dll
的路径,即将其设置为aarvark.dll
,该路径与aardvark_py.py
所在的位置相同。 I import this module, aardvark_py
into my code. 我将此模块
aardvark_py
导入我的代码中。 My code is not located in the same location as aardvark_py.py
and aardvark.dll
. 我的代码与
aardvark_py.py
和aardvark.dll
位于不同的位置。 However when I do an import it fails. 但是,当我导入时失败。 It raises an exception.
它引发一个异常。 It looks like it doesn't know where
.dll
is located, but I can't figure out why. 看起来它不知道
.dll
的位置,但是我不知道为什么。 Here is the error I'm getting 这是我遇到的错误
"path\to\aardvark_py.py", line 71, in <module>
raise ImportError(import_err_msg)
ImportError: Error importing aardvark.dll
Architecture of aardvark.dll may be wrong
No module named aardvark
DLL load failed: The specified module could not be found.
Here is approximate directory layout: 这是大概的目录布局:
project_dir -
|
|- 3rdparty
| |
| |-aardvark
| |
| |-aarvark_py.py
| |-aardvark.dll
|
|
|-tools
|
|-mycode.py
mycode.py
does contain code to ammend sys.path
mycode.py
确实包含用于扩展sys.path
代码
sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__))), '..', '3rdparty', 'aardvark')
I had this same problem in 32-bit python 2.7. 我在32位python 2.7中遇到了同样的问题。 I installed 64-bit python 2.7 and now it works.
我安装了64位python 2.7,现在可以使用了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.