5

PyDev 正在报告不存在的导入错误。最初的症状是假的“未解决的导入”错误,通过以下组合修复:

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

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

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

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


因此,我计划在其他地方发布此代码以征求有关设计的一些评论,并在评论中要求我发布代码。(灵感来自:数据库连接包装器和克林特米勒对这个问题的回答:如何正确清理 Python 对象?)。导入错误发生在第 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()
4

3 回答 3

1

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

于 2013-01-10T15:34:17.507 回答
1

TL;DR version: read the fifth grey box.

Your problem (and mine) appears to be due to multiple levels of imports that should be, but is not, handled correctly. Somewhere along the line, linkage is lost.

Assume for a moment that you have a file

foo/bar.py

and that within that file, you have a symbol named

wazoo=15

If you then try:

from foo import bar
from bar import wazoo <-- false error here

Or if you try to use:

from foo import bar
...
i = bar.wazoo <-- false error here

You may get the false unresolved error on wazoo. Being a bug, this is obviously inconsistent.

If however, you do the following:

from foo.bar import wazoo

The problems do seem to go away.

As an aside, I've noted that this sometime seems to happen for newly-defined symbols within an imported file. Furthermore, the earlier false errors for some symbols within that file will magically disappear, with the only the new error remaining. This implies that there might be some sort of state file that isn't being cleaned, EVEN WHEN you "buildall", etc.

Note also that this problem seems to occur the most for me when I use the Python enum hack...perhaps this will provide a clue to the PyDev-elopers (is PyDev even being maintained anymore?):

bar.py:

def enum(**enums):
  return type('Enum', (), enums)

SOMETHING = enum(A=1, B=2)

someotherfile.py:

from foo import bar
from bar import SOMETHING
...
x = SOMETHING.A
于 2015-08-11T18:31:12.550 回答
0

我在抛出错误的行尾添加了 # @UndefinedVariable。

这不是一个永久性的修复,但至少它暂时摆脱了我屏幕上的红色。如果有更好的长期解决方案,我会很高兴听到它。

于 2014-02-18T17:12:41.513 回答