case1:以下是文件结构在同一文件夹中:
`Folder1
|__init__.py
|gnewsclient.py
|test.py
|utils.py`
1)Content of
__init__.py
`from .gnewsclient import gnewsclient`
2) Content of client.py
`class gnewsclient:
//Some methods
`
3)Content of `utils.py`
Some Dictionaries inside utils.py
4)Content of `test.py`:
Here I want to import methods from client.py which has gnewsclient class()
Now I want to import methods from gnewsclient class of `client.py` file inside test.py
All are in same folder above
In `test.py`:
I tried
`from client import *`
or
`from .client import gnewsclient`
但它说未加载的父模块无法执行相对导入。
case2:现在,如果我制作一个有test.py的文件夹,并尝试进行相同的导入,它仍然没有父母模块无法执行相对导入。
gnewsclient.py.py
的内容class baby():
def method(self):
print 'Method call'
test.py
的内容from gnewsclient import baby # from file_name.py import class_name
b = baby()
b.method()
python test.py
输出
方法调用