Code reuse
When developing programmes in Julia, there is often a need for multiple usage of the same code in different projects. In such cases it is convenient to organise the code in the form of modules or packages.
Module is a simpler way to organise code. It represents a file containing functions, variables and other elements combined into one namespace (see Modules). Modules are convenient for local development and code usage.
A Package is a more comprehensive structure that includes not only code, but also metadata such as project descriptions, dependencies, versions, etc. Packages are maintained by Julia itself through the Pkg package manager, allowing dependencies and versions to be managed.
Unlike packages, modules themselves do not support automatic dependency management via Pkg. This can lead to situations where a module does not work properly if dependencies are not installed or are not at the correct versions. Packages, on the other hand, allow Julia to automatically track and manage dependencies, making them more reliable and easier to reuse and distribute.
Creating a module
If the goal is to create code and reuse it later, you can limit yourself to creating your own module. To do this, in the file browser create a future module file with the desired name and in the desired directory, e.g. the file
my_module.jl
at the path /user/mymodule
.
Use the module
command to add the content of the module, e.g. a function:
module my_module
function get_value(x)
return x
end
end
In Julia, the module name does not have to be the same as the file name, but when connecting a module using using or import , you should use exactly the name that is specified within the module.
|
Add the module path to the Julia path manually or with the command:
engee.addpath("/user/mymodule")
Use the using
operator to load the module:
using my_module
Once the module is loaded, you can call a function (or any other inline code) from it:
my_module.get_value(7)
7
Package creation
To create your own package, first create a basic project structure using the generate
command. To do this, switch the command line to shell
shell mode (key ;) and navigate to the directory where you want to create the package. For example:
cd My_libs
# вывод
/user/My_libs
Generate a new project using the generate
command (first switch to the pkg>
package manager by clicking ] at the command line):
generate My_lib # будет создан проект My_lib с файлами Project.toml и src/My_lib.jl
#вывод
Generating project Test_lib:
My_lib/Project.toml
My_lib/src/My_lib.jl
The generate
command must be run once for each new package. This command creates the initial project structure, including the files Project.toml
and src/package name.jl
. Once generated, you can develop the package, add code, configure dependencies, etc.
A project in Julia is a structure that includes the configuration files and directories needed for organisation, dependency management and versioning. A package can consist of one or more files and can be loaded into any Julia project. |
The source code for the package will be placed in the src
folder. By default, the created package file will contain the code:
module My_lib
greet() = print("Hello World!")
end # module My_lib
Replace the contents of the file with the following code with usage of a different module name:
module My_lib
function get_value(x)
return x
end
end
Next, in Pkg mode, use the develop
command to add your project to the current session:
develop /user/My_libs/My_lib/
#вывод
Resolving package versions...
Updating `~/.project/Project.toml`
[487ef170] + My_lib v0.1.0 `~/My_libs/My_lib`
Updating `~/.project/Manifest.toml`
[487ef170] + My_lib v0.1.0 `~/My_libs/My_lib`
here:
-
A session in Julia is the current working environment, managed by the package manager (Pkg), which defines the available packages and projects for code execution. A session includes the
Project.toml
andManifest.toml
configuration files, which capture version and dependency information about packages, allowing Julia to manage the project and its dependencies. -
Project.toml
- describes the metadata of a package, including its name, version, authors, and dependencies on other packages. This is the main file that helps Julia manage packages and their versions. It is created automatically by usage of thegenerate
command. -
Manifest.toml
- records the exact versions of all dependencies (including their subdependencies) of a package. It is created automatically, when installing dependencies or when adding a package via Pkg. It is created automatically by usage of thedevelop
command to connect to the current session.
The develop command must be run once for each package in the current environment. The package will then be added to the environment and you can use it in your code. If changes are made to the package in the future, they will automatically be available without having to run develop again, since this command creates a symbolic reference to the package in the environment.
|
For usage of the created package, use the import
or using
operators:
using My_lib
# или import My_lib
Creating a package with generate
provides a structure that facilitates dependency and version management. It also simplifies package distribution.
When developing modules and packages, you may need to restart the kernel to reload Julia processes in the current session. To do this, click Feedback
|
To distribute packages, you need to upload the package files to a shared repository, which can be a variety of hosts, such as git.engee.com. For more information on working with a remote repository and uploading local files, see our article series on Git, the version control system used by Engee. Julia packages can also be registered in the General Registry of Julia packages. Use the tool Registrator.jl to do this. Packages can be installed using a repository URL, e.g. for GitHub:
If the package is successfully registered via Registrator.jl, it can be installed with the standard command:
|