teradaasql Python模块只在编写脚本时工作,而在运行cod时不工作

2024-06-16 11:07:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我在使用teradasql包(从pypi安装)时遇到了一个特殊问题。我使用以下代码(我们称之为pytera.py公司)查询数据库:

from dotenv import load_dotenv
import pandas as pd
import teradatasql

# Load the database credentials from .env file
_ = load_dotenv()
db_host = os.getenv('db_host')
db_username = os.getenv('db_username')
db_password = os.getenv('db_password')


def run_query(query):
    """Run query string on teradata and return DataFrame."""
    if query.strip()[-1] != ';':
        query += ';'

    with teradatasql.connect(host=db_host, user=db_username,
                         password=db_password) as connect:
        df = pd.read_sql(query, connect)
    return df

当我在IPython/Python解释器或Jupyter笔记本中导入此函数时,我可以像这样运行查询:

^{pr2}$

但是,如果我将上面的代码保存在一个.py文件中并尝试运行它,大多数时候(不是所有的时间)都会收到一条错误消息。错误信息如下。在

E   teradatasql.OperationalError: [Version 16.20.0.49] [Session 0] [Teradata SQL Driver] Hostname lookup failed for None
E    at gosqldriver/teradatasql.(*teradataConnection).makeDriverError TeradataConnection.go:1046
E    at gosqldriver/teradatasql.(*Lookup).getAddresses CopDiscovery.go:65
E    at gosqldriver/teradatasql.discoverCops CopDiscovery.go:137
E    at gosqldriver/teradatasql.newTeradataConnection TeradataConnection.go:133
E    at gosqldriver/teradatasql.(*teradataDriver).Open TeradataDriver.go:32
E    at database/sql.dsnConnector.Connect sql.go:600
E    at database/sql.(*DB).conn sql.go:1103
E    at database/sql.(*DB).Conn sql.go:1619
E    at main.goCreateConnection goside.go:229
E    at main._cgoexpwrap_e6e101e164fa_goCreateConnection _cgo_gotypes.go:214
E    at runtime.call64 asm_amd64.s:574
E    at runtime.cgocallbackg1 cgocall.go:316
E    at runtime.cgocallbackg cgocall.go:194
E    at runtime.cgocallback_gofunc asm_amd64.s:826
E    at runtime.goexit asm_amd64.s:2361
E   Caused by lookup None on <ip address redacted>: server misbehaving

我在Ubuntu(WSL)18.04上使用python3.7.3和teradaasql16.20.0.49。在

也许并非巧合,我在Windows上尝试类似的工作流时遇到了类似的问题(使用teradata包和安装的teradata Python驱动程序)。当我在解释器内部或在Jupyter中连接时有效,但不是在脚本中。在Windows案例中,错误是:

E teradata.api.DatabaseError: (10380, '[08001] [Teradata][ODBC] (10380) Unable to establish connection with data source. Missing settings: {[DBCName]}')

我有一种感觉,我缺少一些基本的东西,但我在任何地方都找不到解决这个问题的办法。在


Tags: importhostgodbsqlospasswordquery
2条回答

看起来您的客户机找不到Teradata服务器,这就是您看到DBCName丢失错误的原因。这应该是Teradata服务器的“系统名”(即TDServProdA)。在

有几件事可以试试:

  1. {cd4}如果你试图用一个COP}标记直接连接到你的主机名。More info

  2. 尝试更新本地系统上的hosts文件。来自文档:

Modifying the hosts File

If your site does not use DNS, you must define the IP address and the Teradata Database name to use in the system hosts file on the computer.

  1. Locate the hosts file on the computer. This file is typically located in the following folder: %SystemRoot%\system32\drivers\etc
  2. Open the file with a text editor, such as Notepad.
  3. Add the following entry to the file: xxx.xx.xxx.xxx sssCOP1 where xxx.xx.xxx.xxx is the IP address and where sss is the Teradata Database name.

  4. Save the hosts file.

Link 1
Link 2

谢谢你的新鲜眼睛。原来问题是用dotenv在环境变量中加载的。我的模块在Python包(单独的文件夹)中,脚本和.env文件在工作目录中。在

dotenv成功地读取环境变量(.env在我的工作目录中),当我在我的原始post中,逐行,在解释器或Jupyter中运行代码时。但是,当我在脚本中运行相同的代码时,在我的工作目录中的.env文件中找不到它。那将是一个单独的问题,我必须找到答案。在

import teradatasql
import pandas as pd


def run_query(query, db_host, db_username, db_password):
    """Run query string on teradata and return DataFrame."""
    if query.strip()[-1] != ';':
        query += ';'

    with teradatasql.connect(host=db_host, user=db_username,
                         password=db_password) as connect:
        df = pd.read_sql(query, connect)
    return df

下面的代码现在可以在脚本中正常运行:

^{pr2}$

相关问题 更多 >