Search code examples
pythonpycharmweb2py

How to import standalone web2py gluon DAL in pyCharm project


I'm new to python, web2py, and pyCharm, but have had success creating small projects in both web2py and pyCharm. I'm trying to write a fairly simple project in web2py, but would like to develop/debug core functions in pyCharm before moving to a web2py environment.

I'm doing this on a Mac running El Capitan 10.11.4, pyCharm Professional 2016.1.2, and the latest install of web2py source installed in my user directory "~/web2py"

I'm running into a basic problem of not being able to import DAL from gluon. The error I get is:

File "/Users/zabgay/Documents/GitHub/untitled/import_gluon.py", line 8, in <module>
    from gluon import DAL
ImportError: No module named gluon

So far I've created a "pure python" project in pyCharm Professional with one python file containing the following code:

import sys
print (sys.path)

sys.path.insert(1, "~/web2py/gluon")
sys.path.insert(1, "~/web2py")

print (sys.path)
from gluon import DAL
db = DAL('sqlite://storage.sqlite')

The sys.path variable printed out in the second print statement is:

['/Users/zabgay/Documents/GitHub/untitled',    '/Library/Frameworks/SQLite3.framework/Versions/B/Python/2.7', '/Library/Frameworks/GEOS.framework/Versions/3/Python/2.7', '/Library/Python/2.7/site-packages/numpy-override', '/Library/Frameworks/GDAL.framework/Versions/1.11/Python/2.7/site-packages', '/Library/Python/2.7/site-packages/pandas-0.15.1-py2.7-macosx-10.10-intel.egg', '/Library/Python/2.7/site-packages/Sys-1.0-py2.7.egg', '/Library/Python/2.7/site-packages/pip-7.1.0-py2.7.egg', '/Library/Frameworks/cairo.framework/Versions/1/Python/2.7', '/Users/zabgay/Documents/GitHub/untitled', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages', '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload', '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC', '/Library/Python/2.7/site-packages']

I've confirmed that ~/web2py/gluon exists and is populated with files.


Solution

  • If you want to use web2py's DAL outside the framework you need to install it as a standalone package.

    pip install pyDAL
    

    Instead of gluon you use:

    from pydal import DAL
    

    Hope it helps!