p13rr0m
p13rr0m

Reputation: 1297

How can I create and work with global environments in Julia?

I was wondering if it possible in Julia to create global environments and work with them in a similar way as the conda package manager.
So what would be the Julia way of doing the following:

# create a new global environment
conda create -n myenv

# list all global environments
conda env list

# activate a global environment
conda activate myenv

# list my installed packages
conda list

# install a new package
conda install newpackage

# execute a file with a global environment activated
python main.py

Upvotes: 2

Views: 669

Answers (1)

Antonello
Antonello

Reputation: 6431

Yes, the package manager is integrated in the standard lib shipped with Julia and has some nice advantages over conda env, as the local, project specific environments (what you call "global" environments) are really thin, no packages is copied, just a text file ("Project.toml"), automatically updated, defines the exact packages and versions used in the environment. This strongly favours experiment replicability. Packages remain instead under a user-specific julia folder, and may be shared between different local environments.

I am reporting above the "script" syntax. There is an equivalent syntax that you can access in the REPL by typing ] to enter a specific package mode.

using Pkg

# Activate the global environment associated to the Julia version you are using
Pkg.activate() 


# Create or switch (activate) to a new environment (we call it "local" in Julia, not "global")
# Note that the environment used can be a different directory than the "current directory" used to reference relative path, e.g. to import a csv file 
Pkg.activate("/path/to/a/folder")
# more often:
Pkg.activate(".")

# List all global environments
# Not sure about this. There are a lot of [discussions](https://github.com/JuliaLang/Pkg.jl/issues/621), but the fact is that you rarely need it.
# As Julia environments are really light (just a text file) you end up having an environment on each project you work on rather than having a few environments that you share across projects.
# To see if a project has its own environment just check if it has a Project.toml file 

# List my installed packages (in the current environment)
Pkg.status()

# Install a new package
Pkg.install("Mypackage")

# Execute a file with a global environment activated
# Again, used rarely, as it is within the script more than in the calling of the script that the environment to use is defined
julia myScript.jl --project=myProjectDir

Another very important command is Pkg.instantiate(). It will make sure that all the packages defined in the environment (in the Project.toml file) are downloaded, installed and precompiled, so that the rest of the code works with the designed environment.

For more info, the documentation on Julia environments is here.

Upvotes: 1

Related Questions