如何在 pyCharm 项目中导入独立的 web2py gluon DAL

How to import standalone web2py gluon DAL in pyCharm project

我是 python、web2py 和 pyCharm 的新手,但在 web2py 和 pyCharm 中成功创建了小型项目。我正在尝试在 web2py 中编写一个相当简单的项目,但在移动到 web2py 环境之前,我想在 pyCharm 中 develop/debug 核心功能。

我在 Mac 运行 El Capitan 10.11.4,pyCharm Professional 2016.1.2 上执行此操作,并且在我的用户目录中安装了最新安装的 web2py 源“~/web2py”

我 运行 遇到了无法从 gluon 导入 DAL 的基本问题。我得到的错误是:

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

到目前为止,我已经在 pyCharm Professional 中创建了一个 "pure python" 项目,其中一个 python 文件包含以下代码:

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')

第二条打印语句打印出的sys.path变量为:

['/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']

我已经确认 ~/web2py/gluon 存在并且填充了文件。

如果你想在框架外使用 web2py 的 DAL,你需要将它作为独立包安装。

pip install pyDAL

而不是 gluon 你使用:

from pydal import DAL

希望对您有所帮助!