python - PyDev 中假的 Unresolved 导入错误

标签 python eclipse pydev

PyDev 正在报告不存在的导入错误。最初的症状是一个伪造的“ Unresolved 导入”错误,已通过以下组合修复:

  • 清理项目
  • 重新索引项目(删除解释器,重新添加)
  • 重新启动 Eclipse
  • 给蟒神烧香

现在错误是“未验证的导入变量”——它似乎无法找到 pymssql.connect。

这不是 PYHTONPATH 问题。我可以很好地访问该模块,文件中出现(所谓的)错误的代码运行良好——它有单元测试和生产代码调用它。

错误在 PyDev 的某处:我在 PyDev 项目中添加了一个新模块,但错误仅发生在新模块中。我已经尝试了以上所有方法。


因此,我打算将此代码发布到其他地方以征求有关设计的一些评论,并且在评论中要求我发布代码。 (灵感来自:Database connection wrapper 和 Clint Miller 对这个问题的回答:How do I correctly clean up a Python object?)。导入错误发生在第 69 行(self.connection = pymssql.connect ...)。不确定这对回答问题有什么好处,但是......

import pymssql
from util.require_type import require_type

class Connections(object):
    @require_type('host', str)
    @require_type('user', str)
    @require_type('password', str)
    @require_type('database', str)
    @require_type('as_dict', bool)
    def __init__(self, host, user, password, database, as_dict=True):
        self.host = host
        self.user = user
        self.password = password
        self.db = database
        self.as_dict = as_dict

    @staticmethod
    def server1(db):
        return Connections('','','','')

    @staticmethod
    def server2(db):
        pass

    @staticmethod
    def server3(db):
        pass


class DBConnectionSource(object):
    # Usage:
    #        with DBConnectionSource(ConnectionParameters.server1(db = 'MyDB)) as dbConn:
    #            results = dbConn.execute(sqlStatement)

    @require_type('connection_parameters', Connections)
    def __init__(self, connection_parameters=Connections.server1('MyDB')):
        self.host = connection_parameters.host
        self.user = connection_parameters.user
        self.password = connection_parameters.password
        self.db = connection_parameters.db
        self.as_dict = connection_parameters.as_dict
        self.connection = None

    def __enter__(self):

        parent = self

        class DBConnection(object):
            def connect(self):
                self.connection = pymssql.connect(host=parent.host,
                                                  user=parent.user,
                                                  password=parent.password,
                                                  database=parent.db,
                                                  as_dict=parent.as_dict)

            def execute(self, sqlString, arguments={}):
                if self.connection is None:
                    raise Exception('DB Connection not defined')
                crsr = self.connection.cursor()
                crsr.execute(sqlString, arguments)
                return list(crsr)

            def cleanup(self):
                if self.connection:
                    self.connection.close()

        self.connection = DBConnection()
        self.connection.connect()
        return self.connection

    def __exit__(self, typ, value, traceback):
        self.connection.cleanup()

最佳答案

在错误所在的行尝试 ctrl+1 并添加一条注释,说明您期待导入。这应该可以解决 PyDev 错误,因为它进行静态代码分析而不是运行时分析。

关于python - PyDev 中假的 Unresolved 导入错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14260714/

相关文章:

python - 在 PyDev 中使用 nosetests 进行交互式调试

eclipse - Eclipse Juno 中不支持 GlassFish 服务器吗?

eclipse - 只能在以 root 身份运行 Eclipse 或使用 -clean 选项时使用 PyDev 插件

python - Python 中的简单字符串重写系统

python - 在递归中仅应用一次条件

python - 在 Pydev/Eclipse 的 python 程序中使用外部 C 库

java - GWT 2.6.1 拒绝在 Eclipse Luna 中启动开发模式

python - pyDev-Eclipse : how to delay the hints

python - 如何在 ipython 笔记本中将 matplotlib 图抓取为 html?

python - 如何使用Python流对象?