简体   繁体   中英

Emacs elpy and web2py

I have setup Python code completion by installing elpy from Melpa , and it seem to work (mostly) as advertised.

However I want it to also complete the web2py API, and I figured the best way to achieve this is to use the web2py shell, instead of the normal shell.

0th try

Googleing for Emacs , web2py and auto-completion yielded no useful results.

1st try

So I added to my ~/.emacs.d/init.el file

(setq python-shell-interpreter-args 
      "/path/to/web2py/web2py.py --plain --import_models --shell=myapp")

... did not work.

2nd try

Following the elpy doc I realize I probably need to customize the elpy-rpc-python-command variable.

So I wrote this ~/bin/rpc-web2py script such as:

#!/bin/sh
# note: the $@ need to relate to python becase elpy what's to pass `-W` to it.
python2.7 "$@" /path/to/web2py/web2py.py --shell=myapp --plain --import_models

and customized the Elpy Rpc Python Command to be Other: ~/bin/rpc-web2py

... still no "db." completion.

help

Am I approaching this problem in the right way? I am not committed to any specific code-completion solution, and am willing to have a fresh .emacs.d if need be.

Was anybody able to have a similar working setup?

One obvious oversight from my part was that I didn't add the /path/to/web2py to my $PYTHONPATH environment variable, as helpfully stated by Baris's on the code... blog.

The other part of the answer comes from The Web2py book by Massimo Di Pierro, that has this to say:

Using general purpose IDEs with web2py

The general problem with these IDEs (except those which support web2py) is that they do not understand the context in which models and controllers are executed and therefore autocompletion does not work out of the box.

To make autocompletion work the general trick consists in editing your models and controllers and adding the following code:

if False:
    from gluon import *
    request = current.request
    response = current.response
    session = current.session
    cache = current.cache
    T = current.T

The import block does not change the logic as this is never executed but it forces the IDE to parse it and understand where the objects in the global namespace come from (the gluon module) thus making autocompletion work.

If you are relying on variables in your models (such as database definitions) you may consider adding to the list like this:

from db import *

You can also consider importing all models.

if False:  
    from gluon import *
    from db import *  #repeat for all models
    from menu import *

While this is more a workaround than a solution, I will take it, and yasnippet that already comes as part of elpy is the logical tool to make this workaround trivial.

If somebody will come up with some less workaround-ish solution I will gladly accept that answer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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