简体   繁体   English

推荐sys.path但仍然找不到模块

[英]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.pyaardvark.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.

相关问题 在sys.path中存在的本地目录中找不到模块 - Can't find module in local dir, present in sys.path 当父目录位于 sys.path 中时,Python 无法始终找到模块 - Python can't consistently find a module while the parent directory is in sys.path 无法重新加载另一个文件夹中的模块,也无法重新加载sys.path中的模块 - Can't reload module that is in another folder but also in sys.path 为什么鼻子测试不能在sys.path中找到一个元素? - Why can't nosetests find one the elements in sys.path? 无法导入 Python 模块,因为“sys.path”和“sys.executable”存在差异 - Can't import Python module because Difference in “sys.path” and “sys.executable” 无法在Spyder中导入模块,不确定如何使用sys.path - Can't import module in Spyder, not sure how to use sys.path Pyinstaller - ImportError:模块'pythoncom'不在冻结的sys.path中 - Pyinstaller - ImportError: Module 'pythoncom' isn't in frozen sys.path 如何导入不在 sys.path 中的模块? - How can I import a module that is not in sys.path? 为什么 Python 找不到 sys.path 目录中的共享对象? - Why can't Python find shared objects that are in directories in sys.path? ImportError:模块“pythoncom”不在冻结的 sys.path 中 - ImportError: Module 'pythoncom' isn't in frozen sys.path
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM