julia

Running Julia scripts from the terminal in a virtual environment


I'm curious whether Julia supports the same workflow as Python + virtual environments.

In Python I can do the following from the terminal:

$ python -m venv venv
$ source venv/bin/activate
(venv) $ pip install myFavPackage
(venv) $ python src/myFavScript.py

Julia can use virtual environments through its REPL like so:

(@v1.5) pkg> activate .
(myFavProject) pkg> add myFavPackage
julia> include("src/myFavScript.jl")

But if I run the first two lines (which adds all dependencies to the Project.toml and Manifest.toml files) and then jump out to the terminal and run

$ julia src/myFavScript.jl

Then it doesn't recognise the package I installed:

ERROR: LoadError: ArgumentError: Package myFavPackage not found in current path:
- Run `import Pkg; Pkg.add("myFavPackage")` to install the myFavPackage package.

Does this mean that I have to install my packages globally to run Julia scripts from the terminal? And if not, how can I force the terminal to use the local dependencies?


Solution

  • You can use the --project flag, i.e.

    $ julia --project=path/to/project src/myFavScript.jl
    

    If you are in the correct folder you can just omit the path, i.e.

    $ julia --project src/myFavScript.jl
    

    Finally, if you want this behavor by default, you can set the the JULIA_PROJECT environment variable to @., which is equivalent to always starting Julia with --project=@., which will load the project from the current directory or any of its parents.