java调用python脚本失败,从Java调用Python脚本时出现Python ImportError

First off, there is a similar question here that wasn't ever really resolved.

I have a Python script that I want to execute from within my Java code. When I run the Python script on its own, it works properly. When I try to execute it from a Java process, I get an ImportError:

Traceback (most recent call last):

File "address_to_geocode.py", line 3, in

from omgeo import Geocoder

ImportError: No module named omgeo

Per a suggestion from the linked question, I appended a direct path to the module in my Python import section to make sure the interpreter knows where to look, yet it still won't work:

import sys, os

sys.path.append('/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/omgeo')

from omgeo import Geocoder

My next move was to call the python script from a bash script (which again, works on its own), but when I call the bash script from Java, the same error persists. The issue, therefore seems to be on Java's end. Here is my java code:

Process p = runner.exec("python address_to_geocode.py");

BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));

BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));

String stdReader = null;

//Read output of command:

while((stdReader = stdInput.readLine())!=null) {

System.out.println(stdReader);

}

//Read any command errors:

while((stdReader = stdError.readLine())!=null) {

System.out.println(stdReader);

}

p.waitFor();

Is there anything wrong with my Java code or is this a bug? I appreciate any pointers.

解决方案

I solved it. It looks like ProcessBuilder requires the full path not only to the python file itself, but to python:

ProcessBuilder("/Library/Frameworks/Python.framework/Versions/2.7/bin/python",absolute_file_path);

This solves the issue.

Python中检查SDK(Software Development Kit,软件开发工具包)是否正确安装,通常可以采用以下几种方法: 1. 查找特定命令或模块:通过Python的`subprocess`模块来检查系统中是否包含SDK提供的特定命令。例如,如果SDK中包含一个名为`sdkcommand`的命令行工具,可以使用以下代码检查是否可以调用该命令: ```python import subprocess try: # 尝试执行SDK的命令 subprocess.run(['sdkcommand', '--version'], check=True, capture_output=True, text=True) print("SDK安装正确,版本信息如下:") # 打印命令输出,获取版本信息 print(subprocess.run(['sdkcommand', '--version'], capture_output=True, text=True).stdout) except subprocess.CalledProcessError: print("未能找到SDK命令,可能未正确安装SDK。") ``` 2. 导入模块:如果SDK安装后会提供一些Python模块,可以通过尝试导入这些模块来检查SDK是否安装成功。如果导入失败,可能意味着SDK未安装或安装有问题。 ```python try: import sdkspecificmodule # 假设SDK提供了一个名为sdkspecificmodule的Python模块 print("SDK安装正确,模块 sdkspecificmodule 可以被导入。") except ImportError: print("无法导入sdkspecificmodule,可能SDK未正确安装。") ``` 3. 环境变量检查:有些SDK安装后会设置特定的环境变量,可以检查这些环境变量来确认SDK是否安装正确。 ```python import os # 假设SDK安装后会设置名为SDK_HOME的环境变量 sdk_home = os.getenv('SDK_HOME') if sdk_home: print(f"SDK_HOME环境变量存在,路径为:{sdk_home},SDK可能已正确安装。") else: print("未找到SDK_HOME环境变量,SDK可能未正确安装。") ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值