GraalVM 安装及运行Python


关于 GraalVM

GraalVM : a high-performance JDK distribution It is designed to accelerate the execution of applications written in Java and other JVM languages while also providing runtimes for JavaScript, Ruby, Python, and a number of other popular languages. GraalVM’s polyglot capabilities make it possible to mix multiple programming languages in a single application while eliminating any foreign language call costs.

    官网 Github 官方文档

GraalVM Architecture


安装 GraalVM

点击安装社区版,会跳转到 Github 进行下载,我并非 Arm 架构,这里选择 amd64


得到一个压缩包,解压后,就是安装包文件。


macOS 10.15 执行下述命令

$ sudo xattr -r -d com.apple.quarantine  /Users/luyi/Documents/software/graalvm-ce-java17-22.1.0 
Password:

测试

# 执行文件
/../Downloads/graalvm-ce-java17-22.1.0/Contents/Home/bin/java --version
openjdk 17.0.3 2022-04-19
OpenJDK Runtime Environment GraalVM CE 22.1.0 (build 17.0.3+7-jvmci-22.1-b06)
OpenJDK 64-Bit Server VM GraalVM CE 22.1.0 (build 17.0.3+7-jvmci-22.1-b06, mixed mode, sharing)

添加环境变量 我目前使用 bash

修改 ~/.bash_profile 添加如下行

HOME_GRAALVM=/Users/luyi/Documents/software/graalvm-ce-java17-22.1.0/Contents/Home/bin
PATH=$PATH:$HOME_GRAALVM

GraalVM Python

官方说明


GraalVM 提供 Python 3.8 的运行时。 GraalVM Python 运行时的一个主要目标是支持 SciPy 等科学计算、机器学习相关库。


$ gu install native-image

# 安装 python 
$ gu install python

使用 ginstall 安装第三方包

ginstall 可以用来安装第三方包

# 创建虚拟环境 venv1(耗时较长)
$ graalpython -m venv venv1

# 激活/进入 venv1
$ source venv1/bin/activate

# 查看可安装的 库
(venv1) $ graalpython -m ginstall install --help
usage: ginstall.py install [-h] [--prefix PREFIX] [--user] package

Install a known package. Known packages are:
Cython
Keras_preprocessing
Markdown
Pillow
PyYAML
...
wcwidth
wheel
zipp

positional arguments:
  package          comma-separated list

optional arguments:
  -h, --help       show this help message and exit
  --prefix PREFIX  user-site path prefix
  --user           install into user site


# 安装 pandas
(venv1) $ graalpython -m ginstall install pandas

# 也可以使用 pip 安装
(venv1) pip install PyYAML

graalpython 交互模式下执行 python 代码

(venv1) $ which python
/Users/luyi/Documents/code/grvenv/venv1/bin/python
 
(venv1) $ graalpython
Python 3.8.5 (Fri Apr 22 06:38:03 PDT 2022)
[Graal, GraalVM CE, Java 17.0.3] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys, os
>>> os.path.dirname(sys.executable)
/Users/xx/Documents/code/grvenv/venv1/bin
>>>

2022-05-17(二)晴天

经验分享 程序员 微信小程序 职场和发展