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:
10
+
The GraalPy **Maven** and **Gradle** plugins provide functionality to manage Python related resources typically
11
+
required for embedding Python code in Java-based applications:
11
12
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.
13
+
-*Python application files* provided by the user, for example, Python sources which are part of the project.
14
+
-*Third-party Python packages*: installed by the plugin during the build according to the plugin configuration.
15
+
-*The Python standard library*, which is necessary to make Native Image generated executables self-contained.
16
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](https://github.com/oracle/graalpython/blob/master/graalpython/org.graalvm.python.embedding/src/org/graalvm/python/embedding/utils/GraalPyResources.java) provides factory methods
19
-
to create a context suitable for accessing Python embedding relevant resources with a **virtual filessystem** or from a dedicated **external directory**.
17
+
Apart from physically managing and deploying those files, it is also necessary to make them available in Python
18
+
at runtime by configuring the **GraalPy Context** in your Java code accordingly. [GraalPyResources](https://github.com/oracle/graalpython/blob/master/graalpython/org.graalvm.python.embedding/src/org/graalvm/python/embedding/utils/GraalPyResources.java) provides
19
+
factory methods to create a Context preconfigured for accessing Python embedding relevant resources with
20
+
a **Virtual Filesystem** or from a dedicated **external directory**.
21
+
22
+
## Deployment
23
+
24
+
There are two modes how to deploy the resources: as Java resources using the Virtual Filesystem
25
+
to access them in Python, or as an external directory.
20
26
21
27
### Virtual Filesystem
22
28
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. In order to make this work, it is necessary for the resource files to have their resource root directory
28
-
set to `/org.graalvm.python.vfs` which then becomes the virtual filesystem root and is in python code mapped to the
29
-
virtual filesystem mount point, by default `/graalpy_vfs`. For example a python file with the real filesystem path `${project_resources_directory}/org.graalvm.python.vfs/src/foo/bar.py`
30
-
will have at runtime the path `/graalpy_vfs/src/foo/bar.py`.
31
-
32
-
Each kind of the resource files is held in a different project resource directory:
33
-
-`${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,
34
-
only added to the final application file (JAR or native image).
35
-
-`${project_generated_resources_directory}/org.graalvm.python.vfs/venv` - this directory is used for the Python virtual environment holding third-party Python packages
36
-
and its contents are generated and managed entirely by the plugin. Any manual changes may be overwritten.
37
-
-`${project_generated_resources_directory}/org.graalvm.python.vfs/home` - this directory is used for the Standard library
38
-
and its contents are generated and managed entirely by the plugin. Any manual changes may be overwritten.
39
-
40
-
[GraalPyResources](https://github.com/oracle/graalpython/blob/master/graalpython/org.graalvm.python.embedding/src/org/graalvm/python/embedding/utils/GraalPyResources.java) provides factory methods to create a GraalPy context with a default or custom virtual filesystem configuration. For more information on how to configure a virtual filesystem, refer to [VirtualFileSystem.Builder](https://github.com/oracle/graalpython/blob/master/graalpython/org.graalvm.python.embedding/src/org/graalvm/python/embedding/utils/VirtualFileSystem.java).
41
-
42
-
### Resources in an External Directory
43
-
44
-
As an alternative to the virtual filesystem, it is also possible to configure the plugin to hold the resource files in an external directory.
45
-
[GraalPyResources](https://github.com/oracle/graalpython/blob/master/graalpython/org.graalvm.python.embedding/src/org/graalvm/python/embedding/utils/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:
46
-
-`${resources_directory}/src` - is used for Python application files. Contents of this directory are not directly managed by the plugin,
47
-
only added to the final application file (JAR or native image).
48
-
-`${resources_directory}/venv` - this directory is used for the Python virtual environment holding third-party Python packages
49
-
and its contents are generated and managed entirely by the plugin. Any manual changes may be overwritten.
50
-
-`${resources_directory}/home` - this directory is used for the Standard library
51
-
and its contents are generated and managed entirely by the plugin. Any manual changes may be overwritten.
52
-
53
-
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.
54
-
55
-
### GraalPy Context
56
-
57
-
Regarding the particular resource paths, a GraalPy context instance created by factory methods in [GraalPyResources](https://github.com/oracle/graalpython/blob/master/graalpython/org.graalvm.python.embedding/src/org/graalvm/python/embedding/utils/GraalPyResources.java)
58
-
is preconfigured in the following way:
59
-
-`${resources_root_directory}/home` - is reserved for the GraalPy Standard Library. GraalPy context will be configured to
60
-
use this standard library as if set in `PYTHONHOME` environment variable.
61
-
-`${resources_root_directory}/venv` - is reserved for a Python virtual environment holding third-party packages.
62
-
The context will be configured as if it were executed from this virtual environment. Notably packages installed in this
29
+
The Python related resources are embedded in the application file, either in JAR or Native Image generated
30
+
executable, as standard Java resources. The GraalPy Virtual Filesystem accesses resource files as standard
31
+
Java resources and makes them available to Python code running in GraalPy. This is transparent to the Python code,
32
+
which can use standard Python IO to access those files.
33
+
34
+
Java resource files in a Maven or Gradle project are typically located in dedicated resources directories.
35
+
All resources subdirectories named `org.graalvm.python.vfs` are merged and mapped to a configurable
36
+
virtual filesystem mount point at the Python side, by default `/graalpy_vfs`. For example,
37
+
a Python file with the real filesystem path
38
+
`${project_resources_directory}/org.graalvm.python.vfs/src/foo/bar.py` will be accessible as
39
+
`/graalpy_vfs/src/foo/bar.py` in Python.
40
+
41
+
Use the following [GraalPyResources](https://github.com/oracle/graalpython/blob/master/graalpython/org.graalvm.python.embedding/src/org/graalvm/python/embedding/utils/GraalPyResources.java)
42
+
factory methods to create GraalPy Context preconfigured for the use of the Virtual Filesystem:
As an alternative to Java resources with the Virtual Filesystem, it is also possible to configure the Maven
51
+
or Gradle plugin to manage the contents of an external directory, which will **not be embedded** as Java resource
52
+
into the resulting application. The user is then responsible for the deployment of such directory. Python code
53
+
will access the files directly from the real filesystem.
54
+
55
+
Use the following [GraalPyResources](https://github.com/oracle/graalpython/blob/master/graalpython/org.graalvm.python.embedding/src/org/graalvm/python/embedding/utils/GraalPyResources.java)
56
+
factory methods to create GraalPy Context preconfigured for the use of an external directory:
57
+
58
+
*`GraalPyResources.createContextBuilder(Path)`
59
+
60
+
## Conventions
61
+
62
+
The factory methods in [GraalPyResources](https://github.com/oracle/graalpython/blob/master/graalpython/org.graalvm.python.embedding/src/org/graalvm/python/embedding/utils/GraalPyResources.java) rely on the following conventions, where the `${root}` is either
63
+
the external directory, or the Virtual System mount point on the Python side and
64
+
`${project_resources_directory}/org.graalvm.python.vfs` on the real filesystem:
65
+
66
+
-`${root}/src`: used for Python application files. This directory will be configured as the default
67
+
search path for Python module files (equivalent of `PYTHONPATH` environment variable).
68
+
-`${root}/venv`: used for the Python virtual environment holding installed third-party Python packages.
69
+
The Context will be configured as if it were executed from this virtual environment. Notably packages installed in this
63
70
virtual environment will be automatically available for importing.
64
-
-`${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.
71
+
-`${root}/home`: used for the Python standard library (equivalent of `PYTHONHOME` environment variable).
65
72
66
-
where `${resources_root_directory}` is either the virtual filesystem resource root `/org.graalvm.python.vfs` or an external directory.
73
+
The Maven or Gradle plugin will fully manage the contents of the `vevn` and `home` subdirectories.
74
+
Any manual changes in these directories will be overridden by the plugin during the build.
67
75
68
-
### GraalPy Maven Plugin Configuration
76
+
-`${root}/venv`: the plugin creates virtual environment and installs required packages according
77
+
to the plugin configuration in `pom.xml` or `build.gradle`.
78
+
-`${root}/home`: the plugin copies the required (also configurable) parts of the Python standard library into this directory.
79
+
By default, the full standard library is used.
80
+
81
+
The `src` subdirectory is left to be manually populated by the user with custom Python scripts or modules.
82
+
83
+
## GraalPy Maven Plugin Configuration
69
84
70
85
Add the plugin configuration in the `configuration` block of `graalpy-maven-plugin` in the _pom.xml_ file:
71
86
```xml
@@ -90,8 +105,8 @@ The **packages** element declares a list of third-party Python packages to be do
90
105
...
91
106
</configuration>
92
107
```
93
-
- The **pythonHome** subsection declares what parts of the standard library should be added to the final JAR file or native executable.
94
-
108
+
- The **pythonHome** subsection declares what parts of the standard library should be deployed.
109
+
95
110
Each `include` and `exclude` element is interpreted as a Java-like regular expression specifying which file paths should be included or excluded.
96
111
97
112
```xml
@@ -109,15 +124,17 @@ The **packages** element declares a list of third-party Python packages to be do
109
124
...
110
125
</configuration>
111
126
```
112
-
- If the **pythonResourcesDirectory** element is specified, then the given directory is used for GraalPy resources instead of the standard Maven resource directories.
127
+
- If the **pythonResourcesDirectory** element is specified, then the given directory is used
128
+
as [external directory](#external-directory) and no Java resources are embedded.
129
+
Remember to use the appropriate `GraalPyResources` API to create the Context.
Add the plugin configuration in the `GraalPy` block in the _build.gradle_ file:
123
140
The **packages** element declares a list of third-party Python packages to be downloaded and installed by the plugin.
@@ -128,8 +145,8 @@ GraalPy {
128
145
...
129
146
}
130
147
```
131
-
- the **pythonHome** subsection declares what parts of the standard library should be added to the final JAR file or native executable.
132
-
148
+
- the **pythonHome** subsection declares what parts of the standard library should be deployed.
149
+
133
150
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.
134
151
```
135
152
GraalPy {
@@ -140,7 +157,9 @@ GraalPy {
140
157
...
141
158
}
142
159
```
143
-
- If the **pythonResourcesDirectory** element is specified, then the given directory is used for GraalPy resources instead of the standard Gradle resource directories.
160
+
- If the **pythonResourcesDirectory** element is specified, then the given directory is used
161
+
as [external directory](#external-directory) and no Java resources are embedded.
162
+
Remember to use the appropriate `GraalPyResources` API to create the Context.
0 commit comments