gpt4 book ai didi

java - Python "myfirst.py"失败到 "import mySecond.py"。两种资源都在同一个包中 "test"

转载 作者:行者123 更新时间:2023-11-29 06:13:10 25 4
gpt4 key购买 nike

我的主模块加载了“execFile”,然后我尝试导入一个 .py 模块,它与我的 Runner java 类位于同一个包中作为主 .py 模块。

但我还没有成功。我的场景;

My package structure:
/
/test/
/test/Runner.java
/test/myfirst.py
/test/mySecond.py
/test/__init__.py

In Runner.java:
InputStream mPython = getClass().getClassLoader().getResourceAsStream("test/myFirst.py" );
PythonInterpreter mInterp = new PythonInterpreter();
mInterp.execfile( mPython );

In myfirst.py
import sys
print sys.path
import mySecond
mySecond.hello()

In mySecond.py
def hello():
print "hi"

我尝试了各种方法,用测试抵消等,但都没有成功。

从我的 netbeans7 java 项目运行“调试”时的输出下方。也想显示 sys.path 。也许它有助于获得解决方案

debug:
['D:\\....\\ext\\Lib', '__classpath__', '__pyclasspath__/']
Exception in thread "main" Traceback (most recent call last):
File "<iostream>", line 3, in <module>
ImportError: No module named mySecond
Java Result: 1
BUILD SUCCESSFUL (total time: 4 seconds)

我认为这与不在磁盘上但要从我的 java 类路径加载的 .py 文件有关?欢迎任何想法

更新:

看起来有一个更普遍的问题。 “导入操作系统”甚至失败了。我的路径设置错误。指向 lib 目录似乎可以修复它;

PySystemState mPyState = new PySystemState(); 
mPyState.path.insert(0,new PyString("C:\\jython2.5.1\\Lib"));
PythonInterpreter mInterp = new PythonInterpreter( null, mPyState );

我认为这引出了问题;

如何在我的应用程序中嵌入 jython 而无需在我的硬盘驱动器上安装 jython251(从而嵌入 c:/jython251/lib 目录)?

找到了有关 Jython 导入工作原理的良好来源 here

最佳答案

更新的答案。您可以使用包含库的独立 Jython.jar。您可以通过在独立模式下安装 jython 来获取此 jar,或者只需复制标准 Jython jar 根目录中的 lib 目录即可。对此的引用:Jython FAQ on distributing scripts

关于java - Python "myfirst.py"失败到 "import mySecond.py"。两种资源都在同一个包中 "test",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6058945/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com