Developing with IDEs: WingIDE, Rad2Py, Eclipse and PyCharm
You can use web2py with third party IDEs such as WingIDE, Rad2Py, Eclipse and PyCharm.
PyCharm
PyCharm v3 Professional Edition introduces built-in support for web2py.
As of v3.0, PyCharm will recognise web2py applications belong to a web2py parent directory, and this will activate the web2py support. Support means that the web2py library is added to the project.
Start a PyCharm project by opening the directory of the web2py installation, and then navigate through the applications directory.
PyCharm’s git integration copes with this; it supports multiple git repositories inside the one project directory structure. Keep each application in its own git repository for easy deployment.
The Professional edition includes a default launcher for a web2py project starts web2py.py and therefore launches the integrated rocket server.
PyCharm Debugging tips
You can drop into PyCharm’s debugger like any other Python session. This means that you can start debugging the script web2py.py, which will run the rocket server, with PyCharm connected to this process. Use your browser to activate the controller function you are interested it.
When a breakpoint is encountered during the request, you will arrive in PyCharm’s debugger.
However, this requires removal of from gluon.debug import dbg
statements.
Pycharm: Debugging a module in web2py’s context
It can be convenient to directly debug a module with test code in isolation from the rest of web2py. You can make a standard PyCharm run/debug configuration to run a module file as a stand-alone python script and put your testing code in
if __name__ == "__main__":
but then you don’t get web2py’s models and database connections.
You can make dummy controllers and breakpoint into your module after running the dummy controller function via your browser, as described above, but there is a more convenient option:
Imagine you have this testing block in a module module_1.py
:
if __name__ == "__main__":
... code which tests things
and at the same time, you want the database connections and models to be available the same as web2py does normally.
Using web2py command line options, you can make a PyCharm run/debug configuration do this. Make the script: web2py.py
Make the script parameters: -S <your_app_name> -M -R /path/to/your_module/module_1.py
`:code
This will run the web2py models, and then load your module, which will then run the __main__
code in a context similar to a running web2py app. These command line options are documented in chapter 4.
WingIDE
Here is a screenshot of using web2py with WingIDE:
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 *
As well, if using Eclipse with PyDev you should add the web2py folder to the system python path (in PyDev preferences for the python interpreter). In some versions of PyDev, it has been possible to use the debugging support inside Eclipse by launching web2py.py. It is probably wise to remove the import of the gluon debug module. Also, to do this you should make the pydev project the web2py directory, not your specific application.
PyDev’s git integration copes with this; it supports multiple git repositories inside the one project directory structure.