问题描述
我是python的新手,并且已经安装了Jython2.7.0
Java
import org.python.util.PythonInterpreter;
import org.python.core.*;
public class Main {
public static void main(String[] args) {
PythonInterpreter interp = new PythonInterpreter();
interp.execfile("D:/Users/JY/Desktop/test/for_java_test.py");
interp.close();
}
}
Python
import pandas as pd
import ctypes
def main():
data = pd.read_csv('for_test.csv')
data_mean = data.a*2
data_mean.to_csv('catch_test.csv',index=False)
ctypes.windll.user32.MessageBoxW(0, "Done. Output: a * 2", "Output csv", 0)
if __name__ == '__main__':
main()
然后我得到了这个错误.
Exception in thread "main" Traceback (most recent call last):
File "D:\Users\JYJU\Desktop\test_java\for_java_test.py", line 1, in <module>
import pandas as pd
ImportError: No module named pandas
如果我想使用熊猫怎么解决?
您目前无法在Jython中使用Pandas,因为它依赖于CPython特定的本机扩展.一个依赖项是NumPy,另一个依赖项是Cython(实际上这不是本地的CPython扩展,但会生成此类依赖项.)
关注 JyNI项目("Jython本机界面").它使Jython可以使用本机CPython扩展,其确切目的是解决诸如您遇到的问题.但是,它仍处于繁重的开发过程中,尚无法将Pandas或NumPy加载到Jython中,但是这两个框架都在优先级列表中居高不下.
(例如ctypes已经在某种程度上起作用了.)
此外,它目前仅适用于POSIX(在Linux和OSX上进行了测试).
如果您不需要Jython,而只需要Java/Pandas互操作,则已经可行的解决方案是嵌入CPython解释器. JPY 和 JEP 是提供此功能的项目.借助它们中的任何一个,您都应该能够互操作Java和Pandas(或任何其他特定于CPython的框架).
I'm new to python, and I've install Jython2.7.0
Java
import org.python.util.PythonInterpreter;
import org.python.core.*;
public class Main {
public static void main(String[] args) {
PythonInterpreter interp = new PythonInterpreter();
interp.execfile("D:/Users/JY/Desktop/test/for_java_test.py");
interp.close();
}
}
Python
import pandas as pd
import ctypes
def main():
data = pd.read_csv('for_test.csv')
data_mean = data.a*2
data_mean.to_csv('catch_test.csv',index=False)
ctypes.windll.user32.MessageBoxW(0, "Done. Output: a * 2", "Output csv", 0)
if __name__ == '__main__':
main()
Then I got this error.
Exception in thread "main" Traceback (most recent call last):
File "D:\Users\JYJU\Desktop\test_java\for_java_test.py", line 1, in <module>
import pandas as pd
ImportError: No module named pandas
How can I fix this if I want to use pandas?
You currently cannot use Pandas with Jython, because it depends on CPython specific native extensions. One dependency is NumPy, the other is Cython (which is actually not a native CPython extension, but generates such).
Keep an eye on the JyNI project ("Jython Native Interface").It enables Jython to use native CPython-extensions and its exact purpose is to solve issues like that encountered by you.However, it is still under heavy development and not yet capable of loading Pandas or NumPy into Jython, but both frameworks are high on the priority list.
(E.g. ctypes is already working to some extend.)
Also, it is currently POSIX only (tested on Linux and OSX).
If you wouldn't require Jython specifically, but just some Java/Pandas interoperation, an already workable solution would be to embed the CPython interpreter.JPY and JEP are projects that provide this. With either of them you should be able to interoperate Java and Pandas (or any other CPython-specific framework).
这篇关于如何使用Jython导入 pandas 的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!