简体   繁体   English

Eclipse + PyDev + GAE内存缓存“来自导入的未定义变量:get”

[英]Eclipse+PyDev+GAE memcache “Undefined variable from import: get”

I've started using Eclipe+PyDev as an environment for developing my first app for Google App Engine. 我已经开始使用Eclipe + PyDev作为开发我的第一个Google App Engine应用程序的环境。 Eclipse is configured according to this tutorial . Eclipse是根据本教程配置的。

Everything was working until I start to use memcache. 一切正常,直到我开始使用memcache。 PyDev reports the errors and I don't know how to fix it: PyDev报告错误,我不知道如何解决它:

替代文字

Error: Undefined variable from import: get 错误:导入的未定义变量:get

How to fix this? 如何解决这个问题? Sure, it is only PyDev checker problem. 当然,这只是PyDev检查器的问题。 Code is correct and run on GAE. 代码正确并在GAE上运行。

UPDATE: 更新:

  1. I'm using PyDev 1.5.0 but experienced the same with 1.4.8. 我正在使用PyDev 1.5.0,但经历了与1.4.8相同的经历。
  2. My PYTHONPATH includes (set in Project Properties/PyDev - PYTHONPATH): 我的PYTHONPATH包括(在Project Properties / PyDev中设置 - PYTHONPATH):
    • C:\\Program Files\\Google\\google_appengine
    • C:\\Program Files\\Google\\google_appengine\\lib\\django
    • C:\\Program Files\\Google\\google_appengine\\lib\\webob
    • C:\\Program Files\\Google\\google_appengine\\lib\\yaml\\lib

UPDATE 2: 更新2:

