I have a folder with a “Manifest.toml” and a “Project.toml” file. I also have a script, “my_julia_script.jl”. I open the script and run some stuff. However, it seems to use the general julia environment. I want it to use the one define by the local “Manifest.toml” and a “Project.toml” files. Is there some setting to make this default?
(or some way to manually make this happen?)
You have to activate your project.
using Pkg
cd(Base.source_dir()) # Changes into the directory , where your "my_julia.jl" is.
Pkg.activate(".") # active the project, with a static environment
# Pkg.activate(; temp=true) # activate the project with a temporary environment
Pkg.update() # update the ..tom-files
# [here starts your code]
If you need an individual sysimage(it can be faster), you can create a sysimage in the VSC
-
active in the settings of VSC the checkbox of:
“Julia: Use Custom Sysimage” -
in the left side of the status line in the VSC choose the environment of your project
i.e.
julia env: v1.7…to your folder, where you “my_julia_script.jl”, is
Needed for the PackageCompiler, that he can generate the sysimage in your specific
project-directory. -
Press: [Ctrl]+[Shift]+[B] and let VSC build your sysimage.
(An installation of the PackageCompiler is NOT needed).
To start your “my_julia_script.jl” with your individual project sysimage
press the usual [Alt]+[J]+[Alt]+[O]
You can run the command “Julia: Activate this environment” by pressing “Shift+Ctrl+p” and entering that string. This will activate any existing environment in the current folder open in vscode. There is also “Julia: Change current environment” where you can set which environment you want at some location on disk.
The normal invocation if you want a script to activate its local environment (independently of VSCode functionality) is
using Pkg
Pkg.activate(@__DIR__)
This has the advantage of not using a non-exported and undocumented internal function, as well as not changing the current directory.
That comment is strange. It would activate a temporary environment, period, with no relation to the local project files.
Notice that this loses the reproducibility advantage of the Manifest file. If you prefer reproducibility over updated packages you can do Pkg.instantiate()
to make sure that you, or someone else running the script, have the necessary packages available.
I talked to someone who mentioned there was some VSCode setting where one could have scripts automatically use the local environment if there is any (thus avoiding having to run a command at the beginning of every session). Is there such a setting?
using Pkg
Pkg.activate(@__DIR__)
I dont know that, but many thanks for that tip.
I got a summary from Sebastian Pfitzner on slack.
Basically, VSCode will automatically use the environment of the (top) folder you opened in it. In my case, the top folder doesn’t contain an environment, and by script is in a subfolder, so the default environment is used. However, by right-clicking on that folder, I can select to use that environment instead.
I have the following ~/.julia/config/startup.jl
in place, which also works when using the command line interface (CLI) or Jupyter Notebooks:
println("Executing user-specific startup file (", @__FILE__, ")...")
try
using Revise
println("Revise started")
catch e
@warn "Error initializing Revise" exception=(e, catch_backtrace())
end
if isfile("Project.toml") && isfile("Manifest.toml")
Pkg.activate(".")
else
Pkg.activate("$(ENV["HOME"])/.julia/environments/v$(VERSION.major).$(VERSION.minor)")
end
That looks really neat! Thanks!
P.S.: /opt/julia/etc/julia/startup.jl
containing the following:
# This file should contain site-specific commands to be executed on Julia startup;
# Users may store their own personal commands in `~/.julia/config/startup.jl`.
println("Executing site-specific startup file (", @__FILE__, ")...")
push!(LOAD_PATH, "$(ENV["JULIA_PATH"])/local/share/julia/environments/v$(VERSION.major).$(VERSION.minor)")
using Pkg
Some packages (IJulia
, LanguageServer
, Revise
, etc.) being pre-installed in a shared system directory at /opt/julia/local/share/julia
.