[英]How do I get list from imported python file
This was my python file, and I want to move “names” list to other file, and import it.这是我的 python 文件,我想将“名称”列表移动到其他文件,并导入它。
name = [“one”,
“two”,
“three”]
for doing_job in name:
(doing something)
I have imported sucsessfully, but I can't get that “name” list the main python file.我已成功导入,但我无法获取主 python 文件的“名称”列表。
made like this做成这样
main.py主文件
from settings import name
for doing_job in name
settings/name.py设置/名称.py
name = [“one”,
“two”,
“three”]
return name
How can I fix this and get “name” on the main.py file?我该如何解决这个问题并在 main.py 文件中获取“名称”?
Error now is现在的错误是
for doing_job in name:
NameError: name 'name' is not defined
You can get the name variable by calling name.name
.您可以通过调用name.name
来获取 name 变量。 Check the example below:检查下面的例子:
Main.py file:主.py文件:
from settings import name
for n in name.name:
print(n)
settings/name.py file:设置/名称.py 文件:
name = [["one","two","three"]]
Everything is ok unless the curly quotes use straight ones, also it seeems you are missing a ending bracket and why a "useless" return outside function?一切都很好,除非卷曲引号使用直引号,而且似乎您缺少结束括号以及为什么在函数外部返回“无用”?
name = [['one',
'two',
'three']]
You can use double ""
too您也可以使用双""
main.py主文件
from settings import name
for doing_job in name.name:
print(doing_job)
or或者
from settings.name import name
for doing_job in name:
print(doing_job)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.