我是python编程的新手。我有python A文件和python B文件。我知道循环导入效果不好,但就我而言,B文件包含我稍后在文件A中使用的密码。文件B中的密码基于文件A中列出的主机,因此进行了循环导入。我得到属性错误。我曾尝试在需要时导入模块之类的解决方案,但仍无法解决。这是代码段。
#File A
Import argparse, psycopg2, File B...
.
.
.
database = 'postgres'
host = host #This is passed as an argument earlier
port = 5432
user = 'user'
password = file_B.p #I know I can use .pgpass but I am printing password later in the code, hence need to have it in the file instead of using .pgpass
def get_all_db(): """Get all databases from a cluster"""
conn = psycopg2.connect(user=user,
password= password,
host= host,
port= port,
database= database)
....Code Continue
#File B
def get_host_names():
import file A
if file_A.host == 'Host 1':
p = 'Password 1'
elif file_A.host == 'Host 2':
p = 'Password 2'
elif file_A.host == 'Host 3':
p = 'Password 3'
else:
print('this host is not supported')
我收到以下错误;
AttributeError: module 'file B' has no attribute 'p'
我该如何解决?预先感谢。
P.S。我想达到什么目的?我在运行文件A时传递主机名,基于作为参数传递的主机名,密码是从文件B中获取的,以后将在文件A中使用。