无法导入“日志记录”在嵌入式 Jetty 中使用 Jython 的模块 ->导入错误:未找到操作系统特定模块

发布于 2024-09-13 07:34:43 字数 5272 浏览 9 评论 0原文

我正在尝试在 Jetty 服务器(全部通过 Maven)中使用 Jython(嵌入式)来调用简单的 Python 脚本。

只要我不尝试使用任何标准库(例如“日志记录”),我的脚本就可以正常工作。每当我尝试导入标准库之一时,它都会失败并出现异常“ImportError”。

我得到的例外是:

  File "<string>", line 1, in <module>
  File "c:\home\work\sample\content\helloworld\helloworld.py", line 10, in <module>
    import logging
  File "c:\home\work\sample\content\Lib\logging\__init__.py", line 29, in <module>
    import sys, os, types, time, string, cStringIO, traceback
  File "c:\home\work\sample\content\Lib\os.py", line 119, in <module>
    raise ImportError, 'no os specific module found'
ImportError: no os specific module found

    at org.python.core.PyException.fillInStackTrace(PyException.java:70)
    at java.lang.Throwable.<init>(Throwable.java:181)
    at java.lang.Exception.<init>(Exception.java:29)
    at java.lang.RuntimeException.<init>(RuntimeException.java:32)
    at org.python.core.PyException.<init>(PyException.java:46)
    at org.python.core.PyException.doRaise(PyException.java:200)
    at org.python.core.Py.makeException(Py.java:1159)
    at org.python.core.Py.makeException(Py.java:1163)
    at os$py.f$0(c:\home\work\sample\content\Lib\os.py:692)
    at os$py.call_function(c:\home\work\sample\content\Lib\os.py)
    at org.python.core.PyTableCode.call(PyTableCode.java:165)
    at org.python.core.PyCode.call(PyCode.java:18)
    at org.python.core.imp.createFromCode(imp.java:325)
    at org.python.core.imp.createFromPyClass(imp.java:144)
    at org.python.core.imp.loadFromSource(imp.java:504)
    at org.python.core.imp.find_module(imp.java:410)
    at org.python.core.imp.import_next(imp.java:620)
    at org.python.core.imp.import_first(imp.java:650)
    at org.python.core.imp.import_name(imp.java:741)
    at org.python.core.imp.importName(imp.java:791)
    at org.python.core.ImportFunction.__call__(__builtin__.java:1236)
    at org.python.core.PyObject.__call__(PyObject.java:367)
    at org.python.core.__builtin__.__import__(__builtin__.java:1207)
    at org.python.core.__builtin__.__import__(__builtin__.java:1190)
    at org.python.core.imp.importOne(imp.java:802)
    at logging$py.f$0(c:\home\work\sample\content\Lib\logging\__init__.py:1372)
    at logging$py.call_function(c:\home\work\sample\content\Lib\logging\__init__.py)
    at org.python.core.PyTableCode.call(PyTableCode.java:165)
    at org.python.core.PyCode.call(PyCode.java:18)
    at org.python.core.imp.createFromCode(imp.java:325)
    at org.python.core.imp.createFromPyClass(imp.java:144)
    at org.python.core.imp.loadFromSource(imp.java:504)
    at org.python.core.imp.find_module(imp.java:410)
    at org.python.core.imp.import_next(imp.java:620)
    at org.python.core.imp.import_first(imp.java:650)
    at org.python.core.imp.import_name(imp.java:741)
    at org.python.core.imp.importName(imp.java:791)
    at org.python.core.ImportFunction.__call__(__builtin__.java:1236)
    at org.python.core.PyObject.__call__(PyObject.java:367)
    at org.python.core.__builtin__.__import__(__builtin__.java:1207)
    at org.python.core.__builtin__.__import__(__builtin__.java:1190)
    at org.python.core.imp.importOne(imp.java:802)
    at helloworld.helloworld$py.f$0(c:\home\work\sample\content\helloworld\helloworld.py:19)
    at helloworld.helloworld$py.call_function(c:\home\work\sample\content\helloworld\helloworld.py)
    at org.python.core.PyTableCode.call(PyTableCode.java:165)
    at org.python.core.PyCode.call(PyCode.java:18)
    at org.python.core.imp.createFromCode(imp.java:325)
    at org.python.core.imp.createFromPyClass(imp.java:144)
    at org.python.core.imp.loadFromSource(imp.java:504)
    at org.python.core.imp.find_module(imp.java:410)
    at org.python.core.PyModule.impAttr(PyModule.java:109)
    at org.python.core.imp.import_next(imp.java:622)
    at org.python.core.imp.import_name(imp.java:761)
    at org.python.core.imp.importName(imp.java:791)
    at org.python.core.ImportFunction.__call__(__builtin__.java:1236)
    at org.python.core.PyObject.__call__(PyObject.java:367)
    at org.python.core.__builtin__.__import__(__builtin__.java:1207)
    at org.python.core.imp.importFromAs(imp.java:869)
    at org.python.core.imp.importFrom(imp.java:845)
    at org.python.pycode._pyx1.f$0(<string>:1)
    at org.python.pycode._pyx1.call_function(<string>)
    at org.python.core.PyTableCode.call(PyTableCode.java:165)
    at org.python.core.PyCode.call(PyCode.java:18)
    at org.python.core.Py.runCode(Py.java:1197)
    at org.python.core.Py.exec(Py.java:1241)
    at org.python.util.PythonInterpreter.exec(PythonInterpreter.java:138)

