导入时出现问题胶子.html从web2py外部

2024-05-23 20:29:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我想尝试从web2py外部编写一些胶子派生的组件(web2pyhtml helpers)。通过这种方式,我可以在命令行上测试组件,并生成简单的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')

产生预期输出:

^{pr2}$

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

我发现的唯一问题是,我需要有一个到web2py的符号链接,gluon.html导入才能工作,即使胶子在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_dir和{}:它们是为这个问题介绍的)


Tags: installinfrompyimport应用程序htmldir
1条回答
网友
1楼 · 发布于 2024-05-23 20:29:57

你不应该做链接。在

如果希望导入找到“gluon”,则应位于PYTHONPATH的文件夹是其父文件夹:系统路径插入(0,“/install\u dir/web2py')。在

这是可行的(这里的拉链包含胶子文件夹):

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

相关问题 更多 >