[英]How to use class methods from another .py file both files in same package and when both are in different folders?
Case1: Following is the file structure all are in same folder: 情况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`
but it says parent module not loaded '' cannot perform relative import. 但是它说未加载的父模块“无法执行相对导入。
Case2: Also now if I make a folder2 which has test.py inside it and try to do same importing still it gives no parent module cannot perform relative import. 情况2:同样,现在,如果我制作一个其中包含test.py的folder2并尝试执行相同的导入操作,则仍然没有任何父模块无法执行相对导入。
Content of gnewsclient.py gnewsclient.py的内容
class baby():
def method(self):
print 'Method call'
Content of test.py test.py的内容
from gnewsclient import baby # from file_name.py import class_name
b = baby()
b.method()
python test.py python test.py
Output 产量
Method call 方法调用
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.