# Is importing module is allowed inside @static?

**URL:** <https://discourse.julialang.org/t/is-importing-module-is-allowed-inside-static/28975>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [September 20, 2019, 2:34am UTC](https://discourse.julialang.org/t/is-importing-module-is-allowed-inside-static/28975 "2019-09-20T02:34:52Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sijun](https://avatars.discourse-cdn.com/v4/letter/s/b2d939/32.png) [@Sijun](https://discourse.julialang.org/u/Sijun)\
**Post date:** [September 20, 2019, 2:34am UTC](https://discourse.julialang.org/t/is-importing-module-is-allowed-inside-static/28975/1 "2019-09-20T02:34:52Z")

</div>

It seems allowed but somehow limited in what it allows.

I tried:

```julia
use_sklearn = true

@static if use_sklearn
    using ScikitLearn
    @sk_import linear_model: LogisticRegression
else
    using GLM
end

```

, which produces

> ERROR: LoadError: LoadError: UndefVarError: @sk\_import not defined

The code only compiles when using ScikitLearn is placed outside and above @static block:

```julia
using ScikitLearn

use_sklearn = true

@static if use_sklearn
    @sk_import linear_model: LogisticRegression
else
    using GLM
end

```

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [September 20, 2019, 3:32am UTC](https://discourse.julialang.org/t/is-importing-module-is-allowed-inside-static/28975/2 "2019-09-20T03:32:02Z")

</div>

Yes it is.

What you see errors for the same reason it errors without the `@static`, the macro needs to be evaluated **BEFORE** the toplevel expression (i.e. `if ...` or `@static if ...`) can run so the macro is evaluated **BEFORE** you imported the module. Repeat `if` twice, first with import and second time with the macro should work. Note that for the `import`/`using` you don’t need `@static`. You only need it for the `@sk_import` since it’s a macro.

---

<div class="post-metadata">

**Author:** ![Sijun](https://avatars.discourse-cdn.com/v4/letter/s/b2d939/32.png) [@Sijun](https://discourse.julialang.org/u/Sijun)\
**Post date:** [September 20, 2019, 4:26am UTC](https://discourse.julialang.org/t/is-importing-module-is-allowed-inside-static/28975/3 "2019-09-20T04:26:56Z")

</div>

Oh understood. So any macro in a code block is evaluated (or expanded) before anything else than itself. That makes sense. It should be very helpful to new comers if that is mentioned in the manual (seems not). The double ifs or @static ifs work fine. Thanks a lot!
