Java调用python脚本

1 概述

项目遇到需要用Java调用Python脚本,记录中间遇到的问题及解决方案。

2 过程问题归纳

  1. 直接使用Java自带RunTime,运行简单脚本没问题,例如加法脚本,这类不需要第三方库,但是比较复杂的,就会出错经常出现No named module xxx
    解决:
    (1)安装配置python环境,最好使用Anaconda,我使用的的 Anaconda3(python3.8),具体见链接链接:https://pan.baidu.com/s/1YH9eCQG1FJs-avlI7141fA
    提取码:txvv
    **注意:**不同版本的Anaconda3,适配的windows系统不同,此处使用于windows7,有的必须要求Windows10。笔者拿202205版本的Anaconda3安装,最后一步,总是不成功,出现:Failed to create menu(大概如此),查看错误detail,解压失败缺少xxx.dll文件,换成202105的Anaconda3,直接安装成功。最后查看官网,明确提示有windows版本要求!以后多看官网啊。
  2. 环境变量配置
    在系统环境变量的path中,添加:
    D:\anaconda
    D:\anaconda\Scripts
    D:\anaconda\Library\bin
    D:\anaconda\Library\mingw-w64\bin
  3. 安装完Anaconda3后,再次调用脚本,出现Cannot run program “python”: CreateProcess error=2…
    解决:是Python配置环境问题,直接在RunTime调用时,写出python.exe路径:
    String[] args1 = new String[] { “D:\Anaconda3\python.exe”, “C:\project\pytools\runpythonfile.py”};
  4. 出现ImportError: Unable to import required dependencies: numpy。笔者在IDEA中配置了本地的Anaconda3的python环境变量,执行脚本没问题,但使用Java调用,就出现这个问题,可能也是环境的问题,我的大python,我的大环境啊。。。。
    解决:使用pip命令查看所有包,发现有numpy,在安装目录Anaconda3\Lib\site-packages,也是能看到numpy包的。可能版本不对,pip命令卸载,重新下载适配python3.8的numpy,再次pip安装,解决!
    提示:当时笔者不知道具体python版本怎么和包的版本对应,其实下载的时候,是可以根据python版本,进行包的筛选的!!!
    Java调用python脚本_第1张图片
  5. pip安装下载的包,出现whl is not a supported wheel on this platform
    解决:包的版本不适合当前pip环境
    (1)使用命令,查看当前pip支持的包环境
    pip debug --verbose

C:\Users\a5758>pip debug --verbose
WARNING: This command is only meant for debugging. Do not use this with automation for parsing and getting these details, since the output and options of this command may change without notice.
pip version: pip 22.0.4 from C:\Python310\lib\site-packages\pip (python 3.10)
sys.version: 3.10.4 (tags/v3.10.4:9d38120, Mar 23 2022, 23:13:41) [MSC v.1929 64 bit (AMD64)]
sys.executable: C:\Python310\python.exe
sys.getdefaultencoding: utf-8
sys.getfilesystemencoding: utf-8
locale.getpreferredencoding: cp936
sys.platform: win32
sys.implementation:
  name: cpython
'cert' config value: Not specified
REQUESTS_CA_BUNDLE: None
CURL_CA_BUNDLE: None
pip._vendor.certifi.where(): C:\Python310\lib\site-packages\pip\_vendor\certifi\cacert.pem
pip._vendor.DEBUNDLED: False
vendored library versions:
  CacheControl==0.12.10
  colorama==0.4.4
  distlib==0.3.3
  distro==1.6.0
  html5lib==1.1
  msgpack==1.0.3 (Unable to locate actual module version, using vendor.txt specified version)
  packaging==21.3
  pep517==0.12.0
  platformdirs==2.4.1
  progress==1.6
  pyparsing==3.0.7
  requests==2.27.1
  certifi==2021.10.08
  chardet==4.0.0
  idna==3.3
  urllib3==1.26.8
  rich==11.0.0 (Unable to locate actual module version, using vendor.txt specified version)
  pygments==2.11.2
  typing_extensions==4.0.1 (Unable to locate actual module version, using vendor.txt specified version)
  resolvelib==0.8.1
  setuptools==44.0.0 (Unable to locate actual module version, using vendor.txt specified version)
  six==1.16.0
  tenacity==8.0.1 (Unable to locate actual module version, using vendor.txt specified version)
  tomli==1.0.3
  webencodings==0.5.1 (Unable to locate actual module version, using vendor.txt specified version)
Compatible tags: 36
  cp310-cp310-win_amd64
  cp310-abi3-win_amd64
  cp310-none-win_amd64
  cp39-abi3-win_amd64
  cp38-abi3-win_amd64
  cp37-abi3-win_amd64
  cp36-abi3-win_amd64
  cp35-abi3-win_amd64
  cp34-abi3-win_amd64
  cp33-abi3-win_amd64
  cp32-abi3-win_amd64
  py310-none-win_amd64
  py3-none-win_amd64
  py39-none-win_amd64
  py38-none-win_amd64
  py37-none-win_amd64
  py36-none-win_amd64
  py35-none-win_amd64
  py34-none-win_amd64
  py33-none-win_amd64
  py32-none-win_amd64
  py31-none-win_amd64
  py30-none-win_amd64
  cp310-none-any
  py310-none-any
  py3-none-any
  py39-none-any
  py38-none-any
  py37-none-any
  py36-none-any
  py35-none-any
  py34-none-any
  py33-none-any
  py32-none-any
  py31-none-any
  py30-none-any

Compatible tags: 36,支持36中包环境,然后下载对应的包即可。
补充:
Java调用python脚本_第2张图片

6,提示:凡是执行脚本出现No named module xxx,要么是缺少包,要么是包的版本和python的版本对不上。

完!

你可能感兴趣的:(java,python)