Skip to content

Commit 51cd56f

Browse files
Merge pull request #31010 from JuliaLang/sk/load-path-docs
complete, accurate documentation of LOAD_PATH and JULIA_LOAD_PATH
2 parents a0bb006 + 695842f commit 51cd56f

File tree

3 files changed

+57
-28
lines changed

3 files changed

+57
-28
lines changed

base/initdefs.jl

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,36 @@ const DEFAULT_LOAD_PATH = ["@", "@v#.#", "@stdlib"]
7676
LOAD_PATH
7777
7878
An array of paths for `using` and `import` statements to consider as project
79-
environments or package directories when loading code. See [Code Loading](@ref Code-Loading).
79+
environments or package directories when loading code. It is populated based on
80+
the [`JULIA_LOAD_PATH`](@ref JULIA_LOAD_PATH) environment variable if set;
81+
otherwise it defaults to `["@", "@v#.#", "@stdlib"]`. Entries starting with `@`
82+
have special meanings:
83+
84+
- `@` refers to the "current active environment", the initial value of which is
85+
initially determined by the [`JULIA_PROJECT`](@ref JULIA_PROJECT) environment
86+
variable or the `--project` command-line option.
87+
88+
- `@stdlib` expands to the absolute path of the current Julia installation's
89+
standard library directory.
90+
91+
- `@name` refers to a named environment, which are stored in depots (see
92+
[`JULIA_DEPOT_PATH`](@ref JULIA_DEPOT_PATH)) under the `environments`
93+
subdirectory. The user's named environments are stored in
94+
`~/.julia/environments` so `@name` would refer to the environment in
95+
`~/.julia/environments/name` if it exists and contains a `Project.toml` file.
96+
If `name` contains `#` characters, then they are replaced with the major, minor
97+
and patch components of the Julia version number. For example, if you are
98+
running Julia 1.2 then `@v#.#` expands to `@v1.2` and will look for an
99+
environment by that name, typically at `~/.julia/environments/v1.2`.
100+
101+
The fully expanded value of `LOAD_PATH` that is searched for projects and packages
102+
can be seen by calling the `Base.load_path()` function.
103+
104+
See also:
105+
[`JULIA_LOAD_PATH`](@ref JULIA_LOAD_PATH),
106+
[`JULIA_PROJECT`](@ref JULIA_PROJECT),
107+
[`JULIA_DEPOT_PATH`](@ref JULIA_DEPOT_PATH), and
108+
[Code Loading](@ref Code-Loading).
80109
"""
81110
const LOAD_PATH = copy(DEFAULT_LOAD_PATH)
82111
const HOME_PROJECT = Ref{Union{String,Nothing}}(nothing)

doc/src/manual/environment-variables.md

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -75,27 +75,40 @@ and a global configuration search path of
7575

7676
### `JULIA_PROJECT`
7777

78-
A directory path that points to the current Julia project. Setting this
79-
environment variable has the same effect as specifying the `--project` start-up
80-
option, but `--project` has higher precedence. If the variable is set to `@.` then
81-
Julia tries to find a project directory that contains `Project.toml` or
82-
`JuliaProject.toml` file from the current directory and its parents. See also
78+
A directory path that indicates which project should be the initial active project.
79+
Setting this environment variable has the same effect as specifying the `--project`
80+
start-up option, but `--project` has higher precedence. If the variable is set to `@.`
81+
then Julia tries to find a project directory that contains `Project.toml` or
82+
`JuliaProject.toml` file from the current directory and its parents. See also
8383
the chapter on [Code Loading](@ref).
8484

8585
!!! note
8686

87-
`JULIA_PROJECT` must be defined before starting julia; defining it in `startup.jl` is too late in the startup process.
87+
`JULIA_PROJECT` must be defined before starting julia; defining it in `startup.jl`
88+
is too late in the startup process.
8889

8990
### `JULIA_LOAD_PATH`
9091

91-
A separated list of absolute paths that are to be appended to the variable
92-
[`LOAD_PATH`](@ref). (In Unix-like systems, `:` is the path separator; in
93-
Windows systems, `;` is the path separator.) The `LOAD_PATH` variable is where
94-
[`Base.require`](@ref) and `Base.load_in_path()` look for code; it defaults to
95-
the absolute path
96-
`$JULIA_HOME/../share/julia/stdlib/v$(VERSION.major).$(VERSION.minor)` so that,
97-
e.g., version 0.7 of Julia on a Linux system with a Julia executable at
98-
`/bin/julia` will have a default `LOAD_PATH` of `/share/julia/stdlib/v0.7`.
92+
The `JULIA_LOAD_PATH` environment variable is used to populate the global Julia
93+
[`LOAD_PATH`](@ref) variable, which determines which packages can be loaded via
94+
`import` and `using` (see [Code Loading](@ref)). Unlike the shell `PATH` variable,
95+
empty entries in `JULIA_LOAD_PATH` are expanded to the default value of `LOAD_PATH`,
96+
`["@", "@v#.#", "@stdlib"]` when populating `LOAD_PATH`. This allows easy appending,
97+
prepending, etc. of the load path value in shell scripts regardless of whether
98+
`JULIA_LOAD_PATH` is already set or not. For example, to prepend the directory
99+
`/foo/bar` to `LOAD_PATH` just do
100+
```sh
101+
export JULIA_LOAD_PATH="/foo/bar:$JULIA_LOAD_PATH"
102+
```
103+
If the `JULIA_LOAD_PATH` environment variable is already set, its old value will be
104+
prepended with `/foo/bar`. On the other hand, if `JULIA_LOAD_PATH` is not set, then
105+
it will be set to `/foo/bar:` which will expand to a `LOAD_PATH` value of
106+
`["/foo/bar", "@", "@v#.#", "@stdlib"]`. If `JULIA_LOAD_PATH` is set to the empty
107+
string, it expands to an empty `LOAD_PATH` array. In other words, the empty string
108+
is interpreted as a zero-element array, not a one-element array of the empty string.
109+
This behavior was chosen so that it would be possible to set an empty load path via
110+
the environment variable. If you want the default load path, either unset the
111+
environment variable or if it must have a value, set it to the string `:`.
99112

100113
### `JULIA_HISTORY`
101114

doc/src/manual/modules.md

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -183,19 +183,6 @@ look for `Utils` in `Parent`'s enclosing module rather than in `Parent` itself.
183183

184184
Note that relative-import qualifiers are only valid in `using` and `import` statements.
185185

186-
### Module file paths
187-
188-
The global variable [`LOAD_PATH`](@ref) contains the directories Julia searches for modules when calling
189-
`require`. It can be extended using [`push!`](@ref):
190-
191-
```julia
192-
push!(LOAD_PATH, "/Path/To/My/Module/")
193-
```
194-
195-
Putting this statement in the file `~/.julia/config/startup.jl` will extend [`LOAD_PATH`](@ref) on
196-
every Julia startup. Alternatively, the module load path can be extended by defining the environment
197-
variable `JULIA_LOAD_PATH`.
198-
199186
### Namespace miscellanea
200187

201188
If a name is qualified (e.g. `Base.sin`), then it can be accessed even if it is not exported.

0 commit comments

Comments
 (0)