2012-04-26 49 views
2

一個簡單的問題......我使用Jython來完成一些任務,並且我需要訪問一些MySQL數據庫......我找到了一個用於Python2.5的MySQL數據庫連接器;它不包含jython/python安裝(它是第三方)。我將模塊的文件夾放在與main.py相同的目錄中,但是當我啓動它時,它告訴我它找不到該模塊。我之前在Python3.2中完成了導入操作,我認爲它的工作原理是一樣的。在Jython中導入非標準的Python模塊

Jython可以導入非標準的python模塊嗎? - 注意我可以很好地導入Java類。

這裏是錯誤(是的,這是我的世界):

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

由於您使用的Jython,使用mysql connector/J罐子。下載它,將它添加到你的CLASSPATH,並確保你的用戶執行代碼已經讀取了jar的位置的權限。

一旦你有了罐子,請按照the documentation獲取示例代碼。

+0

那麼有沒有辦法導入非標準庫? – Clement 2012-04-26 19:59:44

+0

通常,您可以使用大多數模塊,但數據庫連接器通常是針對實現的庫進行編譯的 - 這就是爲什麼建議使用本機庫。 – 2012-04-26 20:01:32

+0

噢好吧。謝謝!使用Java版本會有多大的不同? – Clement 2012-04-26 20:05:20

4

jython可以導入非標準的python模塊,如果它們是純python。數據庫連接通常是cpython模塊。對於數據庫連接,請參閱zxJDBC它使得java可用的任何jdbc連接都以標準的python DBI 2.0方式運行。

兩全其美!

3

PyDbLite是表面上純粹的python,所以它應該導入到jython中。我沒有嘗試使用Jython的setup.py,但是我已經從Jython中導入了幾個不包含Jython的純python .py。我有一個項目使用通常的導入工具在CPython 2.x,CPython 3.x,Pypy和Jython上運行。

想到的主要問題是:PyDbLite是否在sys.path的目錄中?

+0

它在目錄user1277476中。腳本所在的同一目錄。 – Clement 2012-04-27 10:38:27