# Why won't Julia let me put a module import within a function?

**URL:** <https://discourse.julialang.org/t/why-wont-julia-let-me-put-a-module-import-within-a-function/46504>\
**Category:** General Usage\
**Created:** [September 12, 2020, 10:33am UTC](https://discourse.julialang.org/t/why-wont-julia-let-me-put-a-module-import-within-a-function/46504 "2020-09-12T10:33:29Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![fusion809](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fusion809/32/6080_2.png) [@fusion809](https://discourse.julialang.org/u/fusion809)\
**Post date:** [September 12, 2020, 10:33am UTC](https://discourse.julialang.org/t/why-wont-julia-let-me-put-a-module-import-within-a-function/46504/1 "2020-09-12T10:33:29Z")

</div>

I just created the function:

```julia
function operats()
    using GLPK
    using Clp
    using JuMP
    using Hungarian
end

```

and to my surprise this gave me the error:

```julia
ERROR: syntax: "using" expression not at top level

```

The reason I wanted to put that all into a single function was because those are the modules I use for operations research and it would be handy to have a single function that imported them all. May I ask why Julia refuses to allow module imports to appear within a function’s body? Is there a way around it?

Thanks for your time.

---

<div class="post-metadata">

**Author:** ![josuagrw](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/josuagrw/32/1015_2.png) [@josuagrw](https://discourse.julialang.org/u/josuagrw)\
**Post date:** [September 12, 2020, 10:40am UTC](https://discourse.julialang.org/t/why-wont-julia-let-me-put-a-module-import-within-a-function/46504/2 "2020-09-12T10:40:04Z")

</div>

you could put the using statements in a file and call

```julia
include("thatfile.jl")

```

---

<div class="post-metadata">

**Author:** ![johnmyleswhite](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johnmyleswhite/32/31_2.png) [@johnmyleswhite](https://discourse.julialang.org/u/johnmyleswhite)\
**Post date:** [September 12, 2020, 10:48am UTC](https://discourse.julialang.org/t/why-wont-julia-let-me-put-a-module-import-within-a-function/46504/3 "2020-09-12T10:48:19Z")

</div>

You could also write a macro:

```julia
macro operats()
    quote
        using GLPK
        using Clp
        using JuMP
        using Hungarian
    end
end

```

I’m not totally sure the origin of this restriction, but I imagine that it’s part of the general effort to prevent pathological dynamism like the hypothetical:

```julia
function import_cor()
     import Statistics: cor
end

function foo()
  cor = 10
  import_cor()
  return cor
end

```

---

<div class="post-metadata">

**Author:** ![Keno](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/keno/32/285_2.png) [@Keno](https://discourse.julialang.org/u/Keno)\
**Post date:** [September 12, 2020, 1:27pm UTC](https://discourse.julialang.org/t/why-wont-julia-let-me-put-a-module-import-within-a-function/46504/4 "2020-09-12T13:27:43Z")

</div>

`using` modified bindings in the current module scope. If you’re inside a function, you’re not in a module scope. You could define it to operate in the module scope of whatever module encloses the function, but that probably doesn’t do what you want, since you may be calling it from some other module. If you do really want that behavior, you can use `eval`. As was pointed out, the correct way to factor top-level code is to use include or a macro. @johnmyleswhite is correct of course that the reason we don’t have any sort of dynamic scope for these things is that that would be gratuitous dynamism without any real benefit.

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [September 12, 2020, 6:08pm UTC](https://discourse.julialang.org/t/why-wont-julia-let-me-put-a-module-import-within-a-function/46504/5 "2020-09-12T18:08:32Z")

</div>

Disallowing it also leaves open the possibility of making it work, i.e. by making those bindings visible only in the local scope.

---

<div class="post-metadata">

**Author:** ![fusion809](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fusion809/32/6080_2.png) [@fusion809](https://discourse.julialang.org/u/fusion809)\
**Post date:** [September 13, 2020, 4:57am UTC](https://discourse.julialang.org/t/why-wont-julia-let-me-put-a-module-import-within-a-function/46504/6 "2020-09-13T04:57:07Z")

</div>

I actually tried your method of using macros, and I get the exact same error. It seems like Julia doesn’t like using statements in macros either.

Interestingly, copy-pasting your code into the Julia REPL doesn’t give an error, but if you include it in `~/.julia/config/startup.jl` there are errors.

EDIT: Oops, not sure why but when I copy-paste your code exactly there’s no error, but when I write the code manually I get the same error as I do for including `using` in functions.

---

<div class="post-metadata">

**Author:** ![johnmyleswhite](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johnmyleswhite/32/31_2.png) [@johnmyleswhite](https://discourse.julialang.org/u/johnmyleswhite)\
**Post date:** [September 13, 2020, 11:03am UTC](https://discourse.julialang.org/t/why-wont-julia-let-me-put-a-module-import-within-a-function/46504/7 "2020-09-13T11:03:08Z")

</div>

What code did you write manually?

---

<div class="post-metadata">

**Author:** ![fusion809](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fusion809/32/6080_2.png) [@fusion809](https://discourse.julialang.org/u/fusion809)\
**Post date:** [September 13, 2020, 11:33am UTC](https://discourse.julialang.org/t/why-wont-julia-let-me-put-a-module-import-within-a-function/46504/8 "2020-09-13T11:33:06Z")

</div>

Good question, I’ve found the problem. I called the macros within functions, and that was what caused the error. I’ve since converted those functions into macros. Sorry for forgetting to mention that in my previous post. Thanks for your help, it’s much appreciated!
