Python - 将全局变量作为参数传递给导入的模块

时间:2017-01-01 01:22:05

标签: python python-module

为了运行程序playlist.py,我有一个简单测试界面的脚本interface.py,其代码如下:

seeds = {}

user_input1 = raw_input("Please pick a seed: track, artist or genre. >> ")

user_input2 = raw_input("which one? >> ")

if user_input1 == 'track':
    seeds['track'] = user_input2
elif user_input1 == 'artist':
    seeds['artist']= user_input2
else:
    seeds['genre'] = user_input2

playlist.pyinterface.py位于同一目录,我此时导入前者:

if __name__ == "__main__":
    from playlist import *

playlist.py我定义了我的类,并且应该像这样传递参数seeds

playlilst.__init__(self, **seeds)

但是当我运行interface.py时,我收到以下错误:

playlist.__init__(self, **seeds) NameError: global name 'seeds' is not defined

playlist.py是否以正确的方式导入?我在这做错了什么?

1 个答案:

答案 0 :(得分:0)

在playlist.py中,

from interface import seeds