# Redirecting stdout to avoid banners while \`import\`ing

**URL:** <https://discourse.julialang.org/t/redirecting-stdout-to-avoid-banners-while-import-ing/37633>\
**Category:** General Usage\
**Created:** [April 15, 2020, 10:56am UTC](https://discourse.julialang.org/t/redirecting-stdout-to-avoid-banners-while-import-ing/37633 "2020-04-15T10:56:47Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![abulak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abulak/32/28314_2.png) [@abulak](https://discourse.julialang.org/u/abulak)\
**Post date:** [April 15, 2020, 10:56am UTC](https://discourse.julialang.org/t/redirecting-stdout-to-avoid-banners-while-import-ing/37633/1 "2020-04-15T10:56:47Z")

</div>

I have a silly problem:  
We’re using a few packages that insist on printing their banners upon `import`ing (consider this as something beyond me:). This results in a “barrage of banners” situation.  
I tried to redirect output as follows:

```julia
module TestModule
Libc.flush_cstdio()
Base.flush(stdout)
original_stdout = stdout
out_rd, out_wr = redirect_stdout()
import Nemo
import Polymake
Libc.flush_cstdio()
Base.flush(out_wr)
close(out_wr)
redirect_stdout(original_stdout)
str = read(out_rd, String); @show(str)
end

```

this works well in REPL, but somehow doesn’t work if module is precompiled. Any idea why and how to make it so?

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [April 15, 2020, 12:11pm UTC](https://discourse.julialang.org/t/redirecting-stdout-to-avoid-banners-while-import-ing/37633/2 "2020-04-15T12:11:53Z")

</div>

> [@abulak](#):
>
> Any idea why and how to make it so?

The reason why is that this code is not run when the module is loaded from a precompiled file. The way precompilation of packages work is to deserialize the state of the module to a file during precompilation and when loading, the state is simply deserialized and then the ` __init__ ` function of the module is run. No top-level code is run when the module is loaded.

I’m not really sure how to “hook” into the ` __init__ ` function of dependent packages though.

Unconditional printing of banners and such on package load is really user-hostile behavior…

---

<div class="post-metadata">

**Author:** ![abulak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abulak/32/28314_2.png) [@abulak](https://discourse.julialang.org/u/abulak)\
**Post date:** [April 15, 2020, 12:17pm UTC](https://discourse.julialang.org/t/redirecting-stdout-to-avoid-banners-while-import-ing/37633/3 "2020-04-15T12:17:17Z")

</div>

yes, I know and I would agree with the no banner policy; however, these are decisions beyond my will and I have to play along;

* * *

Btw. thanks for the explanation on precompilation etc.; this should have been obvious to me after reading the documentation, but somehow wasn’t.

---

<div class="post-metadata">

**Author:** ![abulak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abulak/32/28314_2.png) [@abulak](https://discourse.julialang.org/u/abulak)\
**Post date:** [April 15, 2020, 9:24pm UTC](https://discourse.julialang.org/t/redirecting-stdout-to-avoid-banners-while-import-ing/37633/4 "2020-04-15T21:24:46Z")

</div>

If you could spare a moment, I came up with the following ~~solution~~ hack to to detect if package has been imported from other module (and then suppress banner) or from `Main`/module inside `Main` (and display it then).

```julia
__is_root_module() = haskey(Base.package_locks, Base.PkgId(@__MODULE__))

```

is it even remotely close to being correct? (I know that `Base.package_locks` is private and is not intended for this use)

---

<div class="post-metadata">

**Author:** ![BMO](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bmo/32/14820_2.png) [@BMO](https://discourse.julialang.org/u/BMO)\
**Post date:** [July 12, 2020, 9:54am UTC](https://discourse.julialang.org/t/redirecting-stdout-to-avoid-banners-while-import-ing/37633/5 "2020-07-12T09:54:42Z")

</div>

I think Nemo banner is conditional  
[https://github.com/Nemocas/Nemo.jl/issues/817](https://github.com/Nemocas/Nemo.jl/issues/817)  
[Nemo banner](https://github.com/Nemocas/Nemo.jl/blob/ea2e6539431b11f0688d34629063fe6a06e837d6/src/Nemo.jl#L258-L267)  
But I have no idea how to implement this in the ` __init__ ` function.  
I would also like to remove exactly those two banners, did you, @abulak, succed with `__is_root_module()`?  
How do you use it?

---

<div class="post-metadata">

**Author:** ![abulak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abulak/32/28314_2.png) [@abulak](https://discourse.julialang.org/u/abulak)\
**Post date:** [July 13, 2020, 9:27am UTC](https://discourse.julialang.org/t/redirecting-stdout-to-avoid-banners-while-import-ing/37633/6 "2020-07-13T09:27:36Z")

</div>

No; please consider opening an issue in those packages

---

<div class="post-metadata">

**Author:** ![rfourquet](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rfourquet/32/3610_2.png) [@rfourquet](https://discourse.julialang.org/u/rfourquet)\
**Post date:** [July 13, 2020, 10:16am UTC](https://discourse.julialang.org/t/redirecting-stdout-to-avoid-banners-while-import-ing/37633/7 "2020-07-13T10:16:55Z")

</div>

The same trick as in `__is_root_module` is precisely what is used in `Nemo` to not print the banner when it’s loaded from a hardcoded list of packages, I don’t think it’s possible to extend this list. As mentioned in the issue you linked to, you could set the environment variable `ENV["NEMO_PRINT_BANNER"] = false` (but it’s not documented and might not be supported in the future).

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [July 13, 2020, 11:45am UTC](https://discourse.julialang.org/t/redirecting-stdout-to-avoid-banners-while-import-ing/37633/8 "2020-07-13T11:45:17Z")

</div>

> [@abulak](#):
>
> No; please consider opening an issue in those packages

I submitted a PR to remove the banner message, but it was rejected: [remove banner message by stevengj · Pull Request #828 · Nemocas/Nemo.jl · GitHub](https://github.com/Nemocas/Nemo.jl/pull/828)

---

<div class="post-metadata">

**Author:** ![BMO](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bmo/32/14820_2.png) [@BMO](https://discourse.julialang.org/u/BMO)\
**Post date:** [July 14, 2020, 10:46pm UTC](https://discourse.julialang.org/t/redirecting-stdout-to-avoid-banners-while-import-ing/37633/9 "2020-07-14T22:46:42Z")

</div>

Setting this environment variable works on REPL, but I do not know how to do it in a module definition…?

---

<div class="post-metadata">

**Author:** ![Tero\_Frondelius](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tero_frondelius/32/7629_2.png) [@Tero\_Frondelius](https://discourse.julialang.org/u/Tero_Frondelius)\
**Post date:** [July 15, 2020, 2:52pm UTC](https://discourse.julialang.org/t/redirecting-stdout-to-avoid-banners-while-import-ing/37633/10 "2020-07-15T14:52:41Z")

</div>

Wouldn’t it work, if you just add this:  
`ENV["NEMO_PRINT_BANNER"] = false` as the first thing in your Module?

---

<div class="post-metadata">

**Author:** ![BMO](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bmo/32/14820_2.png) [@BMO](https://discourse.julialang.org/u/BMO)\
**Post date:** [July 15, 2020, 3:30pm UTC](https://discourse.julialang.org/t/redirecting-stdout-to-avoid-banners-while-import-ing/37633/11 "2020-07-15T15:30:12Z")

</div>

With the following code, the banner is shown.

```julia
module NemoPolymake

ENV["NEMO_PRINT_BANNER"] = false
using Nemo
using Polymake

end

```

I guess it is related to [https://discourse.julialang.org/t/redirecting-stdout-to-avoid-banners-while-import-ing/37633/2?u=bmo](https://discourse.julialang.org/t/redirecting-stdout-to-avoid-banners-while-import-ing/37633/2)

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [July 17, 2020, 3:00pm UTC](https://discourse.julialang.org/t/redirecting-stdout-to-avoid-banners-while-import-ing/37633/12 "2020-07-17T15:00:10Z")

</div>

> [@Tero\_Frondelius](#):
>
> Wouldn’t it work, if you just add this:  
> `ENV["NEMO_PRINT_BANNER"] = false` as the first thing in your Module?

(The environment is then only set during precompilation—this is when top-level code in the module is executed—and will no longer be set when the user imports the module at a later time.)

---

<div class="post-metadata">

**Author:** ![Tero\_Frondelius](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tero_frondelius/32/7629_2.png) [@Tero\_Frondelius](https://discourse.julialang.org/u/Tero_Frondelius)\
**Post date:** [July 17, 2020, 6:31pm UTC](https://discourse.julialang.org/t/redirecting-stdout-to-avoid-banners-while-import-ing/37633/13 "2020-07-17T18:31:23Z")

</div>

Thanks for the clear explanation. (I just wonder, why didn’t I understand this earlier from other answers.)
