# Avoid namespace pollution from startup.jl

**URL:** <https://discourse.julialang.org/t/avoid-namespace-pollution-from-startup-jl/116743>\
**Category:** General Usage\
**Tags:** startup, namespaces\
**Created:** [July 8, 2024, 12:54am UTC](https://discourse.julialang.org/t/avoid-namespace-pollution-from-startup-jl/116743 "2024-07-08T00:54:30Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![PeterSimon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petersimon/32/25193_2.png) [@PeterSimon](https://discourse.julialang.org/u/PeterSimon)\
**Post date:** [July 8, 2024, 12:54am UTC](https://discourse.julialang.org/t/avoid-namespace-pollution-from-startup-jl/116743/1 "2024-07-08T00:54:30Z")

</div>

I have startup.jl file that I’m mostly happy with. Any time Julia is updated and I start an interactive session without explicitly activating some environment, `startup.jl` adds a few key packages to the base environment. A few of these packages are auto-imported via `using` in every interactive session. I use a `let` block to avoid polluting the `Main` module namespace with any variables used to accomplish these tasks. However, I need to `import Pkg` to obtain its functionality for these same tasks, and therefore every interactive session has the `Pkg` module imported. Is there a way to avoid this? Listing of `startup.jl` follows…

```julia
if isinteractive()
    # If not already present, add selected packages to base (@v1.x) package environment.
    # Automatically use some of these in every REPL session.
    let # Avoid polluting global namespace
        import Pkg
        auto_used_packages = [:Revise, :PkgOnlineHelp, :OhMyREPL]
        manually_used_packages = [:Plots, :BenchmarkTools]
        vstr = string(VERSION)
        dots = findall('.', vstr)
        vstr = "v" * vstr[begin:dots[2]-1]
        baseenvpath = joinpath(first(DEPOT_PATH), "environments", vstr, "Project.toml")
        if Pkg.project().path == baseenvpath
            for package in union(auto_used_packages, manually_used_packages)
                (String(package) in keys(Pkg.project().dependencies)) || Pkg.add(String(package))
            end
        end
        
        for package in auto_used_packages
            @eval using $package
        end
    end

end

```

---

<div class="post-metadata">

**Author:** ![abraemer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abraemer/32/51403_2.png) [@abraemer](https://discourse.julialang.org/u/abraemer)\
**Post date:** [July 8, 2024, 1:56am UTC](https://discourse.julialang.org/t/avoid-namespace-pollution-from-startup-jl/116743/2 "2024-07-08T01:56:13Z")

</div>

For me having Pkg available is actually a feature 🙂 but if you don’t want it then simply wrap the bits that use Pkg into a `module` so the import doesn’t leak.

---

<div class="post-metadata">

**Author:** ![PeterSimon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petersimon/32/25193_2.png) [@PeterSimon](https://discourse.julialang.org/u/PeterSimon)\
**Post date:** [July 8, 2024, 2:33am UTC](https://discourse.julialang.org/t/avoid-namespace-pollution-from-startup-jl/116743/3 "2024-07-08T02:33:13Z")

</div>

Won’t the module name then be the pollution?

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)\
**Post date:** [July 8, 2024, 5:52am UTC](https://discourse.julialang.org/t/avoid-namespace-pollution-from-startup-jl/116743/4 "2024-07-08T05:52:46Z")

</div>

What about `using Pkg: Pkg`, will that pull the name in only locally?

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [July 8, 2024, 6:38am UTC](https://discourse.julialang.org/t/avoid-namespace-pollution-from-startup-jl/116743/5 "2024-07-08T06:38:10Z")

</div>

> [@PeterSimon](#):
>
> ```julia
> vstr = string(VERSION)
> dots = findall('.', vstr)
> vstr = "v" * vstr[begin:dots[2]-1]
> 
> ```

This seemed quite awkward to me, but I was a bit surprised to find there isn’t a lot of convenience functions for working with version numbers. Is there anything better than

```julia
vstr = string('v', VERSION.major, '.', VERSION.minor)

```

?

---

<div class="post-metadata">

**Author:** ![abraemer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abraemer/32/51403_2.png) [@abraemer](https://discourse.julialang.org/u/abraemer)\
**Post date:** [July 8, 2024, 7:32am UTC](https://discourse.julialang.org/t/avoid-namespace-pollution-from-startup-jl/116743/6 "2024-07-08T07:32:09Z")

</div>

Technically yes, but you can choose some other name to avoid any practical issues e.g.

```julia
module var"##super-secret-setup-space"
    import Pkg
    # rest of setup...
end

```

This won’t show up in any auto-complete and so is for practical purposes invisible I’d say.  
Just checked: `names(Main)` and `varinfo(Main)` also do not show it.

---

<div class="post-metadata">

**Author:** ![PeterSimon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petersimon/32/25193_2.png) [@PeterSimon](https://discourse.julialang.org/u/PeterSimon)\
**Post date:** [July 8, 2024, 12:49pm UTC](https://discourse.julialang.org/t/avoid-namespace-pollution-from-startup-jl/116743/7 "2024-07-08T12:49:33Z")

</div>

> [@jules](#):
>
> What about `using Pkg: Pkg`, will that pull the name in only locally?

No, `Pkg` is still seen in `Main` afterwards:

```julia
julia> Pkg
ERROR: UndefVarError: `Pkg` not defined

julia> let
           using Pkg: Pkg
           Pkg.status()
       end
Status `C:\Users\peter\.julia\environments\v1.10\Project.toml`
  [6e4b80f9] BenchmarkTools v1.5.0
  [7073ff75] IJulia v1.25.0
  [5903a43b] Infiltrator v1.8.3
  [c3a54625] JET v0.9.6
  [33e6dc65] MKL v0.7.0
  [5fb14364] OhMyREPL v0.5.27
  [021381c1] PkgOnlineHelp v0.2.3
  [91a5bcdd] Plots v1.40.5
  [295af30f] Revise v3.5.15

julia> Pkg
Pkg

```

---

<div class="post-metadata">

**Author:** ![PeterSimon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petersimon/32/25193_2.png) [@PeterSimon](https://discourse.julialang.org/u/PeterSimon)\
**Post date:** [July 8, 2024, 1:17pm UTC](https://discourse.julialang.org/t/avoid-namespace-pollution-from-startup-jl/116743/9 "2024-07-08T13:17:53Z")

</div>

I think the following, based on @rafael.guerra 's suggestion, also is pretty good:

```julia
vstr = "v"*first(splitext(string(VERSION)))

```

Even though it’s a bit involved, one doesn’t have to access the undocumented fields of a `VersionNumber`.

---

<div class="post-metadata">

**Author:** ![ericphanson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ericphanson/32/215186_2.png) [@ericphanson](https://discourse.julialang.org/u/ericphanson)\
**Post date:** [July 8, 2024, 1:23pm UTC](https://discourse.julialang.org/t/avoid-namespace-pollution-from-startup-jl/116743/10 "2024-07-08T13:23:28Z")

</div>

You can also create an anonymous module with `Module()`, so you can do something like

```julia
@eval Module() begin
    import Pkg
    # ...
end

```

---

<div class="post-metadata">

**Author:** ![PeterSimon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petersimon/32/25193_2.png) [@PeterSimon](https://discourse.julialang.org/u/PeterSimon)\
**Post date:** [July 8, 2024, 1:37pm UTC](https://discourse.julialang.org/t/avoid-namespace-pollution-from-startup-jl/116743/11 "2024-07-08T13:37:52Z")

</div>

Thanks to all who replied. It’s such a pleasure learning from this forum!

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [July 8, 2024, 2:27pm UTC](https://discourse.julialang.org/t/avoid-namespace-pollution-from-startup-jl/116743/12 "2024-07-08T14:27:43Z")

</div>

> [@PeterSimon](#):
>
> `"v"*first(splitext(string(VERSION)))`

My main problem with the above is that it’s a bit indirect, and it’s not clear what it’s actually doing. My suggestion is very explicit.

Are the `major` and `minor` fields actually undocumented?

---

<div class="post-metadata">

**Author:** ![DanielVandH](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/danielvandh/32/31134_2.png) [@DanielVandH](https://discourse.julialang.org/u/DanielVandH)\
**Post date:** [July 8, 2024, 2:30pm UTC](https://discourse.julialang.org/t/avoid-namespace-pollution-from-startup-jl/116743/13 "2024-07-08T14:30:13Z")

</div>

They are documented now as of [Document the fields of `VersionNumber` by jakobnissen · Pull Request #50179 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/pull/50179)
