案例1:以下是所有文件结构都位于同一文件夹中:
`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`
但它表示未加载父模块''无法执行相对导入。
案例2:现在如果我在其中创建一个包含test.py的folder2并尝试进行相同的导入仍然没有给出父模块无法执行相对导入。
答案 0 :(得分:1)
gnewsclient.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
<强>输出强>
方法调用