# Correct handling of LOAD\_PATH in Julia script for running with julia - -project= . .

**URL:** https://discourse.julialang.org/t/correct-handling-of-load-path-in-julia-script-for-running-with-julia-project/120812
**Category:** New to Julia
**Tags:** package
**Created:** [October 2, 2024, 12:51pm UTC](https://discourse.julialang.org/t/correct-handling-of-load-path-in-julia-script-for-running-with-julia-project/120812 "2024-10-02T12:51:08Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![EvgenijGr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/evgenijgr/32/32211_2.png) [@EvgenijGr](https://discourse.julialang.org/u/EvgenijGr)
#### Post date: [October 2, 2024, 12:51pm UTC](https://discourse.julialang.org/t/correct-handling-of-load-path-in-julia-script-for-running-with-julia-project/120812/1 "2024-10-02T12:51:08Z")

</div>

Hello, I have the following questions.  
Below is a MWE script that I want to run with `julia --project=myLocalEnv .\mySimpleScript.jl`. What I’ve discovered is that even if the package `DataFrames` is not installed in `myLocalEnv`, the script will run correctly since `"@#.#"` usually belongs to the `LOAD_PATH` and in my case `DataFrames` were installed in that global environment.

```julia
# LOAD_PATH = ["@", "@stdlib"]
# direct assignment to that variable has no effect

# these lines help to prevent "leaking" 
# packages from 
empty!(LOAD_PATH)
push!(LOAD_PATH, "@")
push!(LOAD_PATH, "@stdlib")

using DataFrames

function main() 
    println("HELLO WORLD FROM MAIN FUNCTION!")
    println("$ARGS")
    @show Base.active_project()
    @show Base.current_project()
    @show pathof(DataFrames)
    @show LOAD_PATH
    dat = DataFrame("a"=>[1, 2, 3], "b"=>[4, 5, 6] )
    @show dat
end 

if abspath(PROGRAM_FILE) == @ __FILE__
    main()
end 

```

So, my questions are:

1. Is this a correct (idiomatic?) way to prevent using packages from a global environment?
2. Why the line

```julia
LOAD_PATH = ["@", "@stdlib"] 

```

has no effect, while

```julia
empty!(LOAD_PATH)
push!(LOAD_PATH, "@")
push!(LOAD_PATH, "@stdlib")

```

produces the desired effect?

Thanks in advance for your help!

---

<div class="post-metadata">

### Author: ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)
#### Post date: [October 2, 2024, 1:36pm UTC](https://discourse.julialang.org/t/correct-handling-of-load-path-in-julia-script-for-running-with-julia-project/120812/2 "2024-10-02T13:36:31Z")

</div>

What you have encountered is the environment stack. The idiomatic approach to this is to keep your version specific global environment mostly empty. Specifically, we do not recommend having a package such as DataFrames.jl in your global default environment.

As an alternative, you can add it to another _shared_ environment such as “@datascience”. The “@” will install the package in a shared space in your `~/.julia/environments` that you can access from anywhere.

Also note that Julia is stricter here if you build a Julia package rather than a script.

---

<div class="post-metadata">

### Author: ![danielwe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/danielwe/32/35657_2.png) [@danielwe](https://discourse.julialang.org/u/danielwe)
#### Post date: [October 2, 2024, 3:33pm UTC](https://discourse.julialang.org/t/correct-handling-of-load-path-in-julia-script-for-running-with-julia-project/120812/3 "2024-10-02T15:33:27Z")

</div>

> [@EvgenijGr](#):
>
> Why the line
> 
> ```julia
> LOAD_PATH = ["@", "@stdlib"] 
> 
> ```
> 
> has no effect, while
> 
> ```julia
> empty!(LOAD_PATH)
> push!(LOAD_PATH, "@")
> push!(LOAD_PATH, "@stdlib")
> 
> ```
> 
> produces the desired effect?

Because only the second version modifies the existing variable `Base.LOAD_PATH`, which determines where packages can be loaded from. The first creates a new variable `Main.LOAD_PATH` that julia’s package loading won’t know or care about. (`Main` is the implicit module in which your script code runs.)

---

<div class="post-metadata">

### Author: ![EvgenijGr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/evgenijgr/32/32211_2.png) [@EvgenijGr](https://discourse.julialang.org/u/EvgenijGr)
#### Post date: [October 2, 2024, 4:28pm UTC](https://discourse.julialang.org/t/correct-handling-of-load-path-in-julia-script-for-running-with-julia-project/120812/4 "2024-10-02T16:28:19Z")

</div>

> [@danielwe](#):
>
> The first creates a new variable `Main.LOAD_PATH` that julia’s package loading won’t know or care about. (`Main` is the implicit module in which your script code runs.)

Interesting… So, in layman terms, by writing `LOAD_PATH = ...` (with a scope in `Main`) I’ve accidentally created a new variable that shadowed `Base.LOAD_PATH`. Is my understanding correct? By the way, is there any special function that can show the scope (module? function?) to which a variable belongs?

---

<div class="post-metadata">

### Author: ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)
#### Post date: [October 2, 2024, 5:00pm UTC](https://discourse.julialang.org/t/correct-handling-of-load-path-in-julia-script-for-running-with-julia-project/120812/5 "2024-10-02T17:00:38Z")

</div>

> [@EvgenijGr](#):
>
> By the way, is there any special function that can show the scope (module? function?) to which a variable belongs?

I don’t think so. The closest thing to that is [`@isdefined`](https://docs.julialang.org/en/v1/base/base/#Base.@isdefined) as far as I know.

---

<div class="post-metadata">

### Author: ![evanfields](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/evanfields/32/1744_2.png) [@evanfields](https://discourse.julialang.org/u/evanfields)
#### Post date: [October 2, 2024, 6:51pm UTC](https://discourse.julialang.org/t/correct-handling-of-load-path-in-julia-script-for-running-with-julia-project/120812/6 "2024-10-02T18:51:47Z")

</div>

> [@EvgenijGr](#):
>
> By the way, is there any special function that can show the scope (module? function?) to which a variable belongs?

Sounds like you’re looking for `@which`:

```julia
help?> @which
  @which

  Applied to a function or macro call, it evaluates the arguments to the specified call, and returns the Method object for the method that would be called for those arguments.
  Applied to a variable, it returns the module in which the variable was bound. It calls out to the which function.

```

---

<div class="post-metadata">

### Author: ![danielwe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/danielwe/32/35657_2.png) [@danielwe](https://discourse.julialang.org/u/danielwe)
#### Post date: [October 2, 2024, 7:44pm UTC](https://discourse.julialang.org/t/correct-handling-of-load-path-in-julia-script-for-running-with-julia-project/120812/7 "2024-10-02T19:44:15Z")

</div>

> [@EvgenijGr](#):
>
> by writing `LOAD_PATH = ...` (with a scope in `Main`) I’ve accidentally created a new variable that shadowed `Base.LOAD_PATH`

Correct, unless you’ve already referenced `LOAD_PATH` and thus implicitly bound that name to `Base.LOAD_PATH`:

```julia-repl
julia> println(LOAD_PATH)
["@", "@v#.#", "@stdlib"]

julia> LOAD_PATH = []
ERROR: cannot assign a value to imported variable Base.LOAD_PATH from module Main
Stacktrace:
 [1] top-level scope
   @ REPL[2]:1

```

> [@EvgenijGr](#):
>
> is there any special function that can show the scope (module? function?) to which a variable belongs?

As @evanfields mentioned, `@which`:

```julia-repl
julia> @which LOAD_PATH
Base

```

Note that simply using `@which` is sufficient to establish the binding:

```julia-repl
julia> @which LOAD_PATH # In a fresh session
Base

julia> LOAD_PATH = []
ERROR: cannot assign a value to imported variable Base.LOAD_PATH from module Main
Stacktrace:
 [1] top-level scope
   @ REPL[1]:1

```

---

<div class="post-metadata">

### Author: ![EvgenijGr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/evgenijgr/32/32211_2.png) [@EvgenijGr](https://discourse.julialang.org/u/EvgenijGr)
#### Post date: [October 3, 2024, 10:43am UTC](https://discourse.julialang.org/t/correct-handling-of-load-path-in-julia-script-for-running-with-julia-project/120812/8 "2024-10-03T10:43:08Z")

</div>

Thanks for the explanation! `@which LOAD_PATH`, unfortunately, didn’t work in my script, but what it showed through REPL is perfectly enough for my understanding of inner workings for now.

---

<div class="post-metadata">

### Author: ![danielwe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/danielwe/32/35657_2.png) [@danielwe](https://discourse.julialang.org/u/danielwe)
#### Post date: [October 3, 2024, 5:21pm UTC](https://discourse.julialang.org/t/correct-handling-of-load-path-in-julia-script-for-running-with-julia-project/120812/9 "2024-10-03T17:21:29Z")

</div>

> [@EvgenijGr](#):
>
> `@which LOAD_PATH`, unfortunately, didn’t work in my script

That’s because `@which` is defined in the InteractiveUtils standard library, which is automatically loaded in an interactive repl session but not when running a script. You could do `using InteractiveUtils` at the top of your script, but it’s probably not the best idea, it’s meant specifically for interactive exploration.
