# Execute consecutive scripts

**URL:** https://discourse.julialang.org/t/execute-consecutive-scripts/137064
**Category:** General Usage
**Tags:** question, scripting
**Created:** [May 10, 2026, 7:20am UTC](https://discourse.julialang.org/t/execute-consecutive-scripts/137064 "2026-05-10T07:20:44Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![Chrysoberyl](https://avatars.discourse-cdn.com/v4/letter/c/ed655f/32.png) [@Chrysoberyl](https://discourse.julialang.org/u/Chrysoberyl)
#### Post date: [May 10, 2026, 7:20am UTC](https://discourse.julialang.org/t/execute-consecutive-scripts/137064/1 "2026-05-10T07:20:44Z")

</div>

Is there a simple way to execute either multiple scripts consecutively, or execute a script with some additional imports? Consider this use case: I have a NN training script, and if I import `CUDA`, it trains on the GPU. Currently the script looks like this:

```julia
#import CUDA
using Flux, StatsBase
...

```

When I want to run this on the GPU, I uncomment the first line, but this is cumbersome. Ideally I want to trigger the GPU behaviour by specifying `import CUDA` in the command line:

```sh
julia --eval "import CUDA" ./nn.jl

```

but this does not achieve my objective.

---

<div class="post-metadata">

### Author: ![eldee](https://avatars.discourse-cdn.com/v4/letter/e/b5a626/32.png) [@eldee](https://discourse.julialang.org/u/eldee)
#### Post date: [May 10, 2026, 7:40am UTC](https://discourse.julialang.org/t/execute-consecutive-scripts/137064/2 "2026-05-10T07:40:30Z")

</div>

This sounds like an [XY problem](https://en.wikipedia.org/wiki/XY_problem). For your example the most straightforward approach would be to use `ARGS` (or e.g. ArgParse.jl) to supply an option (like `--device CUDA`) via the command-line. See also [Command-line Interface · The Julia Language](https://docs.julialang.org/en/v1/manual/command-line-interface/).

---

<div class="post-metadata">

### Author: ![Chrysoberyl](https://avatars.discourse-cdn.com/v4/letter/c/ed655f/32.png) [@Chrysoberyl](https://discourse.julialang.org/u/Chrysoberyl)
#### Post date: [May 10, 2026, 8:08am UTC](https://discourse.julialang.org/t/execute-consecutive-scripts/137064/3 "2026-05-10T08:08:56Z")

</div>

This isn’t an XY problem. In my setup I have a master project that has no GPU package dependency, and there are derived projects based on CUDA and AMDGPU. The reason I do this is one monolithic project having all the CUDA/AMDGPU dependencies would have to download a ton of artefacts and create huge docker images.

I don’t think you can conditionally import a package. If you import the CUDA package while in the AMDGPU or master project environment it will give you an error. This is why I don’t think the command line approach would work.

---

<div class="post-metadata">

### Author: ![eldee](https://avatars.discourse-cdn.com/v4/letter/e/b5a626/32.png) [@eldee](https://discourse.julialang.org/u/eldee)
#### Post date: [May 10, 2026, 9:04am UTC](https://discourse.julialang.org/t/execute-consecutive-scripts/137064/4 "2026-05-10T09:04:15Z")

</div>

Could you provide a MWE showing what the environment structure looks like, and how `nn.jl` makes the switch between CUDA, AMDGPU and the CPU fallback?

Perhaps you could write package extensions for CUDA.jl and AMDGPU.jl?

---

<div class="post-metadata">

### Author: ![Ralph\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ralph_smith/32/10344_2.png) [@Ralph\_Smith](https://discourse.julialang.org/u/Ralph_Smith)
#### Post date: [May 10, 2026, 10:21pm UTC](https://discourse.julialang.org/t/execute-consecutive-scripts/137064/5 "2026-05-10T22:21:21Z")

</div>

Does

```julia-auto
julia --eval 'import CUDA; include("./nn.jl")'

```

work for you?

---

<div class="post-metadata">

### Author: ![abraemer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abraemer/32/51403_2.png) [@abraemer](https://discourse.julialang.org/u/abraemer)
#### Post date: [May 11, 2026, 8:51am UTC](https://discourse.julialang.org/t/execute-consecutive-scripts/137064/6 "2026-05-11T08:51:07Z")

</div>

> [@Chrysoberyl](#):
>
> I don’t think you can conditionally import a package.

Maybe package extensions could solve your issue elegantly.  
Tutorial:

> [@Quick tutorial on package extensions](https://discourse.julialang.org/t/quick-tutorial-on-package-extensions/130923):
>
> Hello! I was reading the documentation on package extensions and felt that I needed some more detailed instructions. I wrote this tutorial based on what I learned (only today, so this is surely not an exhaustive tutorial). Quick tutorial on package extensions A package can have modules that are conditionally loaded based on what other packages are loaded. These are called extensions. Goal In the below text, there will be a package MyPackage. It will have an extension module MyExt which should …

---

<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: [May 11, 2026, 11:59am UTC](https://discourse.julialang.org/t/execute-consecutive-scripts/137064/7 "2026-05-11T11:59:27Z")

</div>

> [@Chrysoberyl](#):
>
> I don’t think you can conditionally import a package.

Yes you can.

```julia-auto
julia> if false; import LinearAlgebra; end

julia> isdefined(Main, :LinearAlgebra)
false

julia> if true; import LinearAlgebra; end

julia> isdefined(Main, :LinearAlgebra)
true

```

---

<div class="post-metadata">

### Author: ![Chrysoberyl](https://avatars.discourse-cdn.com/v4/letter/c/ed655f/32.png) [@Chrysoberyl](https://discourse.julialang.org/u/Chrysoberyl)
#### Post date: [May 13, 2026, 6:44am UTC](https://discourse.julialang.org/t/execute-consecutive-scripts/137064/8 "2026-05-13T06:44:54Z")

</div>

afaik `Flux.jl` automatically switches to CUDA/AMDGPU mode when CUDA/AMDGPU is imported
