How to export modules / packages locally?

How to export modules / packages locally?

Hello!

I’ve been looking unsuccessfully at how to develop multiple distinct modules locally. My project has three parts:

Two modules: TextManipulation, AstroPhysics
One Julia script that uses both TextManipulation and AstroPhysics

I’ve been looking at the documentation and got lost, though I’m pretty sure the answer’s there.

My understanding is that I’d have to

  1. build the modules, and export them
  2. import the built modules to the environment where the script is running

These tasks seem trivial but I’m getting lost. I also don’t want to have them uploaded to the general registry.

Welcome. I think you are searching for:

include("TextManipulation.jl")
using .TextManipulation

include("AstroPhysics.jl")
using .AstroPhysics

if not, please give us further details!

4 Likes

When I develop packages, I use activate --temp; dev /path/to/MyPackage in the Pkg REPL, followed by using MyPackage. This will create a temporary environment where you’re free to test things, then make the local package available as if you downloaded it from the registry. However, I’ve only ever developed packages with a single module.

3 Likes

Thanks Imiq! I believe that my set up is different. Each module and script is in a different environment so I can easily reuse.

I came up with this structure where AstroText.jl is the script (and each project will have its own environment):

[hamburger@x julia_projects]$ tree .
.
├── AstroPhysics
│   ├── Project.toml
│   └── src
│       └── AstroPhysics.jl
├── AstroText
│   ├── Project.toml
│   └── src
│       └── AstroText.jl
└── TextManipulation
    ├── Project.toml
    └── src
        └── TextManipulation.jl

I came up with this as I’d like to reuse the AstroPhysics and TextManipulation modules for other totally unrelated projects.

It seems that what you suggest is to have all modules in the same environment (which I’m trying to avoid as it’d hinder code reuse).

Thanks Ashlin, that may be what I’m looking for.

I see, each “module” is a package (just not registered). Then yes, you can dev them in the AstroText environment. You can also add them using:

Pkg.add(path="/home/leandro/.julia/dev/Package")

That will make a copy of the current state of Package to be available in the current environment (using dev you will track the changes to that package, which may be what you want, or not)

2 Likes

Since you already have a fair bit of structure you might consider registering your packages in a registry of your own. The LocalRegistry package can help you out with that.

1 Like

What do you mean here?
import or using from temp environment?
Or repeat the process in project or default environment?

When you ]develop a package from a local or remote directory (your own computer, GitHub, etc.), it gets added to your current environment, whatever that may be. At that point, you could make its contents available with import or using, just the same as you used Pkg.jl to fetch it from the general registry.

I repeat the process every time I want to test the package I’m developing, which is why I use a temporary environment. I suppose you could just run ]develop once in your default environment if you are working with a stable package that just isn’t available on the general registry.

1 Like

When working with different environments, I push everything to my private GitHub repo and simply add them as dependencies by add repourl (this way I can simply “update” after committing extra work on any of the dependent packages).

Maybe this is not “the way”, but it reduces my cognitive load (by always using the old add package method).

In case the previous answers suggesting to use Pkg.dev weren’t explicit enough, here is what you should be able to do with such a file structure:

shell$ julia --project=AstroText

       # Declare dependencies to both your libraries
       #   using `develop` means that Pkg does not try to manage versions:
       #   you'll use your dependences as they are defined by the current state
       #   of the julia source files in the given paths
julia> import Pkg
julia> Pkg.develop("path/to/AstroPhysics")
julia> Pkg.develop("path/to/TextManipulation")

The commands above should be run once, to populate the AstroText/Manifest.toml file with the relevant information. From there, you can use your libraries from your script without any problem:

shell$ julia --project=AstroText
julia> using AstroPhysics # should work and load sources from the registered path on your filesystem
3 Likes