【问题标题】:Problems importing gluon.html from outside web2py从 web2py 外部导入 gluon.html 的问题
【发布时间】:2012-11-30 16:44:02
【问题描述】:

我想尝试在web2py 之外编写一些胶子衍生组件(web2py html 助手)。通过这种方式,我可以在命令行上测试组件,并生成简单的 html 文件,我可以在将它们与完整的web2py 应用程序堆栈集成之前对其进行分析。这加快了我的开发周期,因为我可以专注于微调组件的 HTML 而无需担心任何其他问题(重新部署、数据库等)。快速简单:使用 gluon.html 助手准备一个 web2py 组件,查看生成的 HTML 是否看起来不错,然后然后web2py 中使用它。

测试基本上是成功的。我只需要:

from   gluon.html                import A, TABLE, DIV, SPAN, THEAD, TR, TH, TBODY, URL, H1

def T(txt):
    return txt

def my_simple_component(title):
    return H1(T(title))

def test():
    print my_simple_component('Hello')

产生预期的输出:

$ python test_web2py_components.py
<h1>Hello</h1>

(我不关心我的测试中的T。对于我想要使用的任何测试URL,我只传递一个假的应用程序/控制器/函数。)

我发现的唯一问题是我需要一个指向 web2py 的符号链接才能使 gluon.html 导入工作,即使 gluon 位于 PYTHONPATH 上。

ln -s /install_dir/web2py/gluon/ .

有没有办法在没有额外链接的情况下导入胶子组件?

考虑到在测试阶段我的组件在 web2py 应用程序树之外。

这是我得到的错误:

Traceback (most recent call last):
  File "lib_test1.py", line 4, in <module>
    from   gluon.html                import A, TABLE, DIV, SPAN, THEAD, TR, TH, TBODY, URL
  File "/install_dir/web2py/gluon/__init__.py", line 15, in <module>
    from globals import current
  File "/install_dir/web2py/gluon/globals.py", line 24, in <module>
    from serializers import json, custom_json
  File "/install_dir/web2py/gluon/serializers.py", line 11, in <module>
    from languages import lazyT
  File "/install_dir/web2py/gluon/languages.py", line 264, in <module>
    PLURAL_RULES = read_possible_plurals()
  File "/install_dir/web2py/gluon/languages.py", line 250, in read_possible_plurals
    for pname in os.listdir(pdir):
OSError: [Errno 2] No such file or directory: '/current_dir/gluon/contrib/rules'

(不要考虑install_dircurrent_dir:他们已经为这个问题引入了)

【问题讨论】:

    标签: python web2py


    【解决方案1】:

    你不应该建立链接。

    如果您希望导入找到“gluon”,则应位于 PYTHONPATH 的文件夹是其父文件夹:sys.path.insert(0,'/install_dir/web2py')。

    这行得通,(这里的 zip 包含胶子文件夹):

    >>> import sys
    >>> sys.path.insert(0, 'C:\web2py\library.zip')
    >>> from gluon.html import H1
    >>> print H1("Hello")
    <h1>Hello</h1>
    

    【讨论】:

      猜你喜欢
      • 2014-01-17
      • 2020-06-29
      • 1970-01-01
      • 2018-09-01
      • 2011-02-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多