Method for configuring Python runtime environment in Bazel tool chain
By creating and configuring shell scripts in the Bazel toolchain, the problem of the inability to mix and specify Python runtime environments in the existing technology is solved, achieving higher construction flexibility and portability.
Patent Information
- Application Number
- CN202411987880.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-12-31
- Publication Date
- 2025-05-13
AI Technical Summary
The existing Bazel Python toolchain cannot mix and specify the Python runtime environment, resulting in low flexibility in software construction and affecting the portability of the built software.
By creating a first shell script and a second shell script, respectively, used to replace the image of the executing process with a Python interpreter or portable Python runtime on the system, and pass command line parameters. These scripts are used to configure the Bazel repository and specify configurations for different Python versions in Bazel's pyruntime rules to use the Python runtime on the system or the portable Python runtime.
It implements the mixed and specified different versions of Python runtime environment in the Bazel toolchain, which improves the flexibility and portability of Python project construction.
Smart Images

Figure CN119987889A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of software construction and configuration, and in particular to a method for configuring a Python runtime environment in a Bazel tool chain. Background Art
[0002] When specifying the corresponding versions of the runtime environment for different versions of Python, the existing Bazel Python toolchain can only specify all versions of Python as Python runtimes installed in the system directory or all as portable Pythons that do not require installation. This hinders the construction and release of Python projects: if a version of Python in the Bazel toolchain does not support the portable runtime environment, then all versions of Python must be built with the installed runtime environment, reducing the flexibility of software construction and the portability of the built software, and may even make it impossible to build or release certain versions of Python software. Summary of the invention
[0003] The present invention provides a method for configuring a Python runtime environment in a Bazel tool chain, so as to solve the problem in the prior art that a mixed specified Python runtime environment cannot be specified in a Bazel tool chain, resulting in low flexibility in software construction.
[0004] A first aspect of the present invention provides a method for configuring a Python runtime environment in a Bazel tool chain, comprising: creating a first shell script for a Python version that expects to use a Python runtime installed on a system, wherein the first shell script can replace an image that executes its process with a Python interpreter on the system and pass corresponding command line parameters; creating a second shell script for a Python version that expects to use a portable Python runtime, wherein the second shell script can replace an image that executes its process with an interpreter of a portable Python runtime and pass corresponding command line parameters; configuring a Bazel repository according to the first shell script and the second shell script; modifying a Python tool chain for a project to be built according to the configured Bazel repository, and specifying, in the pyruntime rule of Bazel, for different Python versions, the configuration of using the Python runtime on the system or the portable Python runtime.
[0005] In a feasible implementation manner, creating a first shell script for the Python version of the Python runtime that is expected to be used on the system includes: determining the path of the Python interpreter installed on the system; writing the first shell script, wherein the first shell script reads and parses command line parameters when started, uses the exec function to replace the image of the current process with the first Python interpreter on the system, and passes the parsed command line parameters to the first Python interpreter.
[0006] In a feasible implementation manner, creating a second shell script for a Python version that is expected to use a portable Python runtime includes: determining necessary files for the portable Python runtime, wherein the necessary files include a second Python interpreter, a standard library, and any necessary third-party libraries; writing a second shell script, wherein the second shell script locates the directory of the portable Python runtime at startup, reads and parses command line parameters, uses an exec function to replace the image of the current process with the second Python interpreter of the portable Python runtime, and passes the parsed command line parameters and the directory path of the portable Python runtime to the second Python interpreter.
[0007] In a feasible implementation manner, configuring the Bazel repository according to the first shell script and the second shell script includes: placing the first shell script and the second shell script in different Bazel repositories respectively, or placing the two in different directories of the same Bazel repository; in each Bazel repository containing shell scripts, modifying the Bazel build configuration file to include a reference to the corresponding shell script and specifying its build rules to ensure that the corresponding shell script can be successfully built into an executable file through the Bazel build system, and in the build configuration file, adding the configuration of the Python environment, including specifying the path of the Python interpreter, version requirements, and any necessary environment variable settings; if the first shell script or the second shell script depends on a specific Python interpreter or environment variable settings, performing corresponding configuration in the build configuration file of the Bazel repository to ensure that the corresponding dependency can be correctly parsed and passed during the build and execution process.
[0008] In a feasible implementation manner, placing the first shell script and the second shell script in different Bazel repositories respectively includes: creating independent directory structures for the first shell script and the second shell script in two different Bazel repositories respectively; creating a file containing the corresponding shell script source code in a corresponding directory of each Bazel repository, and ensuring that the file's naming and path comply with Bazel's build specifications; and modifying the BUILD file in the root directory or parent directory of each Bazel repository to include a reference to the corresponding shell script and build rules.
[0009] In a feasible implementation, the Python toolchain of the project to be built is modified according to the configured Bazel repository, and in the pyruntime rule of Bazel, the configuration of using the Python runtime on the system or the portable Python runtime is specified for different Python versions, including:
[0010] In the Bazel build configuration file of the project to be built, modify or add references to the pyruntime rule to define different Python runtime environments; for the Python version that expects to use the Python runtime installed on the system, specify in the pyruntime rule that the first interpreter attribute is the Bazel label of the executable file of the first shell script, and set the files attribute to an empty list or omit; for the Python version that expects to use the portable Python runtime, specify in the pyruntime rule that the second interpreter attribute is the Bazel label of the executable file of the second shell script, and set the files attribute to the Bazel label of the file group containing the portable Python runtime to ensure that the portable Python environment and its dependencies are correctly packaged and referenced; in the modified Bazel build configuration file, determine that all relevant Python versions are correctly configured to use the corresponding Python runtime environment, and verify and test them through the Bazel build system.
[0011] In a feasible implementation manner, modifying or adding a reference to the pyruntime rule in the Bazel build configuration file of the project to be built includes: opening the Bazel build configuration file of the project to be built; searching for or adding a declaration part of the pyruntime rule in the Bazel build configuration file; and using the name attribute to specify a unique identifier for different Python runtime environments as needed.
[0012] A second aspect of the present invention provides an apparatus for configuring a Python runtime environment in a Bazel tool chain, comprising: a first creation module, used to create a first shell script for a Python version that expects to use a Python runtime installed on a system, wherein the first shell script can replace the image of executing its process with a Python interpreter on the system and pass corresponding command line parameters; a second creation module, used to create a second shell script for a Python version that expects to use a portable Python runtime, wherein the second shell script can replace the image of executing its process with an interpreter of a portable Python runtime and pass corresponding command line parameters; a configuration module, used to configure a Bazel repository according to the first shell script and the second shell script; and a processing module, used to modify the Python tool chain of a project to be built according to the configured Bazel repository, and in the pyruntime rule of Bazel, specify the configuration of using the Python runtime on the system or the portable Python runtime for different Python versions.
[0013] In a feasible implementation manner, the first creation module is specifically used to: determine the path of the Python interpreter installed on the system; write a first shell script, which reads and parses command line parameters when it is started, uses the exec function to replace the image of the current process with the first Python interpreter on the system, and passes the parsed command line parameters to the first Python interpreter.
[0014] In a feasible implementation manner, the second creation module is specifically used to: determine the necessary files of the portable Python runtime, the necessary files including the second Python interpreter, the standard library and any necessary third-party libraries; write a second shell script, the second shell script locates the directory of the portable Python runtime at startup, reads and parses the command line parameters, uses the exec function to replace the image of the current process with the second Python interpreter of the portable Python runtime, and passes the parsed command line parameters and the directory path of the portable Python runtime to the second Python interpreter.
[0015] In a feasible implementation manner, the configuration module includes: a directory configuration unit, which is used to place the first shell script and the second shell script in different Bazel repositories respectively, or to place the two in different directories of the same Bazel repository; a build unit, which is used to modify the Bazel build configuration file in each Bazel repository containing shell scripts to include a reference to the corresponding shell script and specify its build rules to ensure that the corresponding shell script can be successfully built into an executable file through the Bazel build system, and to add the configuration of the Python environment in the build configuration file, including specifying the path of the Python interpreter, version requirements, and any necessary environment variable settings; a dependency configuration unit, which is used to make corresponding configurations in the build configuration file of the Bazel repository if the first shell script or the second shell script depends on a specific Python interpreter or environment variable settings, so as to ensure that the corresponding dependencies can be correctly parsed and passed during the build and execution process.
[0016] In a feasible implementation manner, the directory configuration unit is specifically used to: create independent directory structures for the first shell script and the second shell script in two different Bazel repositories, respectively; create a file containing the corresponding shell script source code in the corresponding directory of each Bazel repository, and ensure that its naming and path comply with Bazel's build specifications; modify the BUILD file in the root directory or parent directory of each Bazel repository to include references to the corresponding shell scripts and build rules.
[0017] In a feasible implementation, the processing module includes: a rule reference unit, which is used to modify or add a reference to the pyruntime rule in the Bazel build configuration file of the project to be built to define different Python runtime environments; a first processing unit, which is used to specify, in the pyruntime rule, a first interpreter attribute as the Bazel label of the executable file of the first shell script for the Python version that expects to use the Python runtime installed on the system, and set the files attribute to an empty list or omission; a second processing unit, which is used to specify, in the pyruntime rule, a second interpreter attribute as the Bazel label of the executable file of the second shell script for the Python version that expects to use the portable Python runtime, and set the files attribute to the Bazel label of the file group containing the portable Python runtime to ensure that the portable Python environment and its dependencies are correctly packaged and referenced; a verification and testing unit, which is used to determine, in the modified Bazel build configuration file, that all relevant Python versions have been correctly configured to use the corresponding Python runtime environment, and verify and test them through the Bazel build system.
[0018] In a feasible implementation, the rule reference unit is specifically used to: open the Bazel build configuration file of the project to be built; search or add the declaration part of the pyruntime rule in the Bazel build configuration file; and use the name attribute to specify a unique identifier for different Python runtime environments as needed.
[0019] A third aspect of the present invention provides a device for configuring a Python runtime environment in a Bazel tool chain, comprising: a memory and at least one processor, wherein the memory stores instructions; the at least one processor calls the instructions in the memory so that the device for configuring a Python runtime environment in a Bazel tool chain executes the above-mentioned method for configuring a Python runtime environment in a Bazel tool chain.
[0020] A fourth aspect of the present invention provides a computer-readable storage medium, wherein the computer-readable storage medium stores instructions, which, when executed on a computer, enable the computer to execute the above-mentioned method for configuring a Python runtime environment in a Bazel tool chain.
[0021] In the technical solution provided by the present invention, a first shell script is created for the Python version of the Python runtime installed on the system, and the first shell script can replace the image of the execution process with the Python interpreter on the system, and pass the corresponding command line parameters; a second shell script is created for the Python version of the portable Python runtime, and the second shell script can replace the image of the execution process with the interpreter of the portable Python runtime, and pass the corresponding command line parameters; a Bazel warehouse is configured according to the first shell script and the second shell script; according to the configured Bazel warehouse, the Python tool chain of the project to be built is modified, and in the pyruntime rule of Bazel, the configuration of using the Python runtime on the system or the portable Python runtime is specified for different Python versions. In the embodiment of the present invention, by creating shell scripts for specifying the use of system installation or portable runtime for different versions of Python, and configuring the Bazel warehouse and modifying the Python tool chain of the project accordingly, the problem that Bazel can mix and specify different versions of Python runtime environments when building Python projects is realized, and the flexibility and portability of Python project construction are improved. BRIEF DESCRIPTION OF THE DRAWINGS
[0022] Figure 1 A schematic diagram of an embodiment of a method for configuring a Python runtime environment in a Bazel tool chain in an embodiment of the present invention;
[0023] Figure 2 It is a schematic diagram of another embodiment of a method for configuring a Python runtime environment in a Bazel tool chain in an embodiment of the present invention;
[0024] Figure 3 A schematic diagram of an embodiment of a device for configuring a Python runtime environment in a Bazel tool chain in an embodiment of the present invention;
[0025] Figure 4 Schematic diagram of another embodiment of an apparatus for configuring a Python runtime environment in a Bazel tool chain in an embodiment of the present invention;
[0026] Figure 5 The figure is a schematic diagram of an embodiment of a device for configuring a Python runtime environment in a Bazel tool chain according to an embodiment of the present invention. DETAILED DESCRIPTION
[0027] An embodiment of the present invention provides a method for configuring a Python runtime environment in a Bazel tool chain, which is used to implement a mixed specified Python runtime environment in the Bazel tool chain to improve the flexibility of software construction.
[0028] The terms "first", "second", "third", "fourth", etc. (if any) in the specification and claims of the present invention and the above-mentioned drawings are used to distinguish similar objects, and are not necessarily used to describe a specific order or sequence. It should be understood that the data used in this way can be interchanged where appropriate, so that the embodiments described herein can be implemented in an order other than that illustrated or described herein. In addition, the terms "including" or "having" and any variations thereof are intended to cover non-exclusive inclusions, for example, a process, method, system, product or device that includes a series of steps or units is not necessarily limited to those steps or units that are clearly listed, but may include other steps or units that are not clearly listed or inherent to these processes, methods, products or devices.
[0029] It is understandable that the execution subject of the present invention may be a device for configuring the Python runtime environment in the Bazel tool chain, or may be a terminal or a server, which is not specifically limited here. The embodiment of the present invention is described by taking the server as the execution subject as an example.
[0030] For ease of understanding, the specific process of the embodiment of the present invention is described below. Figure 1 , an embodiment of the method for configuring the Python runtime environment in the Bazel tool chain in an embodiment of the present invention includes:
[0031] 101. Create a first shell script for the Python version that is expected to use the Python runtime installed on the system, the first shell script being capable of replacing the image of executing its process with the Python interpreter on the system and passing corresponding command line parameters;
[0032] Find the Python executable file in the system path. You can use which python3 or similar commands to determine it. If the system has multiple Python versions installed, you need to make sure that the required version is selected. Next, write the first shell script. The main function of the first shell script is to call the Python interpreter on the system. In the script, all command line parameters passed to the script are received and passed through shell special variables such as $# and $@. Then, use the exec command to replace the image of the current process with the specified Python interpreter and pass these parameters. In this way, when the script is executed, it actually starts the Python interpreter and runs any scripts or commands passed to it. Finally, add executable permissions to the script, usually completed by the chmod+x script_name.sh command, so that it can be run directly. This script can be used as a way to call the Python interpreter on the system during the Bazel build process to ensure that the correct Python version is used during build and runtime.
[0033] 102. Create a second shell script for the Python version that is expected to use the portable Python runtime, the second shell script can replace the image executing its process with the interpreter of the portable Python runtime and pass the corresponding command line parameters;
[0034] Confirm the exact path to the portable Python environment, which is usually a separate directory containing the Python interpreter and all its dependent libraries. It can be created by tools such as virtualenv and conda. Once the environment path is determined, you can start writing the second shell script.
[0035] In the writing process of the second shell script, bash is used as the interpreter, and all command line parameters passed to the script are captured and retained through shell built-in variables such as $# and $@ inside the script. These parameters are essential for the subsequent call to the portable Python interpreter and the execution of the specified Python script or command. Next, the exec command is used to replace the image of the current process, point it to the interpreter in the portable Python environment, and pass the previously captured parameters to it. This step is the core function of the script, which ensures that when the script is executed, the correct Python interpreter will be started to run the specified task.
[0036] After the second shell script is written, test it to ensure that it can correctly call the portable Python interpreter and execute the incoming Python script or command. Create a simple Python test script and verify its output by running the second shell script. If everything goes well, you can see the expected output of the test script, thus confirming the correctness of the script.
[0037] In order to enable the script to run directly, it is also necessary to give it executable permissions, which can be achieved by using the chmod + x command, which allows the script file to run as an executable program.
[0038] Placing the script file in a location accessible to the Bazel build system and configuring custom rules or macros in the BUILD file to call this script during the build process ensures that the Bazel build system can identify and use the correct Python interpreter path, thereby ensuring consistency and portability of the build and runtime environment.
[0039] 103. Configure the Bazel repository according to the first shell script and the second shell script;
[0040] Create a new directory in the root directory of the Bazel repository, for example, named tools, to store the first shell script, the second shell script, and any related configuration files. Copy the first shell script and the second shell script to the tools directory. The first shell script can be named use_system_python.sh; the second shell script can be named use_portable_python.sh.
[0041] Add references to the first and second shell scripts in Bazel's WORKSPACE file. Specifically, you can encapsulate the script calling logic by defining custom genrule or sh_binary rules, and specify the input and output files required by these rules. At the same time, to ensure that Bazel can find and use these scripts during the build process, you need to configure the corresponding build rules in the BUILD file, such as specifying these scripts as tools in the build process through the tools attribute.
[0042] In addition, if the portable Python environment is located in a non-standard path, you also need to specify the path to the environment in the Bazel configuration so that it can be found and used correctly during the build process. This can usually be achieved by setting environment variables in Bazel's command line arguments or by specifying environment variables in the env attribute of rules such as cc_binary and py_binary.
[0043] Finally, the Bazel repository needs to be thoroughly tested to ensure that the first and second shell scripts and the Python environment they depend on can be used correctly during the build and run process. This includes running the Bazel build command and checking the build log, as well as running the generated binary file and verifying that its output is as expected. Through these steps, you can ensure that the Bazel repository has been correctly configured according to the first and second shell scripts.
[0044] 104. Modify the Python toolchain of the project to be built according to the configured Bazel repository. In the pyruntime rule of Bazel, specify the configuration of using the Python runtime on the system or the portable Python runtime for different Python versions.
[0045] In Bazel's BUILD file, define the first pyruntime target and the second pyruntime target. The first pyruntime target is used for the Python runtime on the system, and the second pyruntime target is used for the portable Python runtime. For the Python runtime on the system, specify the first python_path attribute in the py_runtime rule as the path of the Python interpreter on the system. You can use the first
[0046] $(location / / tools:use_system_python.sh) to reference the first shell script created previously (assuming it is located in the tools directory and named use_system_python.sh); for the portable Python runtime, specify the second python_path attribute as the path of the portable Python interpreter in the py_runtime rule. You can reference the second shell script created previously by the second $(location / / tools:use_portable_python.sh) method, but this time it points to the second shell script (assuming it is located in the tools directory and named use_portable_python.sh).
[0047] For each Python target in your project, specify the Python runtime to use. This can be done by adding a dependency on the corresponding pyruntime target in the target's runtime_deps attribute. For example, if you want the target Python binary to use the Python runtime on your system, add a dependency on the system pyruntime target; if you want to use a portable Python runtime, add a dependency on the portable pyruntime target.
[0048] Make sure that Bazel's build configuration (such as a .bazelrc file or command-line arguments) correctly sets the build options to use the defined Python runtime during the build process. You can specify the build configuration by setting the --config flag, or directly specify the path to the Python interpreter through flags such as --python_path. With these steps, you can configure the environment to use the Python runtime on the system or the portable Python runtime separately for different Python versions in Bazel.
[0049] In the embodiment of the present invention, by creating shell scripts for different versions of Python that specify the use of system installation or portable runtime, and configuring the Bazel repository and modifying the Python tool chain of the project accordingly, Bazel can mix and specify different versions of Python runtime environments when building a Python project, thereby improving the flexibility and portability of Python project construction.
[0050] See also Figure 2 Another embodiment of the method for configuring the Python runtime environment in the Bazel tool chain in the embodiment of the present invention includes:
[0051] 201. Create a first shell script for the Python version that is expected to use the Python runtime installed on the system, the first shell script being capable of replacing the image executing its process with the Python interpreter on the system and passing corresponding command line parameters;
[0052] Determine the path of the Python interpreter installed on the system; write a first shell script, which reads and parses command line parameters when it is started, uses the exec function to replace the image of the current process with the first Python interpreter on the system, and passes the parsed command line parameters to the first Python interpreter.
[0053] Determine the exact path to the Python interpreter installed on the system by querying system environment variables (such as PATH) or using specialized command-line tools (such as which or type commands on Unix / Linux systems, and where commands on Windows systems). Write the first shell script. At the beginning of the first shell script, add code to read and parse the command-line parameters passed to the script. These parameters include the path to the Python script to be executed, additional parameters passed to the Python script, etc. After parsing these parameters, the script uses the exec function to execute a system call, which replaces the image of the current process (that is, the currently running shell process) with the image of the Python interpreter process on the specified path. In this process, the exec function also ensures that the parsed command-line parameters are passed intact to the new Python interpreter process. In this way, when the script is executed, the Python interpreter actually takes over the control of the process and starts executing the specified Python script or code.
[0054] 202. Create a second shell script for the Python version that is expected to use the portable Python runtime, the second shell script being capable of replacing the image executing its process with the interpreter of the portable Python runtime and passing corresponding command line parameters;
[0055] Determine necessary files for the portable Python runtime, which include a second Python interpreter, a standard library, and any necessary third-party libraries; write a second shell script, which locates the directory of the portable Python runtime when it is started, reads and parses command line parameters, uses the exec function to replace the image of the current process with the second Python interpreter of the portable Python runtime, and passes the parsed command line parameters and the directory path of the portable Python runtime to the second Python interpreter.
[0056] The necessary files for a portable Python runtime typically include a second Python interpreter, the complete Python standard library, and any third-party libraries that your application may depend on, packaged together to form a self-sufficient Python environment that can run in environments that do not have Python pre-installed.
[0057] In order to use this portable Python runtime, you need to write a second shell script. The first task performed by the second shell script when it is started is to locate the directory of the portable Python runtime, usually by reading a configuration file, environment variable or command line parameter. Once the directory is found, the second shell script will continue to read and parse the command line parameters passed to it, which may include the path to the Python script to be executed, additional parameters passed to the Python script, etc. The second shell script will use the exec function to perform a system call, which will replace the image of the current process with the image of the second Python interpreter process in the portable Python runtime directory. In this process, the script will also ensure that the parsed command line parameters and the directory path of the portable Python runtime are passed intact to the new Python interpreter process.
[0058] 203. Place the first shell script and the second shell script in different Bazel repositories, or place them in different directories of the same Bazel repository.
[0059] In the same Bazel repository, create independent directory structures for the first shell script and the second shell script. In the root directory of the Bazel repository, create a first subdirectory and a second subdirectory respectively. The first subdirectory is used to store the source code file of the first shell script, and the second subdirectory is used to store the source code file of the second shell script. Ensure that the naming and path of each script file conform to the Bazel build specification. Then, in the BUILD file in the root directory or parent directory of the repository, add build rules for the first subdirectory and the second subdirectory respectively to include references to the corresponding shell scripts and necessary build instructions.
[0060] In two different Bazel repositories, create independent directory structures for the first shell script and the second shell script respectively; in the corresponding directory of each Bazel repository, create a file containing the corresponding shell script source code, and ensure that its naming and path conform to the Bazel build specification; modify the BUILD file in the root directory or parent directory of each Bazel repository to include references to the corresponding shell scripts and build rules.
[0061] 204. In each Bazel repository containing shell scripts, modify the Bazel build configuration file to include a reference to the corresponding shell script and specify its build rules to ensure that the corresponding shell script can be successfully built into an executable file through the Bazel build system. In the build configuration file, add the configuration of the Python environment, including specifying the path and version requirements of the Python interpreter and any necessary environment variable settings;
[0062] In the corresponding directory of each Bazel repository containing shell scripts, create or modify the BUILD file to include references to the corresponding shell scripts and their build rules. For shell scripts, genrule or sh_binary rules are usually used to define their build process. For example, sh_binary rules can be used to directly specify shell script source files and generate executable files. When defining rules, you need to ensure that the path and name of the script file are correct.
[0063] In order to configure the Python environment, you need to add the configuration of the Python interpreter in the BUILD file. This includes specifying the path to the Python interpreter, which needs to be determined through environment variables or path lookup if you are using Python on the system; if you are using a portable Python runtime, you need to specify its directory path, as well as the Python version requirements. In addition, you also need to set any necessary environment variables to ensure that the Python interpreter can find its standard library, third-party libraries, and any specific runtime configuration, which can be achieved through Bazel's env properties or by explicitly setting environment variables through scripts during the build process.
[0064] 205. If the first shell script or the second shell script depends on a specific Python interpreter or environment variable setting, configure it accordingly in the build configuration file of the Bazel repository to ensure that the corresponding dependency can be correctly parsed and passed during the build and execution process;
[0065] If the first shell script or the second shell script depends on a specific Python interpreter, you need to explicitly specify the path to the corresponding Python interpreter in the BUILD file. This can be achieved by setting the tools attribute of the sh_binary or genrule rule, which allows you to specify additional tools required during the build process, including the Python interpreter. At the same time, you may need to use Bazel's env attribute or execution_requirements to set specific environment variables, which will be passed to the shell process when building or executing the script. For example, if the script depends on a specific PYTHONPATH environment variable to find the Python library it depends on, you need to set this environment variable in the BUILD file.
[0066] Additionally, for scripts that depend on a specific Python version, I need to use Bazel's toolchain feature to specify the version of the Python interpreter. You can set the toolchain resolver in the repository's .bazelrc configuration file and specify the required Python version in the BUILD file. In this way, Bazel will use the correct Python interpreter version when building the script and ensure that all relevant environment variables are set correctly.
[0067] These configurations ensure that in the Bazel build system, regardless of the build or execution process, the shell script can find and correctly use the Python interpreter and environment variables it depends on, thereby improving the reliability and repeatability of the build process.
[0068] 206. In the Bazel build configuration file of the project to be built, modify or add references to the pyruntime rule to define different Python runtime environments;
[0069] Open the Bazel build configuration file for the project to be built. Find or add the declaration section for the pyruntime rule in the Bazel build configuration file. Use the name attribute to specify unique identifiers for different Python runtime environments as needed.
[0070] Open the Bazel build configuration file of the project. In the configuration file, you need to find whether the declaration part of the pyruntime rule already exists. If not, you need to add a new declaration. When declaring the pyruntime rule, the name attribute is required. It is used to specify a unique identifier for the Python runtime environment. This identifier will be referenced in the subsequent configuration and build process to ensure that the correct Python interpreter is used.
[0071] For example, if the project needs to use two different Python versions to build and execute code, you can declare two pyruntime rules in the configuration file, specify different names for them, and configure the corresponding interpreter path and version information. In this way, when building the Python target, you can select which Python runtime environment to use by specifying the python_version attribute.
[0072] In addition, the pyruntime rule can also configure other properties, such as files (specify additional files provided with the interpreter, such as standard libraries or third-party libraries), env (set environment variables), etc., to further customize the Python runtime environment. By configuring the pyruntime rule, you can ensure that Bazel uses the correct interpreter and environment settings when building and executing Python code, thereby improving the build efficiency and portability of the project.
[0073] 207. For the Python version that expects to use the Python runtime installed on the system, specify in the pyruntime rule that the first interpreter attribute is the Bazel tag of the executable file of the first shell script, and set the files attribute to an empty list or be omitted;
[0074] In the Bazel build system, when a Python project needs to rely on the Python runtime environment installed on the system, the first interpreter attribute needs to be correctly configured in the pyruntime rule. The key to this step is to specify the first Bazel tag, which points to the executable file of the first shell script, which is responsible for starting the Python interpreter installed on the system.
[0075] Set the first interpreter attribute to the Bazel tag of the first shell script to ensure that Bazel uses the correct first Python interpreter when building and running. At this time, the files attribute should be set to an empty list or omitted because no additional Python runtime files need to be packaged. This configuration reduces build time because Bazel does not need to handle additional Python environment dependencies and avoids potential environment conflicts because the system-level Python environment is used.
[0076] 208. For Python versions that expect to use a portable Python runtime, specify the second interpreter attribute in the pyruntime rule as the Bazel label of the executable file of the second shell script, and set the files attribute to the Bazel label of the file group containing the portable Python runtime to ensure that the portable Python environment and its dependencies are packaged and referenced correctly;
[0077] When a Python project needs to run in a system-independent, self-contained Python environment, the pyruntime rule needs to be configured differently. In this case, the second interpreter attribute should be specified as a second Bazel tag for the executable of a second shell script, which is responsible for starting a portable Python interpreter. At the same time, the files attribute needs to be set to a second Bazel tag containing the portable Python runtime file group. This file group typically contains the second Python interpreter binary, the standard library, and any project-specific third-party dependencies. By packaging these files into the build output, Bazel ensures that the Python environment and all its dependencies are correctly referenced and packaged.
[0078] 209. In the modified Bazel build configuration file, make sure that all relevant Python versions are correctly configured to use the corresponding Python runtime environment and are verified and tested by the Bazel build system.
[0079] Check whether the interpreter and files attributes in the pyruntime rule are set correctly according to the requirements. For Python versions that rely on system installation, confirm that the script pointed to by the interpreter can correctly call the system Python interpreter and that the files attribute is empty or omitted. For scenarios using a portable Python environment, make sure that the script pointed to by the interpreter can start the packaged Python interpreter and that the files attribute contains all necessary runtime files. In addition, it is necessary to verify the validity of the configuration through Bazel's build and test commands, including running unit tests, integration tests, and any build targets related to the Python environment to ensure that they can all be successfully executed under the new configuration.
[0080] In the embodiment of the present invention, by creating shell scripts of specific runtime environments for different Python versions and configuring corresponding build rules in the Bazel repository, the flexibility of Python project construction is significantly enhanced, so that a system-installed or portable Python runtime environment can be flexibly selected as needed, while ensuring the smooth execution of the build process and the portability of the final software product.
[0081] The above describes the method for configuring the Python runtime environment in the Bazel tool chain in an embodiment of the present invention. The following describes the device for configuring the Python runtime environment in the Bazel tool chain in an embodiment of the present invention. Figure 3 In one embodiment of the present invention, an apparatus for configuring a Python runtime environment in a Bazel tool chain includes:
[0082] A first creation module 301 is used to create a first shell script for the Python version that is expected to use the Python runtime installed on the system, the first shell script can replace the image of executing its process with the Python interpreter on the system and pass corresponding command line parameters;
[0083] A second creation module 302 is used to create a second shell script for the Python version that is expected to use the portable Python runtime, the second shell script can replace the image executing its process with the interpreter of the portable Python runtime and pass the corresponding command line parameters;
[0084] A configuration module 303, configured to configure a Bazel repository according to the first shell script and the second shell script;
[0085] The processing module 304 is used to modify the Python tool chain of the project to be built according to the configured Bazel repository, and in the pyruntime rule of Bazel, specify the configuration of using the Python runtime on the system or the portable Python runtime for different Python versions.
[0086] In the embodiment of the present invention, by creating shell scripts for different versions of Python that specify the use of system installation or portable runtime, and configuring the Bazel repository and modifying the Python tool chain of the project accordingly, Bazel can mix and specify different versions of Python runtime environments when building a Python project, thereby improving the flexibility and portability of Python project construction.
[0087] See also Figure 4Another embodiment of the apparatus for configuring a Python runtime environment in a Bazel tool chain in an embodiment of the present invention includes:
[0088] A first creation module 301 is used to create a first shell script for the Python version that is expected to use the Python runtime installed on the system, the first shell script can replace the image of executing its process with the Python interpreter on the system and pass corresponding command line parameters;
[0089] A second creation module 302 is used to create a second shell script for the Python version that is expected to use the portable Python runtime, the second shell script can replace the image executing its process with the interpreter of the portable Python runtime and pass the corresponding command line parameters;
[0090] A configuration module 303, configured to configure a Bazel repository according to the first shell script and the second shell script;
[0091] The processing module 304 is used to modify the Python tool chain of the project to be built according to the configured Bazel repository, and in the pyruntime rule of Bazel, specify the configuration of using the Python runtime on the system or the portable Python runtime for different Python versions.
[0092] Optionally, the first creation module 301 is specifically used for:
[0093] Determine the path of the Python interpreter installed on the system; write a first shell script, which reads and parses command line parameters when it is started, uses the exec function to replace the image of the current process with the first Python interpreter on the system, and passes the parsed command line parameters to the first Python interpreter.
[0094] Optionally, the second creation module 302 is specifically used for:
[0095] Determine necessary files for the portable Python runtime, which include a second Python interpreter, a standard library, and any necessary third-party libraries; write a second shell script, which locates the directory of the portable Python runtime when it is started, reads and parses command line parameters, uses the exec function to replace the image of the current process with the second Python interpreter of the portable Python runtime, and passes the parsed command line parameters and the directory path of the portable Python runtime to the second Python interpreter.
[0096] Optionally, the configuration module 303 includes:
[0097] A directory configuration unit 3031 is used to place the first shell script and the second shell script in different Bazel repositories, or place them in different directories of the same Bazel repository;
[0098] A building unit 3032 is used to modify the Bazel build configuration file in each Bazel repository containing a shell script to include a reference to the corresponding shell script and specify its build rules to ensure that the corresponding shell script can be successfully built into an executable file through the Bazel build system, and to add the configuration of the Python environment in the build configuration file, including specifying the path and version requirements of the Python interpreter and any necessary environment variable settings;
[0099] The dependency configuration unit 3033 is used to make corresponding configurations in the build configuration file of the Bazel repository if the first shell script or the second shell script depends on a specific Python interpreter or environment variable setting, so as to ensure that the corresponding dependencies can be correctly parsed and transferred during the build and execution process.
[0100] Optionally, the directory configuration unit 3031 may be specifically used for:
[0101] In two different Bazel repositories, create independent directory structures for the first shell script and the second shell script respectively; in the corresponding directory of each Bazel repository, create a file containing the corresponding shell script source code, and ensure that its naming and path conform to the Bazel build specification; modify the BUILD file in the root directory or parent directory of each Bazel repository to include references to the corresponding shell scripts and build rules.
[0102] Optionally, the processing module 304 includes:
[0103] A rule reference unit 3041 is used to modify or add a reference to the pyruntime rule in a Bazel build configuration file of a project to be built, so as to define different Python runtime environments;
[0104] A first processing unit 3042 is configured to specify, in the pyruntime rule, for a Python version that expects to use the Python runtime installed on the system, a first interpreter attribute as a Bazel tag of an executable file of a first shell script, and to set the files attribute to an empty list or to be omitted;
[0105] The second processing unit 3043 is used to specify, for the Python version that expects to use the portable Python runtime, in the pyruntime rule, that the second interpreter attribute is the Bazel label of the executable file of the second shell script, and set the files attribute to the Bazel label of the file group containing the portable Python runtime, so as to ensure that the portable Python environment and its dependencies are correctly packaged and referenced;
[0106] The verification and testing unit 3044 is used to determine that all relevant Python versions are correctly configured to use the corresponding Python runtime environment in the modified Bazel build configuration file, and is verified and tested by the Bazel build system.
[0107] Optionally, the rule reference unit 3041 may be specifically used for:
[0108] Open the Bazel build configuration file for the project to be built. Find or add the declaration section for the pyruntime rule in the Bazel build configuration file. Use the name attribute to specify unique identifiers for different Python runtime environments as needed.
[0109] In the embodiment of the present invention, by creating shell scripts of specific runtime environments for different Python versions and configuring corresponding build rules in the Bazel repository, the flexibility of Python project construction is significantly enhanced, so that a system-installed or portable Python runtime environment can be flexibly selected as needed, while ensuring the smooth execution of the build process and the portability of the final software product.
[0110] above Figure 3 and Figure 4 The apparatus for configuring the Python runtime environment in the Bazel tool chain in an embodiment of the present invention is described in detail from the perspective of modular functional entities. The device for configuring the Python runtime environment in the Bazel tool chain in an embodiment of the present invention is described in detail from the perspective of hardware processing.
[0111] See also Figure 5 As shown, the device for configuring the Python runtime environment in the Bazel tool chain includes a processor 500 and a memory 501, wherein the memory 501 stores machine executable instructions that can be executed by the processor 500, and the processor 500 executes the machine executable instructions to implement the above-mentioned method for configuring the Python runtime environment in the Bazel tool chain.
[0112] Further, Figure 5The device for configuring the Python runtime environment in the Bazel tool chain shown further includes a bus 502 and a communication interface 503 , and the processor 500 , the communication interface 503 , and the memory 501 are connected via the bus 502 .
[0113] Among them, the memory 501 may include a high-speed random access memory (RAM), and may also include a non-volatile memory (non-volatile memory), for example, at least one disk storage. The communication connection between the system network element and at least one other network element is realized through at least one communication interface 503 (which can be wired or wireless), and the Internet, wide area network, local area network, metropolitan area network, etc. can be used. The bus 502 can be an ISA bus, a PCI bus or an EISA bus, etc. The bus can be divided into an address bus, a data bus, a control bus, etc. For ease of representation, Figure 5 Only one bidirectional arrow is used in the diagram, but this does not mean that there is only one bus or only one type of bus.
[0114] The processor 500 may be an integrated circuit chip with signal processing capability. In the implementation process, each step of the above method may be completed by an integrated logic circuit of hardware in the processor 500 or by instructions in the form of software. The above processor 500 may be a general-purpose processor, including a central processing unit (CPU), a network processor (NP), etc.; it may also be a digital signal processor (DSP), a dedicated integrated circuit (ASIC), or a processor.
[0115] (Application Specific Integrated Circuit, ASIC), Field-Programmable Gate Array (Field-Programmable Gate Array, FPGA) or other programmable logic devices, discrete gates or transistor logic devices, discrete hardware components. The methods, steps and logic block diagrams disclosed in the embodiments of the present disclosure can be implemented or executed. The general processor can be a microprocessor or the processor can also be any conventional processor, etc. The steps of the method disclosed in the embodiments of the present disclosure can be directly embodied as being executed by a hardware decoding processor, or can be executed by a combination of hardware and software modules in the decoding processor. The software module can be located in a mature storage medium in the field such as a random access memory, a flash memory, a read-only memory, a programmable read-only memory or an electrically erasable programmable memory, a register, etc. The storage medium is located in the memory 501, and the processor 500 reads the information in the memory 501, and completes the method steps of the aforementioned embodiment in combination with its hardware.
[0116] The present invention also provides a device for configuring a Python runtime environment in a Bazel tool chain. The computer device includes a memory and a processor. The memory stores computer-readable instructions. When the computer-readable instructions are executed by the processor, the processor executes the steps of the method for configuring a Python runtime environment in a Bazel tool chain in the above-mentioned embodiments.
[0117] The present invention also provides a computer-readable storage medium, which may be a non-volatile computer-readable storage medium or a volatile computer-readable storage medium. The computer-readable storage medium stores instructions, which, when executed on a computer, cause the computer to execute the steps of the method for configuring a Python runtime environment in a Bazel tool chain.
[0118] Those skilled in the art can clearly understand that, for the convenience and brevity of description, the specific working processes of the systems, devices and units described above can refer to the corresponding processes in the aforementioned method embodiments and will not be repeated here.
[0119] If the integrated unit is implemented in the form of a software functional unit and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of the present invention is essentially or the part that contributes to the prior art or all or part of the technical solution can be embodied in the form of a software product. The computer software product is stored in a storage medium, including a number of instructions to enable a computer device (which can be a personal computer, a server, or a network device, etc.) to perform all or part of the steps of the method described in each embodiment of the present invention. The aforementioned storage medium includes: U disk, mobile hard disk, read-only memory (ROM), random access memory (RAM), disk or optical disk and other media that can store program codes.
[0120] As described above, the above embodiments are only used to illustrate the technical solutions of the present invention, rather than to limit the same. Although the present invention has been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that the technical solutions described in the aforementioned embodiments may still be modified, or some of the technical features thereof may be replaced by equivalents. However, these modifications or replacements do not deviate the essence of the corresponding technical solutions from the spirit and scope of the technical solutions of the embodiments of the present invention.
Claims
1. A method for configuring a Python runtime environment in a Bazel toolchain, characterized in that: The method comprises: Creating a first shell script for the Python version that is expected to use the Python runtime installed on the system, wherein the first shell script is capable of replacing the image executing its process with the Python interpreter on the system and passing corresponding command line parameters; Creating a second shell script for the Python version that expects to use the portable Python runtime, the second shell script is capable of replacing the image executing its process with the interpreter of the portable Python runtime and passing corresponding command line parameters; Configure the Bazel repository according to the first shell script and the second shell script; Modify the Python toolchain of the project to be built based on the configured Bazel repository. In the pyruntime rule of Bazel, specify the configuration of using the Python runtime on the system or the portable Python runtime for different Python versions.
2. The method for configuring a Python runtime environment in a Bazel toolchain according to claim 1, wherein: The first shell script is created for the version of Python that is expected to be used by the Python runtime installed on the system, including: Determine the path to the Python interpreter installed on your system; Write a first shell script, which reads and parses command line parameters when it is started, uses the exec function to replace the image of the current process with the first Python interpreter on the system, and passes the parsed command line parameters to the first Python interpreter.
3. The method for configuring a Python runtime environment in a Bazel toolchain according to claim 1, wherein: Create a second shell script for the version of Python you wish to use with the portable Python runtime, including: determining necessary files for a portable Python runtime, the necessary files including a second Python interpreter, a standard library, and any necessary third-party libraries; Write a second shell script that locates the directory of the portable Python runtime when it is started, reads and parses command line parameters, uses the exec function to replace the image of the current process with the second Python interpreter of the portable Python runtime, and passes the parsed command line parameters and the directory path of the portable Python runtime to the second Python interpreter.
4. The method for configuring a Python runtime environment in a Bazel toolchain according to claim 1, wherein: The configuring the Bazel repository according to the first shell script and the second shell script includes: Placing the first shell script and the second shell script in different Bazel repositories respectively, or placing them in different directories of the same Bazel repository; In each Bazel repository containing shell scripts, modify the Bazel build configuration file to include a reference to the corresponding shell script and specify its build rules to ensure that the corresponding shell script can be successfully built into an executable file through the Bazel build system. In the build configuration file, add the configuration of the Python environment, including specifying the path to the Python interpreter, the required version, and any necessary environment variable settings; If the first shell script or the second shell script depends on a specific Python interpreter or environment variable setting, configure it accordingly in the build configuration file of the Bazel repository to ensure that the corresponding dependency can be correctly parsed and passed during the build and execution process.
5. The method for configuring a Python runtime environment in a Bazel toolchain according to claim 4, characterized in that: Placing the first shell script and the second shell script in different Bazel repositories respectively includes: In two different Bazel repositories, create independent directory structures for the first shell script and the second shell script respectively; In the corresponding directory of each Bazel repository, create a file containing the corresponding shell script source code, and make sure its name and path conform to the Bazel build specification; Modify the BUILD file in the root or parent directory of each Bazel repository to include references to the appropriate shell scripts and build rules.
6. The method for configuring a Python runtime environment in a Bazel toolchain according to claim 1, wherein: According to the configured Bazel repository, the Python toolchain of the project to be built is modified. In the pyruntime rule of Bazel, the configuration of using the Python runtime on the system or the portable Python runtime is specified for different Python versions, including: In the Bazel build configuration file of the project to be built, modify or add references to the pyruntime rule to define different Python runtime environments; For Python versions that expect to use the Python runtime installed on the system, specify the first interpreter attribute in the pyruntime rule as the Bazel tag of the executable file of the first shell script, and set the files attribute to an empty list or omit it; For Python versions that expect to use a portable Python runtime, specify the second interpreter attribute in the pyruntime rule as the Bazel label of the second shell script's executable file, and set the files attribute to the Bazel label of the file group containing the portable Python runtime to ensure that the portable Python environment and its dependencies are packaged and referenced correctly; In the modified Bazel build configuration file, make sure that all relevant Python versions are properly configured to use the corresponding Python runtime environment and are verified and tested by the Bazel build system.
7. The method for configuring a Python runtime environment in a Bazel toolchain according to claim 6, characterized in that: In the Bazel build configuration file of the project to be built, modify or add a reference to the pyruntime rule, including: Open the Bazel build configuration file of the project to be built; Find or add the declaration section for the pyruntime rule in said Bazel build configuration file; Use the name attribute as needed to specify unique identifiers for different Python runtime environments.
8. A device for configuring a Python runtime environment in a Bazel toolchain, characterized in that: The device for configuring the Python runtime environment in the Bazel tool chain includes: A first creation module, used to create a first shell script for a Python version that is expected to use the Python runtime installed on the system, wherein the first shell script can replace the image of executing its process with the Python interpreter on the system and pass corresponding command line parameters; A second creation module, used to create a second shell script for the Python version that expects to use the portable Python runtime, wherein the second shell script can replace the image executing its process with the interpreter of the portable Python runtime and pass corresponding command line parameters; A configuration module, configured to configure a Bazel repository according to the first shell script and the second shell script; The processing module is used to modify the Python toolchain of the project to be built according to the configured Bazel repository. In the pyruntime rule of Bazel, the configuration of using the Python runtime on the system or the portable Python runtime is specified for different Python versions.
9. A device for configuring a Python runtime environment in a Bazel toolchain, characterized in that: The device for configuring the Python runtime environment in the Bazel tool chain includes: a memory and at least one processor, wherein the memory stores instructions; The at least one processor calls the instructions in the memory to enable the device for configuring the Python runtime environment in the Bazel tool chain to perform the method for configuring the Python runtime environment in the Bazel tool chain as described in any one of claims 1-7.
10. A computer-readable storage medium having instructions stored thereon, characterized in that: When the instructions are executed by a processor, the method for configuring a Python runtime environment in a Bazel tool chain is implemented as described in any one of claims 1 to 7.