我正在使用 IronPython 和 clr 模块通过 SMO 检索 SQL Server 信息。我想使用 SQL Alchemy 在 SQL Server 数据库中检索/存储这些数据,但在加载 pyodbc 模块时遇到了一些问题。
这是设置:
- IronPython 2.6.1(安装在 D:\Program Files\IronPython)
- CPython 2.6.5(安装在 D:\Python26)
- SQL Alchemy 0.6.1(安装在 D:\Python26\Lib\site-packages\sqlalchemy)
- pyodbc 2.1.7(安装在 D:\Python26\Lib\site-packages)
我在 IronPython site.py 中有这些条目来导入 CPython 标准和第三方库:
# Add CPython standard libs and DLLs
import sys
sys.path.append(r"D:\Python26\Lib")
sys.path.append(r"D:\Python26\DLLs")
sys.path.append(r"D:\Python26\lib-tk")
sys.path.append(r"D:\Python26")
# Add CPython third-party libs
sys.path.append(r"D:\Python26\Lib\site-packages")
# sqlite3
sys.path.append(r"D:\Python26\Lib\sqlite3")
# Add SQL Server SMO
sys.path.append(r"D:\Program Files\Microsoft SQL Server\100\SDK\Assemblies")
import clr
clr.AddReferenceToFile('Microsoft.SqlServer.Smo.dll')
clr.AddReferenceToFile('Microsoft.SqlServer.SqlEnum.dll')
clr.AddReferenceToFile('Microsoft.SqlServer.ConnectionInfo.dll')
SQL Alchemy 在 IronPython 中导入 OK,当我尝试连接到 SQL Server 时收到此错误消息:
IronPython 2.6.1 (2.6.10920.0) on .NET 2.0.50727.3607
Type "help", "copyright", "credits" or "license" for more information.
>>> import sqlalchemy
>>> e = sqlalchemy.MetaData("mssql://")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "D:\Python26\Lib\site-packages\sqlalchemy\schema.py", line 1780, in __init__
File "D:\Python26\Lib\site-packages\sqlalchemy\schema.py", line 1828, in _bind_to
File "D:\Python26\Lib\site-packages\sqlalchemy\engine\__init__.py", line 241, in create_engine
File "D:\Python26\Lib\site-packages\sqlalchemy\engine\strategies.py", line 60, in create
File "D:\Python26\Lib\site-packages\sqlalchemy\connectors\pyodbc.py", line 29, in dbapi
ImportError: No module named pyodbc
这段代码在 CPython 中运行良好,但看起来 pyodbc 模块无法从 IronPython 访问。
有什么建议么?我意识到这可能不是解决问题的最佳方法,所以我愿意以不同的方式解决这个问题。只是想获得一些使用 SQL Alchemy 和 pyodbc 的经验。