Skip to main content
 首页 » 编程设计

python之模块属性导入

2024年11月24日8dudu

我不确定为什么我不能导入 db_password 变量。

运行 db_setup 得到:

Traceback (most recent call last): 
  File "db_setup.py", line 5, in <module> 
    passwd=dbconfig.db_password) 
TypeError: 'module' object is not callable 

我遗漏了一些东西,但我不确定是什么。

我可以打开一个 interperter 并导入 dbconfig 以及调用 `dbconfig.db_password'

db_setup.py

import pymysql 
import dbconfig 
 
connection = pymysql.connections(host='localhost', user=dbconfig.db_user, passwd=dbconfig.db_password) 

dbconfig.py

db_user='username' 
db_password='password' 

我希望模块传递变量'password'

请您参考如下方法:

db_password变量导入没有问题,错误在别处:

import pymysql 
import dbconfig 
 
connection = pymysql.connect(host='localhost', user=dbconfig.db_user, password=dbconfig.db_password) 

pymysql.connections 是一个模块,而不是一个函数,它包含 Connection 类,该类由 pymysql.connect() 实例化并返回>

Connection class doc :

The proper way to get an instance of this class is to call connect().