我的脚本看起来像:

from java.util import Random
from java.util import Date

import sys

print(sys.path)
print(sys.builtin_module_names)

import logging

logging.basicConfig(level=logging.WARNING)
logger1 = logging.getLogger('aaa') 
logger1.warning('************* This message comes from one module') 

def say_hello():
        return 'hello world1'

到目前为止我已经尝试过以下操作,但没有任何效果:

  • 包括我的类路径中的“Lib”目录
  • 当我设置解释器时,对“Lib”路径进行硬编码。

如果我直接从交互式 Jython shell 执行此操作,则脚本可以正常工作(并且会显示一条日志消息)。

谢谢。

康健泉

I'm trying to use Jython (embedded) in a Jetty server (all through Maven) to invoke a simple Python script.

My script works fine as long as I don't try to use any of the standard library's such as 'logging.' Whenever I try to import one of the standard library's it fails with the exception "ImportError."

The exception I get is:

  File "<string>", line 1, in <module>
  File "c:\home\work\sample\content\helloworld\helloworld.py", line 10, in <module>
    import logging
  File "c:\home\work\sample\content\Lib\logging\__init__.py", line 29, in <module>
    import sys, os, types, time, string, cStringIO, traceback
  File "c:\home\work\sample\content\Lib\os.py", line 119, in <module>
    raise ImportError, 'no os specific module found'
