# \`using SomePackage\` in block

**URL:** <https://discourse.julialang.org/t/using-somepackage-in-block/85314>\
**Category:** General Usage\
**Created:** [August 4, 2022, 7:31pm UTC](https://discourse.julialang.org/t/using-somepackage-in-block/85314 "2022-08-04T19:31:45Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![TheLateKronos](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thelatekronos/32/12824_2.png) [@TheLateKronos](https://discourse.julialang.org/u/TheLateKronos)\
**Post date:** [August 4, 2022, 7:31pm UTC](https://discourse.julialang.org/t/using-somepackage-in-block/85314/1 "2022-08-04T19:31:45Z")

</div>

How can I call `using SomePackage` to load a package inside a block? Be it `begin`, a function, or any other block.

Here is my attempts at loading GLMakie in a block:

```julia
julia> begin
           using GLMakie
           L"f(x)"
       end
ERROR: LoadError: UndefVarError: @L_str not defined
in expression starting at REPL[2]:3

```

But the same code works when the lines are run one at a time.

```julia
julia> using GLMakie

julia> L"f(x)"
L"$f(x)$"

```

I would also like to load `Pkg` as part of some sysimage generating function to programmatically update packages.

---

<div class="post-metadata">

**Author:** ![mikmoore](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mikmoore/32/31109_2.png) [@mikmoore](https://discourse.julialang.org/u/mikmoore)\
**Post date:** [August 4, 2022, 9:00pm UTC](https://discourse.julialang.org/t/using-somepackage-in-block/85314/2 "2022-08-04T21:00:45Z")

</div>

You cannot load a package within a `function`, `begin`, `let`, etc scope. Packages can only be loaded at “top level”. This means the top-level of a module scope or in the REPL (which is actually a module called `Main`). I am not aware of any additional places, but perhaps someone else can list more.

Loading a package is potentially side-effectful. It changes the method tables for any functions it extends. I suspect this is why it cannot be done within a narrow scope. For that matter, loading a module anywhere _still_ affects those same extended functions in all scopes.

Why is it necessary for you to load a package within a narrow scope? Perhaps we can suggest a reorganization that will help alleviate whatever issue you perceive. Are you primarily concerned with namespaces?

EDIT: I was mistaken. You can load a module within a `begin` or `let` block (but not a function). But it will not contain the module to just that block. It will be as if you had loaded it outside the block.

```julia
julia> SVector(1,2,3)
ERROR: UndefVarError: SVector not defined
Stacktrace:
 [1] top-level scope
   @ REPL[25]:1

julia> begin; using StaticArrays; SVector(1,2,3); end
3-element SVector{3, Int64} with indices SOneTo(3):
 1
 2
 3

julia> SVector(1,2,3) # StaticArrays loaded within the entire module
3-element SVector{3, Int64} with indices SOneTo(3):
 1
 2
 3

```

---

<div class="post-metadata">

**Author:** ![aplavin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aplavin/32/222056_2.png) [@aplavin](https://discourse.julialang.org/u/aplavin)\
**Post date:** [August 4, 2022, 9:50pm UTC](https://discourse.julialang.org/t/using-somepackage-in-block/85314/3 "2022-08-04T21:50:14Z")

</div>

You can load a package in a block without any issues, but not in a function. Your example just happens to use a macro from the package in the same block it was imported: _this_ is not possible. A block is parsed as a single unit, and macros have to be expanded beforehand.  
Functions from loaded packages can be used immediately, for example.

---

<div class="post-metadata">

**Author:** ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)\
**Post date:** [August 4, 2022, 10:48pm UTC](https://discourse.julialang.org/t/using-somepackage-in-block/85314/4 "2022-08-04T22:48:25Z")

</div>

The problem is that in a regular `block`, julia is macroexpanding the whole thing at once, so it tries to expand the macro before it gets loaded.

One way around this would be to use this:

```julia
macro toplevel(ex::Expr)
    if ex.head == :block
        ex.head = :toplevel
        return esc(ex)
    else
        throw(ArgumentError("@toplevel must be used on `begin ... end` blocks"))
    end
end

```

Now, writing

```julia
@toplevel begin
    using GLMakie
    L"f(x)"
end

```

should work.
