Eclipse+PyDev+GAE memcache“导入时未定义的变量:获取”
我开始使用 Eclipe+PyDev 作为开发我的第一个 Google App Engine 应用程序的环境。 Eclipse 是根据本教程配置的。
一切正常,直到我开始使用内存缓存。 PyDev 报告错误,我不知道如何修复它:
错误:导入时未定义变量:get
如何解决此问题? 当然,这只是 PyDev 检查器问题。代码正确并在 GAE 上运行。
更新:
- 我正在使用 PyDev 1.5.0,但在 1.4.8 中也有同样的经历。
- 我的 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:
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:
- I'm using PyDev 1.5.0 but experienced the same with 1.4.8.
- 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
有一个更简洁的解决方案:尝试将 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.
我来晚了一点,但是您可以在所有使用 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
如果您不想关闭项目/模块的所有代码分析,则只需关闭该行即可。 这个答案解释了这一点您可以按 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.您使用什么版本的 PyDev?最近的版本 (1.5) 还是 Google 教程提到的旧版本?
请参阅此主题。
PyROOT 也存在类似的问题
那么您的
PYTHONPATH
是否引用了 Google 库?它们可能是 1.5 中代码完成的问题,这可能会迫使您禁用代码分析:Pydev 1.5.0 代码分析破坏代码 pyqt4 代码完成 - ID:2855598
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
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
这对我有用,并且与上面的解决方案不同。
非常简单:
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.
我通过将这几行添加到文件顶部的代码中来解决这个问题:
您可以在生产版本中将其通讯出去。
这只是为了让 Eclipse 开心并让代码完成工作。
I fixed that by adding this few lines to my code on the top of my file:
You can commnet it out in production version.
It's only to keep eclipse happy and to let code completion work.