简体   繁体   中英

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:

`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.

Content of gnewsclient.py

class baby():
    def method(self):
        print 'Method call'

Content of test.py

from gnewsclient import baby # from file_name.py import class_name

b = baby()
b.method()

python test.py

Output

Method call

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM