Search code examples
pythondjangowebapp2

Use ipdb with webapp2


I am django developer recently starting with webapp2 project. My question is how do I enter into debugging mode in webapp2 application using ipdb package as we do in django and flask.

my app structure:

helloapp
    - libs/
    - stylesheets/
    - templates/
    - .gitignore
    - app.yaml
    - index.yaml
    - main.py
    - webapp2.py

I have installed ipdb in libs folder using

sudo pip install -t github_projects/hellowebapp2/libs ipdb

main.py

from .libs import ipdb
class HelloWebapp2(webapp2.RedirectHandler):
    def get(self):
        import ipdb; ipdb.set_trace()

Error

/home/kishan/github_projects/hellowebapp2/main.py
ERROR    2016-11-07 06:48:01,566 wsgi.py:263] 
Traceback (most recent call last):
  File "/usr/lib/google-cloud-sdk/platform/google_appengine/google/appengine/runtime/wsgi.py", line 240, in Handle
    handler = _config_handle.add_wsgi_middleware(self._LoadHandler())
  File "/usr/lib/google-cloud-sdk/platform/google_appengine/google/appengine/runtime/wsgi.py", line 299, in _LoadHandler
    handler, path, err = LoadObject(self._handler)
  File "/usr/lib/google-cloud-sdk/platform/google_appengine/google/appengine/runtime/wsgi.py", line 85, in LoadObject
    obj = __import__(path[0])
  File "/home/kishan/github_projects/hellowebapp2/main.py", line 12, in <module>
from .libs import ipdb
ValueError: Attempted relative import in non-package

Update

I followed this link now I get different error ImportError: No module named termios :(


Solution

  • Its recommended that You should use virtualenvironments. Install virtualenvwrapper using pip.

    pip install virtualenvwrapper
    

    After that open your bashrc and insert relevant lines as mentioned in virtualenvwrapper docs. Then create a virtual environment.

    mkvirtualenv myapp
    

    install all python packages including webapp2 to your virtual environment.

    pip install webapp2
    pip install webob
    pip install paste
    pip install ipdb
    

    This way, all your packages will be at one single location (/home/username/.virtualenvs/myapp)

    And you can import any installed package you want without headaches from relative imports. And for missing termios, you may check SO Post