python查询模块路径,如何检查Jython ScriptEngine查找python模块的类路径?

I have this Java code which I am using to run a python script using Jython ScriptEngine:

StringWriter writer = new StringWriter();

ScriptEngineManager manager = new ScriptEngineManager();

ScriptContext context = new SimpleScriptContext();

context.setWriter(writer);

ScriptEngine engine = manager.getEngineByName("python");

engine.eval(new FileReader("/Users/folder1/test.py"), context);

In my python script there are several module import statements and when I run the Java code I get error as javax.script.ScriptException: ImportError: No module named psycopg2. All the modules are installed in my machine and when I run the python script normally through CLI it executes. So my understanding is that Jython classpath is looking somewhere else for the python modules.

How can I check where does the Jython ScriptEngine looks for modules and then modify it include where actually my python modules are present? I am new to this so please forgive any lack of understanding.

Note: I have CentOS and python 2.7.5 installed on my machine

解决方案

sys.path is a list of strings that specifies where Jython (and Python) searches for modules. You can check its value like so:

engine.eval("import sys; print sys.path");

To add a directory to sys.path, use the JYTHONPATH environment variable. If yourmodule is installed in /path/to/modules/yourmodule, it would look like this:

export JYTHONPATH=/path/to/modules

Another way is to use the python.path property.

Unfortunately, in the case of psycopg2 the above won't help since that package is a C extension and therefore only compatible with CPython. Perhaps you can use the port of Psycopg for Ctypes instead.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值