Import non-standard python module in Jython

2020-04-16 04:37发布

A quick question... I'm using Jython to do some tasks, and I need to access some MySQL Databases... I've found a MySQL Database Connector for Python2.5; it doesn't come with the jython/python installation (it's 3rd party). I've placed the module's folder in the same directory as my main.py, but when I launch it, it tells me that it can't find the module. I've done this before in Python3.2 for importing and I thought it would work the same.

Can Jython import non-standard python modules? -- Note I can import Java classes just fine.

Here is the Error (Yes, this is for Minecraft):

20:50:44 [SEVERE] org.bukkit.plugin.InvalidPluginException: Traceback (most recent call last):
  File "<iostream>", line 10, in <module>
ImportError: No module named PyDbLite

20:50:44 [SEVERE]   at net.lahwran.bukkit.jython.PythonPluginLoader.loadPlugin(PythonPluginLoader.java:280)
20:50:44 [SEVERE]   at net.lahwran.bukkit.jython.PythonPluginLoader.loadPlugin(PythonPluginLoader.java:113)
20:50:44 [SEVERE]   at net.lahwran.bukkit.jython.PythonPluginLoader.loadPlugin(PythonPluginLoader.java:83)
20:50:44 [SEVERE]   at org.bukkit.plugin.SimplePluginManager.loadPlugin(SimplePluginManager.java:305)
20:50:44 [SEVERE]   at com.master.bukkit.python.PythonLoader.onLoad(PythonLoader.java:58)
20:50:44 [SEVERE]   at org.bukkit.craftbukkit.CraftServer.loadPlugins(CraftServer.java:212)
20:50:44 [SEVERE]   at org.bukkit.craftbukkit.CraftServer.<init>(CraftServer.java:183)
20:50:44 [SEVERE]   at net.minecraft.server.ServerConfigurationManager.<init>(ServerConfigurationManager.java:53)
20:50:44 [SEVERE]   at net.minecraft.server.MinecraftServer.init(MinecraftServer.java:156)
20:50:44 [SEVERE]   at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:422)
20:50:44 [SEVERE]   at net.minecraft.server.ThreadServerApplication.run(SourceFile:492)
20:50:44 [SEVERE] Caused by: Traceback (most recent call last):
  File "<iostream>", line 10, in <module>
ImportError: No module named PyDbLite

20:50:44 [SEVERE]   at org.python.core.PyException.fillInStackTrace(PyException.java:70)
20:50:44 [SEVERE]   at java.lang.Throwable.<init>(Throwable.java:181)
20:50:44 [SEVERE]   at java.lang.Exception.<init>(Exception.java:29)
20:50:44 [SEVERE]   at java.lang.RuntimeException.<init>(RuntimeException.java:32)
20:50:44 [SEVERE]   at org.python.core.PyException.<init>(PyException.java:46)
20:50:44 [SEVERE]   at org.python.core.PyException.<init>(PyException.java:43)
20:50:44 [SEVERE]   at org.python.core.PyException.<init>(PyException.java:61)
20:50:44 [SEVERE]   at org.python.core.Py.ImportError(Py.java:290)
20:50:44 [SEVERE]   at org.python.core.imp.import_first(imp.java:750)
20:50:44 [SEVERE]   at org.python.core.imp.import_name(imp.java:834)
20:50:44 [SEVERE]   at org.python.core.imp.importName(imp.java:884)
20:50:44 [SEVERE]   at org.python.core.ImportFunction.__call__(__builtin__.java:1220)
20:50:44 [SEVERE]   at org.python.core.PyObject.__call__(PyObject.java:357)
20:50:44 [SEVERE]   at org.python.core.__builtin__.__import__(__builtin__.java:1173)
20:50:44 [SEVERE]   at org.python.core.imp.importOne(imp.java:903)
20:50:44 [SEVERE]   at org.python.pycode._pyx5.f$0(<iostream>:52)
20:50:44 [SEVERE]   at org.python.pycode._pyx5.call_function(<iostream>)
20:50:44 [SEVERE]   at org.python.core.PyTableCode.call(PyTableCode.java:165)
20:50:44 [SEVERE]   at org.python.core.PyCode.call(PyCode.java:18)
20:50:44 [SEVERE]   at org.python.core.Py.runCode(Py.java:1261)
20:50:44 [SEVERE]   at org.python.util.PythonInterpreter.execfile(PythonInterpreter.java:235)
20:50:44 [SEVERE]   at org.python.util.PythonInterpreter.execfile(PythonInterpreter.java:230)
20:50:44 [SEVERE]   at net.lahwran.bukkit.jython.PythonPluginLoader.loadPlugin(PythonPluginLoader.java:239)
20:50:44 [SEVERE]   ... 10 more

3条回答
孤傲高冷的网名
2楼-- · 2020-04-16 05:18

jython can import non-standard python modules if they are pure python. database connections are typically cpython modules. For database connectivity, see zxJDBC It makes any jdbc connectivity available to java behave in the standard python DBI 2.0 kind of way.

Best of both worlds!

查看更多
Emotional °昔
3楼-- · 2020-04-16 05:27

Since you are using jython, use the mysql connector/J jar. Download it, add it to your CLASSPATH, and make sure your user executing the code has read permissions to the location where the jar is.

Once you have the jar, follow the documentation for sample code.

查看更多
一纸荒年 Trace。
4楼-- · 2020-04-16 05:27

PyDbLite is ostensibly pure-python, so it should import into jython OK. I've not tried to use a setup.py with Jython, but I've imported several pure-python .py's that didn't ship with Jython, from Jython. I have one project that runs on CPython 2.x, CPython 3.x, Pypy and Jython using the usual import machinery.

The main question that comes to mind is: Is PyDbLite in a directory on your sys.path?

查看更多
登录 后发表回答