2017-09-01 2 views
0

基本クラスを継承するクラスで構成されるパッケージのインポートに関する質問があります。ここに私のディレクトリ構造は次のとおりです。基本クラスを継承するクラスを持つパッケージをインポートする

. 
|-- cisco.py 
|-- cisco.pyc 
|-- __init__.py 
|-- __init__.pyc 
|-- objects.py 
`-- objects.pyc 

0 directories, 6 files 
[email protected]:~/objects# 

私の親クラスはobjects.pyの内側にある:

class BasePlatform(object): 
     def __init__(self,ip,hostname): 
       self.ip = ip 
       self.hostname = hostname 

       print self.ip,self.hostname 

     def hello(self): 
       print 'hello world' 

そして、私の子クラスcisco.pyの内側にある:

の内部
class CiscoPlatform(BasePlatform): 
     def somefunc(self): 
       print 'hello world' 

私ののinitの.py、私はこれを持っています:

from . import objects 
from . import cisco 

しかし、私は実行すると、これは私が取得エラーです:

[email protected]:~/objects# cd .. 
[email protected]:~# python 
Python 2.7.6 (default, Oct 26 2016, 20:30:19) 
[GCC 4.8.4] on linux2 
Type "help", "copyright", "credits" or "license" for more information. 
>>> import objects 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
    File "objects/__init__.py", line 2, in <module> 
    from . import cisco 
    File "objects/cisco.py", line 1, in <module> 
    class CiscoPlatform(BasePlatform): 
NameError: name 'BasePlatform' is not defined 
>>> 

誰かが私を助けてくださいことはできますか?ありがとう!

答えて

1
from objects import BasePlatform 
class CiscoPlatform(BasePlatform): 
    def somefunc(self): 
     print 'hello world' 

あなたが

をcisco.pyする BasePlatformクラスをインポートする必要があります
関連する問題