包可以访问从中导入的范围吗?

时间:2018-01-30 14:32:23

标签: python

考虑一下

# file1.py
file1_variable = "something"
import file2

# file2.py
print(file1_variable)

这可能吗?如果是,怎么做?

显然globalslocals file2.py不包含file1.py个变量

高级示例我的目标是:

文件结构:

- file1.py
- my_package/
  - __init__.py

# file1.py
file1_variable = "something"
import my_package

# my_package/__init__.py
# Do something to get all variables from file1.py, I dont know what
print(locals()) # Should print file1_variable among others

0 个答案:

没有答案