有人可以帮我理解这里发生了什么吗?我一直在尝试从 powershell 运行它,但无法让它工作。我有 vscode 并尝试在那里运行它以及更改 cwd 和解释器。相反,我只是得到错误回溯。我也尝试安装 oracleDB,但似乎是从 sqlalchemy 调用 cx_Oracle 并导致此错误
我的Coworker运行没有问题,并且输出了预期的结果
PS C:\Users\USERNAMEPLACEHOLDER\Downloads>python ib_newsletter_v2.py
Traceback (most recent call last):
File "C:\Users\USERNAMEPLACEHOLDER\Downloads\ib_newsletter_v2.py", line 15, in <module>
db = dl_db.DataLendDB('EQLVXRAIL-NJ-W2-ORCLDBPRD01.equilend.com', '1521', 'EQMRKT01', True)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\USERNAMEPLACEHOLDER\Downloads\dl_IB.py", line 25, in __init__
self.engine = create_engine(con_str)
^^^^^^^^^^^^^^^^^^^^^^
File "<string>", line 2, in create_engine
File "C:\Users\USERNAMEPLACEHOLDER\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\sqlalchemy\util\deprecations.py", line 281, in warned
return fn(*args, **kwargs) # type: ignore[no-any-return]
^^^^^^^^^^^^^^^^^^^
File "C:\Users\USERNAMEPLACEHOLDER\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\sqlalchemy\engine\create.py", line 599, in create_engine
dbapi = dbapi_meth(**dbapi_args)
^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\USERNAMEPLACEHOLDER\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\sqlalchemy\dialects\oracle\cx_oracle.py", line 1104, in import_dbapi
import cx_Oracle
ModuleNotFoundError: No module named 'cx_Oracle'
PS C:\Users\USERNAMEPLACEHOLDER\Downloads> pip install sqlalchemy
Requirement already satisfied: sqlalchemy in c:\users\USERNAMEPLACEHOLDER\appdata\local\packages\pythonsoftwarefoundation.python.3.11_qbz5n2kfra8p0\localcache\local-packages\python311\site-packages (2.0.32)
Requirement already satisfied: typing-extensions>=4.6.0 in c:\users\USERNAMEPLACEHOLDER\appdata\local\packages\pythonsoftwarefoundation.python.3.11_qbz5n2kfra8p0\localcache\local-packages\python311\site-packages (from sqlalchemy) (4.12.2)
Requirement already satisfied: greenlet!=0.4.17 in c:\users\USERNAMEPLACEHOLDER\appdata\local\packages\pythonsoftwarefoundation.python.3.11_qbz5n2kfra8p0\localcache\local-packages\python311\site-packages (from sqlalchemy) (3.0.3)
PS C:\Users\USERNAMEPLACEHOLDER\Downloads>
肯定会使用 python-oracledb 而不是实际上已经过时的旧 cx_Oracle 驱动程序。
如果您有旧的 SQLAlchemy 1.4,请执行以下操作:
# Using python-oracledb in SQLAlchemy 1.4
import os
import oracledb
from sqlalchemy import create_engine
from sqlalchemy import text
# These three lines let SQLAchemy 1.4 use python-oracledb
import sys
oracledb.version = "8.3.0"
sys.modules["cx_Oracle"] = oracledb
# Get credentials
un = os.environ.get("PYTHON_USERNAME")
pw = os.environ.get("PYTHON_PASSWORD")
cs = os.environ.get("PYTHON_CONNECTSTRING")
engine = create_engine(f'oracle://@',
connect_args={
"user": un,
"password": pw,
"dsn": cs
}
)
with engine.connect() as conn:
print(conn.scalar(text(
"""SELECT UNIQUE CLIENT_DRIVER
FROM V$SESSION_CONNECT_INFO
WHERE SID = SYS_CONTEXT('USERENV', 'SID')""")))
如果您有 SQLAlchemy 2,请执行以下操作:
# Using python-oracledb in SQLAlchemy 2
import os
import oracledb
from sqlalchemy import create_engine
from sqlalchemy import text
# Get credentials
un = os.environ.get("PYTHON_USERNAME")
pw = os.environ.get("PYTHON_PASSWORD")
cs = os.environ.get("PYTHON_CONNECTSTRING")
# Note the first arg is different than for SQLAlchemy 1.4
engine = create_engine(f'oracle+oracledb://@',
connect_args={
"user": un,
"password": pw,
"dsn": cs
}
)
with engine.connect() as conn:
print(conn.scalar(text(
"""SELECT UNIQUE CLIENT_DRIVER
FROM V$SESSION_CONNECT_INFO
WHERE SID = SYS_CONTEXT('USERENV', 'SID')""")))
注意
create_engine()
参数对于不同的 SQLAlchemy 版本是不同的。
另请参阅博客: