2017-08-29 3 views
0
bank 
    __init__.py 
    Account.py 
    SavingAccount.py 
main.py 

SavingAccountクラスは、Account(抽象クラ​​ス)から継承しています。 Python、クラスが抽象クラスを継承するときにパッケージモジュールをインポートする

bank.SavingAccount輸入SavingAccountから

それは " 'アカウント' という名前のモジュール" 表示されない:以下のようにするとmain.py輸入SavingAccount。誰かがそれを解決する方法を知っていましたか?

以下のように出力ウィンドウで完全なエラーコード:

from abc import ABCMeta,abstractmethod 
class Account(metaclass=ABCMeta): 
    _id = 0 
    _name = '' 
    _balance = 0 
    __next = 0 

    def __init__(self,name,initBal = 1000): 
     self._name=name; 
     self._balance = initBal 

がSavingAccount.py

from Account import Account 
class SavingAccount(Account): 
    _interestRate = 0 

    def __init__(self,name,initBal=0): 
     super(SavingAccount,self).__init__(name,initBal) 

    @classmethod 
    def interestRate(cls): 
     _interestRate = 0 

    @classmethod 
    def interestRate(cls,rate): 
     cls._interestRate = rate 
+0

Python 2またはPython 3?同じパッケージの兄弟サブモジュールをインポートする場合は、絶対インポートまたは*明示的*相対インポートのどちらかで行う必要があります。 – user2357112

+0

Python3とPycharm –

答えて

1

、変更すべき

Traceback (most recent call last): 
    File "main.py", line 5, in <module> 
    from bank.SavingAccount import SavingAccount 
    File "\bank\SavingAccount.py", line 1, in <module> 
    from Account import Account 
ModuleNotFoundError: No module named 'Account' 

Acccount.py

from Account import Account 

from .Account import Account 

に後者の相対インポートアプローチは、パッケージの内部に推奨されます。

関連する問題