问题描述
我是python的初学者。我已经在笔记本电脑上安装了python,版本为 python 2.7.18(v2.7.18:8d21aa21f2,2020年4月20日,13:19:08)[win32 MSC v.1500 32位(Intel)] 。我正在尝试导入其他人创建的现有框架,并使用现有包创建一个新的示例python文件。我写了一条简单的线:
if(TempData["UserId"] != null)
{
---
---
sqlcom.Parameters.Add("@UserID",sqlDbType.Int).Value = (int)TempData["UserId"];
---
}
pydblib是从SWIG自动生成的文件。下面的代码是从SWIG文件中获取的:
from machine_lib.pydblib import db
错误出现在except块:from sys import version_info as _swig_python_version_info
if _swig_python_version_info >= (2,7,0):
def swig_import_helper():
import importlib
pkg = __name__.rpartition('.')[0]
mname = '.'.join((pkg,'_pydblib')).lstrip('.')
try:
return importlib.import_module(mname)
except ImportError:
return importlib.import_module('_pydblib')
_pydblib = swig_import_helper()
上,其内容为:
return importlib.import_module('_pydblib')
我已经针对相同的错误回答了所有问题,但没有任何效果。如何摆脱这个错误?如果需要,我很乐意分享更多信息。预先感谢!
解决方法
如果您来这里搜索“python”、“导入错误”和“DLL 加载失败”的相关说明:在Python 3.8 DLL resolution under Windows has changed 中。另见what's new in Python 3.8:
Windows 上使用 ctypes 加载的扩展模块和 DLL 的 DLL 依赖项现在可以更安全地解析。仅搜索系统路径、包含 DLL 或 PYD 文件的目录以及使用 add_dll_directory() 添加的目录以查找加载时依赖项。具体来说,不再使用 PATH 和当前工作目录,对它们的修改将不再对正常的 DLL 解析产生任何影响。如果您的应用程序依赖于这些机制,您应该检查 add_dll_directory() 并且如果它存在,在加载您的库时使用它来添加您的 DLL 目录。请注意,Windows 7 用户需要确保已安装 Windows Update KB2533623(这也由安装程序验证)。
您还可以使用 ProcessMonitor 来检查 Python 正在寻找的 DLL 的名称。