I am trying to use IronPython in the browser and attempting to import external python scripts:
<script src="ht开发者_如何学Ctp://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>
The import statement appears to run successfully (it will fail if test.py doesn't exist). However, it doesn't appear to load the contents of the file. test.py contains the following code:
document.testing1.innerHTML = 'Hello from test.py'
def Hello():
window.Alert('Hello from test.py')
In fact, it doesn't appear to make any difference what is in test.py. It won't complain at syntax errors.
In the IronPython console you can access from within the browser, the same issue happens:
>>> 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'
What am I doing wrong?
Update: It works with the new IronPython 2.7 beta 1 VS2010 integration which kicks off the "Chiron" test web server, but not when I use WebMatrix. I'm wondering if it is an issue with the mime-type configured for .py files being wrong on WebMatrix?
I've finally got it working. It was MIME types that caused the problem. I was using WebMatrix for testing. I added a web.config file with the following xml to fix the issue:
<configuration>
<system.webServer>
<staticContent>
<mimeMap fileExtension=".py" mimeType="text/python" />
</staticContent>
</system.webServer>
</configuration>
精彩评论