我有一个包含good.py模块的hello1包.
hello1
??? __init__.py
??? good.py
Run Code Online (Sandbox Code Playgroud)
该初始化模块有一个变量A = 1,我需要访问的变量hello1.A在good.py.
import hello1
class Good(object):
def __init__(self):
print hello1.A
if __name__ == "__main__":
g = Good()
Run Code Online (Sandbox Code Playgroud)
问题是,当我执行python脚本时,我收到了ImportError: 'No module named hello1'错误.我可以import sys; sys.path.append("..")在第一行添加good.py快速修复.
但是,good.py位于hello1包中__init__.py也在其中,所以我想知道是否有办法从同一个包中的模块访问__init__.py中的变量.
导入__init__似乎工作正常.
import __init__
class Good(object):
def __init__(self):
print hello1.A
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10884 次 |
| 最近记录: |