代码之家  ›  专栏  ›  技术社区  ›  Mark Heath

无法在浏览器中的IronPython DLR中导入外部脚本

  •  0
  • Mark Heath  · 技术社区  · 14 年前

    我想用 IronPython in the browser

    <script src="http://gestalt.ironpython.net/dlr-latest.js" type="text/javascript">
    </script>
    ...
    <script type="application/python" src="test.py" defer="true"></script>
    <script type="application/python">
    import test
    test.Hello()
    </script>
    

    import语句似乎运行成功(如果test.py不存在,它将失败)。但是,它似乎没有加载文件的内容。test.py包含以下代码:

    document.testing1.innerHTML = 'Hello from test.py'
    
    def Hello():
       window.Alert('Hello from test.py')
    

    实际上,test.py中的内容似乎没有任何区别。它不会抱怨语法错误。

    在您可以从浏览器中访问的IronPython控制台中,也会出现同样的问题:

    >>> import test
    >>> dir(test)
    ['__builtins__', '__doc__', '__file__', '__name__', '__package__']
    >>> test.Hello()
    Traceback (most recent call last):
      at <module> in <string>, line 1
      at <module> in <string>, line 0
    AttributeError: 'module' object has no attribute 'Hello'
    

    更新:

    1 回复  |  直到 14 年前
        1
  •  0
  •   Mark Heath    14 年前

    我终于把它修好了。是MIME类型导致了这个问题。我在用WebMatrix进行测试。我添加了一个包含以下xml的web.config文件来解决此问题:

    <configuration>
        <system.webServer>
            <staticContent>
                <mimeMap fileExtension=".py" mimeType="text/python" />
            </staticContent>
        </system.webServer>
    </configuration>