Eclipse+PyDev+GAE memcache“导入时未定义的变量:获取”

发布于 2024-08-05 20:52:12 字数 2320 浏览 1 评论 0原文

我开始使用 Eclipe+PyDev 作为开发我的第一个 Google App Engine 应用程序的环境。 Eclipse 是根据本教程配置的。

一切正常,直到我开始使用内存缓存。 PyDev 报告错误,我不知道如何修复它:

alt text

错误:导入时未定义变量:get

如何解决此问题? 当然,这只是 PyDev 检查器问题。代码正确并在 GAE 上运行。

更新:

  1. 我正在使用 PyDev 1.5.0,但在 1.4.8 中也有同样的经历。
  2. 我的 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

更新 2:

我查看了 C:\Program Files\Google \google_appengine\google\appengine\api\memcache\__init__.py 并发现 get() 未声明为 memcache 模块函数。他们使用以下技巧来做到这一点(我没有听说过这种可能性):

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

嗯......知道如何迫使 PyDev 认识到这一点吗?

I've started using Eclipe+PyDev as an environment for developing my first app for Google App Engine. Eclipse is configured according to this tutorial.

Everything was working until I start to use memcache. PyDev reports the errors and I don't know how to fix it:

alt text

Error: Undefined variable from import: get

How to fix this?
Sure, it is only PyDev checker problem. Code is correct and run on GAE.

UPDATE:

  1. I'm using PyDev 1.5.0 but experienced the same with 1.4.8.
  2. My PYTHONPATH includes (set in 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:

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. 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

三月梨花 2024-08-12 20:52:12

有一个更简洁的解决方案:尝试将 GAE 的内存缓存添加到您的强制内置中。

在 PyDev->Interpreter-Python->ForcedBuiltins 窗口中,添加“google.appengine.api.memcache”条目并应用。

双击内存缓存错误来检查它们,它们消失了!

请确保系统 pythonpath 包含 google APE 安装目录。

There is a cleaner solution: Try adding GAE's memcache to your forced builtins.

In your PyDev->Interpreter-Python->ForcedBuiltins window, add the "google.appengine.api.memcache" entry and apply.

Double-click on the memcache errors to check them back, they disappear!

Please make sure that system pythonpath includes google APE install directory.

十雾 2024-08-12 20:52:12

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

#@PydevCodeAnalysisIgnore

I'm a bit late to the party, but you can add the following comment in all of your files that use memcache to selectively switch off pydev analysis:

#@PydevCodeAnalysisIgnore

马蹄踏│碎落叶 2024-08-12 20:52:12

如果您不想关闭项目/模块的所有代码分析,则只需关闭该行即可。 这个答案解释了这一点您可以按 Ctrl+1 调出快速修复,然后在行尾插入 #@UndefinedVariable

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.

西瑶 2024-08-12 20:52:12

您使用什么版本的 PyDev?最近的版本 (1.5) 还是 Google 教程提到的旧版本?
请参阅此主题

PyROOT 也存在类似的问题

由于 PyDEV 插件无法读取 $HOME/.pystartup,因此触摸函数/类不是解决方案。因为它不是即时分析要导入的 python 模块的语法和结构,而是当我从 Eclipse 的首选项面板设置 PYTHONPATH 时分析。

那么您的 PYTHONPATH 是否引用了 Google 库?


它们可能是 1.5 中代码完成的问题,这可能会迫使您禁用代码分析:Pydev 1.5.0 代码分析破坏代码 pyqt4 代码完成 - ID:2855598

已安装pyqt 4.5.4
最初我安装了开源版本 pydev 1.4.8 并且代码完成工作正常。
更新到 pydev 1.5.0 后,pyqt 代码补全停止运行。
“eclipse偏好设置->pydev->编辑器->代码分析->进行代码分析?”中禁用Pydev代码分析后,代码完成开始工作再次为 PyQt
课程等

What version of PyDev are you using? A recent one (1.5) or the old one referred by the Google tutorial?
See this thread.

There is a similar issue with PyROOT

Since PyDEV plugin does not read $HOME/.pystartup, touching functions/ classes is not a solution. 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.

So does your PYTHONPATH reference the Google library?


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

have pyqt 4.5.4 installed.
Initially I had pydev 1.4.8 the open source version installed and code completion worked fine.
After updating to pydev 1.5.0, pyqt code completion stopped functioning.
After disabling the Pydev code analysis in "eclipse preferences -> pydev -> editor -> code analysis -> do code analysis?", code completion began working again for PyQt
classes etc.

马蹄踏│碎落叶 2024-08-12 20:52:12

这对我有用,并且与上面的解决方案不同。
非常简单:
http://blog.kicaj.com/fixing-pydev-memcache-unresolved- import/

只是说将 google_appengine 文件夹添加到 pydev python 解释器库中。

This worked for me and it's different than the solutions above.
Pretty straight forward:
http://blog.kicaj.com/fixing-pydev-memcache-unresolved-import/

Just says to add the google_appengine folder to the pydev python interpreter library.

夏了南城 2024-08-12 20:52:12

我通过将这几行添加到文件顶部的代码中来解决这个问题:

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

您可以在生产版本中将其通讯出去。

这只是为了让 Eclipse 开心并让代码完成工作。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文