You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The GraalPy Maven and Gradle plugins provide functionality to manage resources required for embedding Python in Java-based applications:
11
+
12
+
This embedding relevant resources can be one of the following:
13
+
- Python application files, for example, Python sources which are part of the project.
14
+
- Third-party Python packages which can be accessed either from the projects Python sources or from Java.
15
+
- The Standard library which is necessary to make a generated native executable self-contained.
16
+
17
+
Besides physically managing and distributing this files by the plugins, it is also necessary to make them available at runtime by accordingly
18
+
configuring the GraalPy context in your Java code. `GraalPyResources.java` TODO - how link to file/javadoc? provides factory methods
19
+
to create a **GraalPy context** suitable for accessing Python embedding relevant resources with a **virtual filessystem** or from a dedicated **external directory**.
20
+
21
+
### Virtual filesystem
22
+
23
+
Resource files in a Maven or Gradle project are typically located in dedicated project resource directories, and then distributed in
24
+
one application file (JAR or native image).
25
+
26
+
The GraalPy virtual filesystem can access resource files as standard Java resources and make them available to Python code running in GraalPy so
27
+
that it can use standard Python IO to access those files.
28
+
29
+
Each kind of the resource files is held in a different project resource directory:
30
+
-`${project_resources_directory}/org.graalvm.python.vfs/src` - is used for Python application files. Contents of this directory are not directly managed by the plugin,
31
+
only added to the final application file (JAR or native image).
32
+
-`${project_generated_resources_directory}/org.graalvm.python.vfs/venv` - this directory is used for the Python virtual environment holding third-party Python packages
33
+
and its contents are generated and managed entirely by the plugin. Any manual changes may be overwritten.
34
+
-`${project_generated_resources_directory}/org.graalvm.python.vfs/home` - this directory is used for the Standard library
35
+
and its contents are generated and managed entirely by the plugin. Any manual changes may be overwritten.
36
+
37
+
`GraalPyResources.java` provides factory methods to create a GraalPy context with a default or custom virtual filesystem configuration.
38
+
For more information on how to configure a virtual filesystem, refer to `VirtualFileSystem.Builder` TODO - how link to file/javadoc?.
39
+
40
+
### Resources in an external directory
41
+
42
+
As an alternative to the virtual filesystem, it is also possible to configure the plugin to hold the resource files in an external directory.
43
+
`GraalPyResources.java` provides methods to create a GraalPy context preconfigured to work with a dedicated resources directory as long at it has the following structure:
44
+
-`${resources_directory}/src` - is used for Python application files. Contents of this directory are not directly managed by the plugin,
45
+
only added to the final application file (JAR or native image).
46
+
-`${resources_directory}/venv` - this directory is used for the Python virtual environment holding third-party Python packages
47
+
and its contents are generated and managed entirely by the plugin. Any manual changes may be overwritten.
48
+
-`${resources_directory}/home` - this directory is used for the Standard library
49
+
and its contents are generated and managed entirely by the plugin. Any manual changes may be overwritten.
50
+
51
+
Note, that by storing the resource files in an external directory, they are not bundled with the application file (JAR or native image) and must be provided separately.
52
+
53
+
### GraalPy Context
54
+
55
+
Regarding the particular resource paths, a GraalPy context instance created by factory methods in `GraalPyResources` is preconfigured in the following way:
56
+
-`${resources_root_directory}/home` - is reserved for the GraalPy Standard Library. GraalPy context will be configured to
57
+
use this standard library as if set in `PYTHONHOME` environment variable.
58
+
-`${resources_root_directory}/venv` - is reserved for a Python virtual environment holding third-party packages.
59
+
The context will be configured as if it were executed from this virtual environment. Notably packages installed in this
60
+
virtual environment will be automatically available for importing.
61
+
-`${resources_root_directory}/src` - is reserved for python application files. GraalPy context will be configured to see those files as if set in `PYTHONPATH` environment variable.
62
+
63
+
where `${resources_root_directory}` is either the virtual filesystem resource root `/org.graalvm.python.vfs` or an external directory.
64
+
65
+
### GraalPy Maven Plugin Configuration
66
+
67
+
Add the plugin configuration in the `configuration` block of `graalpy-maven-plugin` in the _pom.xml_ file:
68
+
```xml
69
+
<plugin>
70
+
<groupId>org.graalvm.python</groupId>
71
+
<artifactId>graalpy-maven-plugin</artifactId>
72
+
...
73
+
<configuration>
74
+
...
75
+
</configuration>
76
+
...
77
+
```
78
+
79
+
The **packages** element declares a list of third-party Python packages to be downloaded and installed by the plugin.
80
+
- The Python packages and their versions are specified as if used with `pip`.
81
+
```xml
82
+
<configuration>
83
+
<packages>
84
+
<package>termcolor==2.2</package>
85
+
...
86
+
</packages>
87
+
...
88
+
</configuration>
89
+
```
90
+
- The **pythonHome** subsection declares what parts of the standard library should be added to the final JAR file or native executable.
91
+
92
+
Each `include` and `exclude` element is interpreted as a Java-like regular expression specifying which file paths should be included or excluded.
93
+
94
+
```xml
95
+
<configuration>
96
+
<pythonHome>
97
+
<includes>
98
+
<include>.*</include>
99
+
...
100
+
</includes>
101
+
<excludes>
102
+
<exclude></exclude>
103
+
...
104
+
</excludes>
105
+
</pythonHome>
106
+
...
107
+
</configuration>
108
+
```
109
+
- If the **pythonResourcesDirectory** element is specified, then the given directory is used for GraalPy resources instead of the standard Maven resource directories.
Add the plugin configuration in the `GraalPy` block in the _build.gradle_ file:
120
+
The **packages** element declares a list of third-party Python packages to be downloaded and installed by the plugin.
121
+
- The Python packages and their versions are specified as if used with `pip`.
122
+
```
123
+
GraalPy {
124
+
packages = ["termcolor==2.2"]
125
+
...
126
+
}
127
+
```
128
+
- the **pythonHome** subsection declares what parts of the standard library should be added to the final JAR file or native executable.
129
+
130
+
Each element in the `includes` and `excludes` list is interpreted as a Java-like regular expression specifying which file paths should be included or excluded.
131
+
```
132
+
GraalPy {
133
+
pythonHome {
134
+
includes = [".*"]
135
+
excludes = []
136
+
}
137
+
...
138
+
}
139
+
```
140
+
- If the **pythonResourcesDirectory** element is specified, then the given directory is used for GraalPy resources instead of the standard Gradle resource directories.
Copy file name to clipboardExpand all lines: docs/user/README.md
+66-32Lines changed: 66 additions & 32 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -18,16 +18,15 @@ Other build systems (Ant, Make, CMake, ...) can also be used with a bit more man
18
18
19
19
GraalPy can generate a Maven project that embeds Python packages into a Java application using [Maven artefacts](https://mvnrepository.com/artifact/org.graalvm.python).
20
20
21
-
22
21
1. Since version 24.0, the GraalPy project publishes a Maven archetype to generate a starter project:
2. Build a native executable using the [GraalVM Native Image](https://www.graalvm.org/latest/reference-manual/native-image/) plugin that was added for you automatically:
29
+
2. Build a native executable using the [GraalVM Native Image "tool"](https://www.graalvm.org/latest/reference-manual/native-image/) plugin that was added for you automatically:
31
30
```bash
32
31
mvn -Pnative package
33
32
```
@@ -38,9 +37,10 @@ GraalPy can generate a Maven project that embeds Python packages into a Java app
38
37
```
39
38
The application prints "hello java" to the console.
40
39
41
-
The project uses the [GraalVM SDK Polyglot API](https://www.graalvm.org/sdk/javadoc/org/graalvm/polyglot/package-summary.html) with additional features to manage Python virtual environments and integrate Python package dependencies with a Maven workflow.
40
+
The project uses the [GraalVM Polyglot API](https://www.graalvm.org/sdk/javadoc/org/graalvm/polyglot/package-summary.html) with additional features to manage Python virtual environments and integrate Python package dependencies with a Maven workflow.
42
41
The Java code and the _pom.xml_ file are heavily documented and the generated code describes available features.
43
-
(If you do not wish to use Maven, the archetype Java code also provides guidance to create a custom embedding.)
42
+
43
+
See also [Embedding Build Tools](Embedding-Build-Tools.md#graalpy-maven-plugin) for more information about the GraalPy Maven Plugin.
44
44
45
45
### Creating Cross-platform JARs with Native Python Packages
46
46
@@ -88,34 +88,14 @@ In order to distribute the resulting application for other systems, follow these
88
88
```
89
89
90
90
2. Open your project configuration file, _app/build.gradle_, and modify it as follows.
91
-
- Include the GraalPy support and the [GraalVM SDK Polyglot API](https://www.graalvm.org/sdk/javadoc/org/graalvm/polyglot/package-summary.html) in the `dependencies` section:
- We recommend you use the Java modules build. Add the appropriate plugin to the `plugins` section:
99
-
```
100
-
id("org.javamodularity.moduleplugin") version "1.8.12"
101
-
```
91
+
- Include the GraalPy support and the [GraalVM Polyglot API](https://www.graalvm.org/sdk/javadoc/org/graalvm/polyglot/package-summary.html) in the `dependencies` section:
102
92
103
-
- To run the application as a module rather than from the classpath, edit the `application` section to look like this:
3. Create a new file named _app/src/main/java/module-info.java_ with the following contents:
112
-
```java
113
-
module interop {
114
-
requires org.graalvm.polyglot;
115
-
}
116
-
```
117
-
118
-
4. Finally, replace the code in the file named _App.java_ as follows for a small Python embedding:
98
+
3. Finally, replace the code in the file named _App.java_ as follows for a small Python embedding:
119
99
```java
120
100
package interop;
121
101
@@ -130,14 +110,68 @@ In order to distribute the resulting application for other systems, follow these
130
110
}
131
111
```
132
112
133
-
5. Run the application with Gradle:
113
+
4. Run the application with Gradle:
134
114
```bash
135
115
./gradlew run
136
116
```
137
117
The application prints "Hello Python!" to the console.
138
118
139
119
> Note: The performance of the GraalPy runtime depends on the JDK in which you embed it. For more information, see [Runtime Optimization Support](https://www.graalvm.org/latest/reference-manual/embed-languages/#runtime-optimization-support).
140
120
121
+
5. Optionally, you can also use a third-party Python package:
122
+
123
+
5.1. In _app/build.gradle_:
124
+
- add the graalpy-gradle-plugin to the `plugins` section:
125
+
```
126
+
id "org.graalvm.python"
127
+
```
128
+
129
+
- configure the GraalPy Gradle plugin:
130
+
```
131
+
graalPy {
132
+
packages = ["termcolor==2.2"]
133
+
}
134
+
```
135
+
136
+
5.2, In _settings.gradle_, add the following build script configuration.
137
+
Note that all buildscript blocks must appear before any plugins blocks.
0 commit comments