簡體   English   中英

如何使用另一個.py文件中的類方法將兩個文件放在同一個程序包中,並且兩個文件都放在不同的文件夾中?

[英]How to use class methods from another .py file both files in same package and when both are in different folders?

情況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並嘗試執行相同的導入操作,則仍然沒有任何父模塊無法執行相對導入。

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

產量

方法調用

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM