Python未解决的导入错误

时间:2017-01-30 14:11:19

标签: python

我正在编写我的第一个Python项目,我无法理解为什么我得到一个未解决的导入错误。我有2个类 - apiProcessor和apiDetail。在apiProcessor中我试图从apiDetail调用一个方法。该方法存在,但导入无法解决。两者都在同一个PyDev项目中的同一个包中。

APIDETAIL

from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative.api import declared_attr

    engine = create_engine("mysql+pymysql://efultz:twalker1@localhost:3306/apidb", echo=False)

    Base = declarative_base(engine)
    ########################################################################
    class apiDetail(Base):
        """"""
        __tablename__ = 'api_detail'
        __table_args__ = {'autoload':True}

    #----------------------------------------------------------------------
    def loadSession():
        """"""
        metadata = Base.metadata
        Session = sessionmaker(bind=engine)
        session = Session()
        return session

    if __name__ == "__main__":
        session = loadSession()
        res = session.query(apiDetail).all()
        for entry in res:
            print ('id: ', entry.api_id, ' description: ', entry.description, ' url: ', entry.url, 'json: ', entry.json)

APIPROCESSOR(尝试从APIDETAIL调用loadSession)

#!/usr/bin/python
from apiDetail import apiDetail

class apiProcessor:

    def build_test_objects(self):
        session = apiDetail.loadSession()
        res = session.query(apiDetail).all()
        for entry in res:
            print ('id: ', entry.api_id, ' description: ', entry.description, ' url: ', entry.url, 'json: ', entry.json)

        return res    

if __name__ == "__main__":
    res = build_test_objects()
    for entry in res:
        print ('api id: ', entry.api_id, ' description: ', entry.description, ' url: ', entry.url, 'json: ', entry.json)

1 个答案:

答案 0 :(得分:1)

你正在编写Java而不是python;)

无论如何,问题是from apiDetail import apiDetailapiDetail包导入模块apiDetail。那不是你想要做的。使用import apiDetail代替...然后以apiDetail.apiDetail的形式访问该类...然后将代码重写为python:)。