# Initialization of Packages on import

**URL:** <https://discourse.julialang.org/t/initialization-of-packages-on-import/13633>\
**Category:** General Usage\
**Tags:** package\
**Created:** [August 17, 2018, 5:58pm UTC](https://discourse.julialang.org/t/initialization-of-packages-on-import/13633 "2018-08-17T17:58:51Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![azraq27](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/azraq27/32/951_2.png) [@azraq27](https://discourse.julialang.org/u/azraq27)\
**Post date:** [August 17, 2018, 5:58pm UTC](https://discourse.julialang.org/t/initialization-of-packages-on-import/13633/1 "2018-08-17T17:58:51Z")

</div>

I’m still getting used to the new v0.7 Pkg (Pkg3), but I kind of wish I had more documentation on it. Overall looks cool, I’m just getting confused about some things.

One specific change I’m struggling with is that it seems like module-level code is only run once, during pre-compilation. During subsequent imports, this code isn’t re-run.

For example, using a package:

```julia
module Tester

@info "test"

end

```

When you first type `using Tester`, it will appropriately print out “test”. But, then if you quit Julia, restart it, and type `using Tester` again, it won’t appear again.

If there is initialization code for a package, is there a way to have a segment of code run on the first import during a session? For example, include a `startup.jl` file in the package directory?

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [August 17, 2018, 6:07pm UTC](https://discourse.julialang.org/t/initialization-of-packages-on-import/13633/2 "2018-08-17T18:07:46Z")

</div>

You can have an ` __init__ ` function.

---

<div class="post-metadata">

**Author:** ![azraq27](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/azraq27/32/951_2.png) [@azraq27](https://discourse.julialang.org/u/azraq27)\
**Post date:** [August 17, 2018, 6:13pm UTC](https://discourse.julialang.org/t/initialization-of-packages-on-import/13633/3 "2018-08-17T18:13:43Z")

</div>

🙂 I just saw that looking through someone else’s code. That’s perfect.

Is that documented somewhere? Is the documentation just lagging behind, or is there some big “here’s how the new Pkg works” page that I’m missing? (I’ve obviously read [https://docs.julialang.org/en/stable/stdlib/Pkg/](https://docs.julialang.org/en/stable/stdlib/Pkg/))

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [August 17, 2018, 6:22pm UTC](https://discourse.julialang.org/t/initialization-of-packages-on-import/13633/4 "2018-08-17T18:22:47Z")

</div>

> [@azraq27](#):
>
> Is that documented somewhere? Is the documentation just lagging behind, or is there some big “here’s how the new Pkg works” page that I’m missing?

This is not related to Pkg, ` __init__ `has been there for long. But it does not seem to be documented, so that would be good to do! Seems like a great opportunity for you to contribute to Julia! Otherwise, please open an issue and someone else will get to it.

---

<div class="post-metadata">

**Author:** ![azraq27](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/azraq27/32/951_2.png) [@azraq27](https://discourse.julialang.org/u/azraq27)\
**Post date:** [August 17, 2018, 6:29pm UTC](https://discourse.julialang.org/t/initialization-of-packages-on-import/13633/5 "2018-08-17T18:29:12Z")

</div>

Ahh! That was my issue. I was thinking about it in terms of the Pkg system. The precompilation of modules on import, and the usage of ` __init__ ` is very nicely documented here:

[https://docs.julialang.org/en/stable/manual/modules/#Module-initialization-and-precompilation-1](https://docs.julialang.org/en/stable/manual/modules/#Module-initialization-and-precompilation-1)

That explains a lot. Thanks

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [August 17, 2018, 6:32pm UTC](https://discourse.julialang.org/t/initialization-of-packages-on-import/13633/6 "2018-08-17T18:32:01Z")

</div>

Nice find, would be good to have it available from the REPL too, see [https://github.com/JuliaLang/julia/issues/28730](https://github.com/JuliaLang/julia/issues/28730)

---

<div class="post-metadata">

**Author:** ![tlienart](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tlienart/32/7640_2.png) [@tlienart](https://discourse.julialang.org/u/tlienart)\
**Post date:** [February 1, 2021, 8:40am UTC](https://discourse.julialang.org/t/initialization-of-packages-on-import/13633/7 "2021-02-01T08:40:54Z")

</div>

updating the link since that one is now dead : [Modules · The Julia Language](https://docs.julialang.org/en/v1/manual/modules/#Module-initialization-and-precompilation)
