Python“myfirst.py”无法“导入 mySecond.py”。两个资源都在同一个包“test”中
我的主模块加载了“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)
,我认为这与 .py 文件不在磁盘上,而是从我的 java 类路径加载有关? 欢迎任何想法
更新:
看起来有一个更普遍的问题。 “导入操作系统”甚至失败了。我的路径设置错误。指向 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 导入工作方式的良好来源 此处
My main module was loaded with 'execFile', then i'm trying to import a .py module which is located in the same package as both my Runner java class as the main .py module.
But i'm not succeeding yet. My scenario;
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"
I've tried all kind of things, offsets with test etc. without success.
Below the output when running 'debug' from my netbeans7 java project.
Thought to show the sys.path as well. Maybe it helps getting a solution
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)
I assume it's to do with the .py files not being on disk, but to be loaded from my java classpath?
Any ideas are welcome
UPDATE:
Looks had a more generic problem. An 'import os' even failed. My paths settings were wrong. Pointing to the lib directory seemed to fix it;
PySystemState mPyState = new PySystemState();
mPyState.path.insert(0,new PyString("C:\\jython2.5.1\\Lib"));
PythonInterpreter mInterp = new PythonInterpreter( null, mPyState );
I think this leads to the question;
How can i embed jython in my application without having to install jython251 on my harddrive (thus embedding the c:/jython251/lib directory)?
Found a good source on how importing works for Jython
here
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为您的更新提供答案。
您可以使用包含这些库的独立 Jython.jar。
您可以通过以独立模式安装 jython 来获取此 jar,或者只需复制标准 Jython jar 根目录中的 lib 目录即可。
相关参考:有关分发脚本的 Jython 常见问题解答
An answer for your update.
You can use a standalone Jython.jar that contains the libs.
You can get this jar by installing jython in standalone mode, or simply by copying the lib directory in the root of the standard Jython jar.
A reference on that : Jython FAQ on distributing scripts
听起来 Python 的工作目录将是
test
上面的文件夹,因此您需要提供完整的模块名称 (test.mySecond
),然后您将还需要制作test
一个 Python 模块。为此,只需在test/__init__.py
添加一个文件(可以为空)。It sounds like Python's working directory is going to be the folder above
test
, so you'll need to give the full module name (test.mySecond
), and you'll also need to maketest
a Python module. To do that, just add a file attest/__init__.py
(it can be empty).