PyDev中的未解决的导入错误 [英] Fake unresolved import error in PyDev

查看:193
本文介绍了PyDev中的未解决的导入错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

PyDev报告不存在的导入错误。最初的症状是一个假的未解决的导入错误,这是由以下组合修复的:




  • 清洁项目

  • 重新索引项目(删除解释器,再添加)

  • 重新启动Eclipse

  • li>


现在错误是未验证的变量从导入 - 似乎找不到pymssql.connect。



这不是一个PYHTONPATH问题。我可以访问模块很好,文件中的代码(据称)错误运行正常 - 它有单元测试和生产代码调用它。



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






所以,我打算在其他地方发布这段代码来征求一些意见关于设计,我在评论中被要求发布代码。 (灵感来自:数据库连接包装器和Clint Miller对此问题的回答:如何正确清理Python对象?)。导入错误发生在第69行(self.connection = pymssql.connect ...)。不知道在回答这个问题时有什么好处,但是...

 从util.require_type导入导入pymssql 
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):
#用法:
#with DB ConnectionSource(ConnectionParameters.server1(db ='MyDB))作为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 =无

def __enter __(self):

parent = self

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

def execute(self,sqlString,arguments = {}):
如果self.connection是None:
raise异常('DB Connection not defined')
crsr = self.connection.cursor()
crsr.execute(sqlString,arguments)
返回列表(crsr)

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

self.connection = DBConnection()
self 。$。$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $
$ b $ / code>


解决方案

TL; DR版本:阅读第五个灰色框



您的问题(和我的)应用程式耳朵是由于应该是但不能正确处理的多级进口。



假设有一段时间你有一个文件

  foo / bar.py 

而在该文件中,您有符号名为

  wazoo = 15 

如果您尝试:

 从foo导入栏
从bar import wazoo< ; - false false here

或者如果您尝试使用:

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

可能 wazoo中获取错误的未解决错误 即可。作为一个错误,这显然是不一致的。



然而,如果您执行以下操作:

 从foo.bar导入wazoo 

问题似乎消失了。 / p>

除此之外,我注意到这似乎是在导入的文件中新定义的符号发生的。
此外,该文件中某些符号的早期错误错误将会神奇地消失,唯一的新错误仍然存​​在。这意味着可能会有某种状态文件没有被清除,甚至当你建立等等。



还要注意,这个问题似乎发生了当我使用Python枚举黑客的时候,对我来说最好的 - 也许这将为PyDev-elopers提供一个线索(PyDev是否甚至被维护?):



bar.py:

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

SOMETHING =枚举(A = 1,B = 2)

someotherfile.py:

 从foo import bar 
从bar import SOMETHING
...
x = SOMETHING.A


PyDev is reporting import errors which don't exist. The initial symptom was a fake "unresolved import" error, which was fixed by some combination of:

  • Cleaning the Project
  • Re-indexing the project (remove interpreter, add again)
  • Restarting Eclipse
  • Burning incense to the Python deities

Now the error is "unverified variable from import"--it can't seem to find pymssql.connect.

This IS NOT a PYHTONPATH problem. I can access the module just fine, the code in the file with the (alleged) error runs fine---it has unit tests and production code calling it.

The error is somewhere in PyDev: I added a new module to my PyDev project, and the error only occurs in the new module. I've tried all of the above.


So, I was planning on posting this code somewhere else to solicit some comments about the design, and I was asked in the comments to post code. (Inspired by: Database connection wrapper and Clint Miller's answer to this question: How do I correctly clean up a Python object?). The import error happens at line 69 (self.connection = pymssql.connect...). Not sure what good this does in answering the question, but...

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()

解决方案

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

这篇关于PyDev中的未解决的导入错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