# Import only when code used?

**URL:** <https://discourse.julialang.org/t/import-only-when-code-used/100266>\
**Category:** General Usage\
**Tags:** question\
**Created:** [June 13, 2023, 5:23am UTC](https://discourse.julialang.org/t/import-only-when-code-used/100266 "2023-06-13T05:23:26Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![nharr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nharr/32/24698_2.png) [@nharr](https://discourse.julialang.org/u/nharr)\
**Post date:** [June 13, 2023, 5:23am UTC](https://discourse.julialang.org/t/import-only-when-code-used/100266/1 "2023-06-13T05:23:26Z")

</div>

Is there a way to import a module only if certain code is used?

I ideally I would put it in a function, but it’s not allowed:

```julia
function initialize()
    using A
    # create objects using functionality from A
    # to be used throughout the rest of the program
end

```

Background:  
My program initializes some data structures and then runs an algorithm on the data. I have a file holding all the initialize functions of various kinds. One of them sets up a ROS interface through RobotOS.jl for the needed data. If python + rospy isn’t installed on the system, just importing this will error out the whole program. In other cases it still adds to the startup time even when its functionality is never used.

Any good ideas on keeping the extra package from being imported without resorting to commenting out the code?

---

<div class="post-metadata">

**Author:** ![gdalle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdalle/32/27854_2.png) [@gdalle](https://discourse.julialang.org/u/gdalle)\
**Post date:** [June 13, 2023, 5:27am UTC](https://discourse.julialang.org/t/import-only-when-code-used/100266/2 "2023-06-13T05:27:05Z")

</div>

Could you use package extensions for this? I’m not sure it fits your application, since it allows conditional code loading based on whether or not a _package_ is present in the environment

[https://pkgdocs.julialang.org/dev/creating-packages/#Conditional-loading-of-code-in-packages-(Extensions)](https://pkgdocs.julialang.org/dev/creating-packages/#Conditional-loading-of-code-in-packages-(Extensions))

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [June 13, 2023, 5:49am UTC](https://discourse.julialang.org/t/import-only-when-code-used/100266/3 "2023-06-13T05:49:36Z")

</div>

> [@nharr](#):
>
> If python + rospy isn’t installed on the system, just importing this will error out the whole program

if we’re talking about loading python pkg on the fly specifically, what I like to do is something like

```julia
function initialize()
    rospy = pyimport("rospy")
end

```

otherwise you just have to do it in ` __init__ ()` of your package

---

<div class="post-metadata">

**Author:** ![Per](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/per/32/10387_2.png) [@Per](https://discourse.julialang.org/u/Per)\
**Post date:** [June 13, 2023, 5:51am UTC](https://discourse.julialang.org/t/import-only-when-code-used/100266/4 "2023-06-13T05:51:25Z")

</div>

You can do:

```julia
function initialize()
    @eval begin
       using A
       # create objects using functionality from A
       # to be used throughout the rest of the program
    end
end

```

Code inside an `@eval` will run in the global scope, so any objects that you create will end up there (and not local to the `initialize` function).

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [June 13, 2023, 9:02am UTC](https://discourse.julialang.org/t/import-only-when-code-used/100266/5 "2023-06-13T09:02:06Z")

</div>

What do you mean by “when code used”, what are the conditions for something like `initialize` to run, and what sort of things does `initialize` do? Things that only need to be done once and can’t be undone, like a system-conditional import, aren’t usually put in functions that can be called repeatedly. Best practice really depends on what you need to do, and there’s no shortage of ways to conditionally evaluate code in the global scope: `@eval` expressions, `include` files, even a simple if statement.

For example, the `initialize`-`@eval` function would only evaluate the expression in the module that `initialize` belongs to. If you want to `eval`uate that expression in specified modules, you would need to add an argument to provide the module that you import `initialize` into. If conditional code is in separate files, `include` is nice because you specify what module to run by simply running it in that module’s scope.

---

<div class="post-metadata">

**Author:** ![nharr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nharr/32/24698_2.png) [@nharr](https://discourse.julialang.org/u/nharr)\
**Post date:** [June 14, 2023, 2:13am UTC](https://discourse.julialang.org/t/import-only-when-code-used/100266/6 "2023-06-14T02:13:40Z")

</div>

Thanks for all the responses 🙂. They gave me some good options to try out.

`@eval` within the function worked and was the simplest fix for the time being. I feel like creating a package extension may eventually be the better solution if I can re-organize my code.

Further clarifications: My initialization file holds all the functions I use to initialize various data for the main algorithm. Only one of them is run and only once at the start of the program. Every function returns the same data structures, just with different values.