ImportError: no os specific module found

    at org.python.core.PyException.fillInStackTrace(PyException.java:70)
    at java.lang.Throwable.<init>(Throwable.java:181)
    at java.lang.Exception.<init>(Exception.java:29)
    at java.lang.RuntimeException.<init>(RuntimeException.java:32)
    at org.python.core.PyException.<init>(PyException.java:46)
    at org.python.core.PyException.doRaise(PyException.java:200)
    at org.python.core.Py.makeException(Py.java:1159)
    at org.python.core.Py.makeException(Py.java:1163)
    at os$py.f$0(c:\home\work\sample\content\Lib\os.py:692)
    at os$py.call_function(c:\home\work\sample\content\Lib\os.py)
    at org.python.core.PyTableCode.call(PyTableCode.java:165)
    at org.python.core.PyCode.call(PyCode.java:18)
    at org.python.core.imp.createFromCode(imp.java:325)
    at org.python.core.imp.createFromPyClass(imp.java:144)
    at org.python.core.imp.loadFromSource(imp.java:504)
    at org.python.core.imp.find_module(imp.java:410)
    at org.python.core.imp.import_next(imp.java:620)
    at org.python.core.imp.import_first(imp.java:650)
    at org.python.core.imp.import_name(imp.java:741)
    at org.python.core.imp.importName(imp.java:791)
    at org.python.core.ImportFunction.__call__(__builtin__.java:1236)
    at org.python.core.PyObject.__call__(PyObject.java:367)
    at org.python.core.__builtin__.__import__(__builtin__.java:1207)
    at org.python.core.__builtin__.__import__(__builtin__.java:1190)
    at org.python.core.imp.importOne(imp.java:802)
    at logging$py.f$0(c:\home\work\sample\content\Lib\logging\__init__.py:1372)
    at logging$py.call_function(c:\home\work\sample\content\Lib\logging\__init__.py)
    at org.python.core.PyTableCode.call(PyTableCode.java:165)
    at org.python.core.PyCode.call(PyCode.java:18)
    at org.python.core.imp.createFromCode(imp.java:325)
    at org.python.core.imp.createFromPyClass(imp.java:144)
    at org.python.core.imp.loadFromSource(imp.java:504)
    at org.python.core.imp.find_module(imp.java:410)
    at org.python.core.imp.import_next(imp.java:620)
    at org.python.core.imp.import_first(imp.java:650)
    at org.python.core.imp.import_name(imp.java:741)
    at org.python.core.imp.importName(imp.java:791)
    at org.python.core.ImportFunction.__call__(__builtin__.java:1236)
    at org.python.core.PyObject.__call__(PyObject.java:367)
    at org.python.core.__builtin__.__import__(__builtin__.java:1207)
    at org.python.core.__builtin__.__import__(__builtin__.java:1190)
    at org.python.core.imp.importOne(imp.java:802)
    at helloworld.helloworld$py.f$0(c:\home\work\sample\content\helloworld\helloworld.py:19)
    at helloworld.helloworld$py.call_function(c:\home\work\sample\content\helloworld\helloworld.py)
    at org.python.core.PyTableCode.call(PyTableCode.java:165)
    at org.python.core.PyCode.call(PyCode.java:18)
    at org.python.core.imp.createFromCode(imp.java:325)
    at org.python.core.imp.createFromPyClass(imp.java:144)
    at org.python.core.imp.loadFromSource(imp.java:504)
    at org.python.core.imp.find_module(imp.java:410)
    at org.python.core.PyModule.impAttr(PyModule.java:109)
    at org.python.core.imp.import_next(imp.java:622)
    at org.python.core.imp.import_name(imp.java:761)
    at org.python.core.imp.importName(imp.java:791)
    at org.python.core.ImportFunction.__call__(__builtin__.java:1236)
    at org.python.core.PyObject.__call__(PyObject.java:367)
    at org.python.core.__builtin__.__import__(__builtin__.java:1207)
    at org.python.core.imp.importFromAs(imp.java:869)
    at org.python.core.imp.importFrom(imp.java:845)
    at org.python.pycode._pyx1.f$0(<string>:1)
    at org.python.pycode._pyx1.call_function(<string>)
    at org.python.core.PyTableCode.call(PyTableCode.java:165)
    at org.python.core.PyCode.call(PyCode.java:18)
    at org.python.core.Py.runCode(Py.java:1197)
    at org.python.core.Py.exec(Py.java:1241)
    at org.python.util.PythonInterpreter.exec(PythonInterpreter.java:138)

My scripts looks like:

from java.util import Random
from java.util import Date

import sys

print(sys.path)
print(sys.builtin_module_names)

import logging

logging.basicConfig(level=logging.WARNING)
logger1 = logging.getLogger('aaa') 
logger1.warning('************* This message comes from one module') 

def say_hello():
        return 'hello world1'

I've tried the following so far but nothing has worked:

  • Include the zip of the 'Lib' directory in my classpath
  • Hard-coding the 'Lib' path when i setup the interpreter.

If I do it directly from the interactive Jython shell the script works fine (and a logging message appears).

Thanks.

KJQ

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

与酒说心事 2024-09-20 07:34:43

我想现在我已经找到了我自己问题的答案......

基本上,我知道这与我的路径有关,但不知道如何去做。

我最终通过安装程序创建了 Jython jar 的“独立”版本(它包括 /Libs 目录)并使用它。

I think for now i've found an answer to my own question...

Basically, i knew it had something to do with my paths but could not figure out how to do them.

I ended up creating a "standalone" version of the Jython jar through the installer (and it includes the /Libs directory) and using that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文