I took a look at C:\\Program Files\\Google\\google_appengine\\google\\appengine\\api\\memcache\\__init__.py and found get() is not declared as memcache module function. 我看了一下C:\\Program Files\\Google\\google_appengine\\google\\appengine\\api\\memcache\\__init__.py ,发现get()未被声明为memcache模块函数。 They use the following trick to do that (I didn't hear about such possibility): 他们使用以下技巧来做到这一点(我没有听说过这种可能性):

_CLIENT = None


def setup_client(client_obj):
  """Sets the Client object instance to use for all module-level methods.

  Use this method if you want to have customer persistent_id() or
  persistent_load() functions associated with your client.

  Args:
    client_obj: Instance of the memcache.Client object.
  """
  global _CLIENT
  var_dict = globals()

  _CLIENT = client_obj
  var_dict['set_servers'] = _CLIENT.set_servers
  var_dict['disconnect_all'] = _CLIENT.disconnect_all
  var_dict['forget_dead_hosts'] = _CLIENT.forget_dead_hosts
  var_dict['debuglog'] = _CLIENT.debuglog
  var_dict['get'] = _CLIENT.get
  var_dict['get_multi'] = _CLIENT.get_multi
  var_dict['set'] = _CLIENT.set
  var_dict['set_multi'] = _CLIENT.set_multi
  var_dict['add'] = _CLIENT.add
  var_dict['add_multi'] = _CLIENT.add_multi
  var_dict['replace'] = _CLIENT.replace
  var_dict['replace_multi'] = _CLIENT.replace_multi
  var_dict['delete'] = _CLIENT.delete
  var_dict['delete_multi'] = _CLIENT.delete_multi
  var_dict['incr'] = _CLIENT.incr
  var_dict['decr'] = _CLIENT.decr
  var_dict['flush_all'] = _CLIENT.flush_all
  var_dict['get_stats'] = _CLIENT.get_stats


setup_client(Client())

Hmm... Any idea how to force PyDev to recognize that? 嗯......知道如何强迫PyDev识别出来吗?

There is a cleaner solution: Try adding GAE's memcache to your forced builtins. 有一个更清洁的解决方案:尝试将GAE的memcache添加到您的强制内置。

In your PyDev->Interpreter-Python->ForcedBuiltins window, add the "google.appengine.api.memcache" entry and apply. 在PyDev-> Interpreter-Python-> ForcedBuiltins窗口中,添加“google.appengine.api.memcache”条目并应用。

Double-click on the memcache errors to check them back, they disappear! 双击memcache错误检查它们,它们消失了!

Please make sure that system pythonpath includes google APE install directory. 请确保系统pythonpath包含google APE安装目录。

我参加派对有点晚了,但是你可以在所有使用memcache来有选择地关闭pydev分析的文件中添加以下注释:

#@PydevCodeAnalysisIgnore

If you don't want to turn off all code analysis for your project/module, then just turn it off for that line. 如果您不想关闭项目/模块的所有代码分析,则只需为该行关闭它。 This answer explains that you can hit Ctrl+1 to bring up quick fix and insert #@UndefinedVariable at the end of the line. 这个答案解释了你可以按Ctrl + 1调出快速修复,并在行尾插入#@UndefinedVariable

What version of PyDev are you using? 您使用的是什么版本的PyDev? A recent one (1.5) or the old one referred by the Google tutorial? 最近的一个(1.5)或Google教程引用的旧版本?
See this thread . 看到这个帖子

There is a similar issue with PyROOT PyROOT也存在类似的问题

Since PyDEV plugin does not read $HOME/.pystartup , touching functions/ classes is not a solution. 由于PyDEV插件不读取$HOME/.pystartup ,因此触摸函数/类不是解决方案。 Because it analyze the syntax and structures of python modules to be imported not on-the-fly but when I set the PYTHONPATH from Eclipse's preference panel. 因为它分析了要动态导入的python模块的语法和结构,但是当我从Eclipse的首选项面板设置PYTHONPATH时。

So does your PYTHONPATH reference the Google library? 您的PYTHONPATH参考Google图书馆也是如此吗?


They might be an issue with code completion in 1.5 which could force you to disable code analysis: Pydev 1.5.0 code anlaysis breaks code pyqt4 code completion - ID: 2855598 它们可能是1.5中代码完成的问题,这可能会迫使您禁用代码分析: Pydev 1.5.0代码anlaysis中断代码pyqt4代码完成 - ID:2855598

have pyqt 4.5.4 installed. 已安装pyqt 4.5.4
Initially I had pydev 1.4.8 the open source version installed and code completion worked fine. 最初我有pydev 1.4.8安装的开源版本和代码完成工作正常。
After updating to pydev 1.5.0, pyqt code completion stopped functioning. 更新到pydev 1.5.0后,pyqt代码完成停止运行。
After disabling the Pydev code analysis in "eclipse preferences -> pydev -> editor -> code analysis -> do code analysis?" "eclipse preferences -> pydev -> editor -> code analysis -> do code analysis?"后禁用Pydev "eclipse preferences -> pydev -> editor -> code analysis -> do code analysis?" , code completion began working again for PyQt classes etc. ,代码完成开始为PyQt类等工作。

This worked for me and it's different than the solutions above. 这对我有用,它与上面的解决方案不同。 Pretty straight forward: http://blog.kicaj.com/fixing-pydev-memcache-unresolved-import/ 非常简单: http//blog.kicaj.com/fixing-pydev-memcache-unresolved-import/

Just says to add the google_appengine folder to the pydev python interpreter library. 只是说要将google_appengine文件夹添加到pydev python解释器库中。

I fixed that by adding this few lines to my code on the top of my file: 我通过在我的文件顶部的代码中添加这几行来修复它:

from google.appengine.api import memcache
# work-around for Eclipse+PyDev+GAE memcache error
if not hasattr(memcache, 'set'):
    Client=None
    memcache.setup_client(Client)
    memcache = Client

You can commnet it out in production version. 您可以在生产版本中将其公开。

It's only to keep eclipse happy and to let code completion work. 这只是为了让eclipse高兴并让代码完成工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM