# How can I avoid uselessly adding modules to an environment?

**URL:** <https://discourse.julialang.org/t/how-can-i-avoid-uselessly-adding-modules-to-an-environment/125043>\
**Category:** General Usage\
**Tags:** question, package\
**Created:** [January 21, 2025, 8:40pm UTC](https://discourse.julialang.org/t/how-can-i-avoid-uselessly-adding-modules-to-an-environment/125043 "2025-01-21T20:40:07Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![tb877](https://avatars.discourse-cdn.com/v4/letter/t/d26b3c/32.png) [@tb877](https://discourse.julialang.org/u/tb877)\
**Post date:** [January 21, 2025, 8:40pm UTC](https://discourse.julialang.org/t/how-can-i-avoid-uselessly-adding-modules-to-an-environment/125043/1 "2025-01-21T20:40:07Z")

</div>

Sorry for the confusing title, but I wasn’t sure how to say it better.

I’m currently developing some code that I’m running on an HPC. In order for the code to be easy to use and reproducible, I’ve made it into a module, and included an environment (i.e., a `project.toml`). When I want to compute things, I load julia with `julia --project=.` and load my module with `using MyModule`.

However I’ve found out that over time, whenever I did this _and also loaded other Julia modules to experiment with new stuff_, these were added to the `project.toml`. I now had a bunch of unused modules in my environment, which I’ve fixed using `Aqua.test_stale_deps(MyModule)`.

How can I prevent adding useless stuff to my `project.toml` over time? Is there a way to avoid this? Should I just track changes to `project.toml` before pushing commits? Or should I instead clean up my `project.toml` of obsolete/unused dependencies periodically?

---

<div class="post-metadata">

**Author:** ![jlapeyre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jlapeyre/32/4514_2.png) [@jlapeyre](https://discourse.julialang.org/u/jlapeyre)\
**Post date:** [January 21, 2025, 9:11pm UTC](https://discourse.julialang.org/t/how-can-i-avoid-uselessly-adding-modules-to-an-environment/125043/2 "2025-01-21T21:11:20Z")

</div>

I wrote a package to help with this. It should appear in the General Registry in a day or two.

You can put the following in a script, say in `./extra/extra.jl`, then do `include("./extra/extra.jl")`

```julia
using StackEnvs
ensure_in_stack("my_env", ["PackA", "PackB"])

```

When you want to use another package: 1) add it to the list, say, after `"PackB"`. 2) Run `include("./extra/extra.jl")` again. (Or maybe use Revise and `includet`.)

Alternatively, you can look in the package to see what’s happening and do something similar yourself.

I also have found it difficult in the past, especially if I go quite a while without engaging with Julia much. One thing I kept trying to do was, 1) activate an environment with some “development” packages (that I don’t want in my default environment) 2) `use APackage` from that environment. 3) Re-activate the environment of the package I am working on. But `Pkg` does not work well that way. I think it’s not intended to work that way. (Issues arise that I won’t go into much here. To name one, if you for some reason do `use APackage` again, you’ll get an error because it’s not in any environment in your `LOAD_PATH`)

> **[GitHub - jlapeyre/StackEnvs.jl: Simple manipulation of an environment in your...](https://github.com/jlapeyre/StackEnvs.jl)**
>
> Simple manipulation of an environment in your stack of environments

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [January 21, 2025, 9:17pm UTC](https://discourse.julialang.org/t/how-can-i-avoid-uselessly-adding-modules-to-an-environment/125043/3 "2025-01-21T21:17:28Z")

</div>

> [@tb877](#):
>
> How can I prevent adding useless stuff to my `project.toml` over time?

Just remove what you do not need any more manually.

---

<div class="post-metadata">

**Author:** ![jlapeyre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jlapeyre/32/4514_2.png) [@jlapeyre](https://discourse.julialang.org/u/jlapeyre)\
**Post date:** [January 21, 2025, 9:25pm UTC](https://discourse.julialang.org/t/how-can-i-avoid-uselessly-adding-modules-to-an-environment/125043/4 "2025-01-21T21:25:54Z")

</div>

> [@ufechner7](#):
>
> Just remove what you do not need any more manually.

This is what I’ve done in the past. It’s a chore, and requires remembering to clean up the Project.toml exactly as it was in the last commit before committing again. Even if you do it correctly, it still takes effort. And if you are still using the experimental package, you’ll have to… IDK, remove it from Project.toml, commit, push, add it back to Project.toml.

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [January 21, 2025, 9:40pm UTC](https://discourse.julialang.org/t/how-can-i-avoid-uselessly-adding-modules-to-an-environment/125043/5 "2025-01-21T21:40:37Z")

</div>

I mean, how many packages do you try out per week? For me it is not more than one per week. If you try out a new package you must also learn how to use it. That might need 30 min or more. Removing it from Project.toml takes 30s or less.

---

<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:** [January 21, 2025, 10:08pm UTC](https://discourse.julialang.org/t/how-can-i-avoid-uselessly-adding-modules-to-an-environment/125043/6 "2025-01-21T22:08:56Z")

</div>

Just don’t install packages that you want to try out/experiment into your persistent environment(s)!

Julia has this neat feature where it suggests you to automatically install a package when doing `using MyPackage`. For the most convenience, add

```julia
insert!(LOAD_PATH, 2, mktempdir())

```

to your `~/.julia/config/startup.jl`. Then, the workflow is simply

- type `using SomePackage`
- press “o”, Enter, Enter:

```julia
julia> using Accessors
 │ Package Accessors not found, but a package named Accessors is available from a registry. 
 │ Install package?
 │ (@v1.10) pkg> add Accessors 
# press "o" and Enter
 └ Select environment:
 > 1: `/var/folders/2j/9vtd991d201dbkh9dnx3wvy00000gq/T/jl_pUi3Ag` (/var/folders/2j/9vtd991d201dbkh9dnx3wvy00000gq/T/jl_pUi3Ag)
   2: `~/.julia/environments/v1.10/Project.toml` (@v#.#)
# press Enter
   Resolving package versions...
    Updating `/private/var/folders/2j/9vtd991d201dbkh9dnx3wvy00000gq/T/jl_pUi3Ag/Project.toml`
  [7d9f7c33] + Accessors v0.1.41
    Updating `/private/var/folders/2j/9vtd991d201dbkh9dnx3wvy00000gq/T/jl_pUi3Ag/Manifest.toml`
  [7d9f7c33] + Accessors v0.1.41
  [a33af91c] + CompositionsBase v0.1.2
  [187b0558] + ConstructionBase v1.5.8
  [3587e190] + InverseFunctions v0.1.17
  [1914dd2f] + MacroTools v0.5.15
  [ade2ca70] + Dates
  [de0858da] + Printf
  [4ec0a83e] + Unicode
# the package is installed and loaded

```

No need for any extra packages or setup, no permanent environment changes.

---

<div class="post-metadata">

**Author:** ![jlapeyre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jlapeyre/32/4514_2.png) [@jlapeyre](https://discourse.julialang.org/u/jlapeyre)\
**Post date:** [January 22, 2025, 2:11am UTC](https://discourse.julialang.org/t/how-can-i-avoid-uselessly-adding-modules-to-an-environment/125043/7 "2025-01-22T02:11:58Z")

</div>

Doing it by hand works well for some situations. Doing this `insert!(LOAD_PATH, 2, mktempdir())` etc. works well for some situations.

But if you think about it for a couple of seconds, you’ll realize that they and `StackEnv` are not the same in terms of function and convenience. I won’t claim, especially without thinking it through, that `StackEnv` is perfect for whatever workflow you have. But there are use cases, where it seems better. Like, I have a dozen packages that I have found I use when developing something. And I like the TAB completion when doing `using ...`. Or I want to see the list of packages as a reminder. Or I appreciate the smaller amount of friction.

Or, I want to load the packages immediately, so I load this from a script.

```julia
using StackEnvs
ensure_in_stack("myenv2", [:StatsBase, :PrettyTables])
using StatsBase, PrettyTables

```

Not only would there be more friction if I loaded this

```julia
using StatsBase, PrettyTables

```

and I were prompted for which environment I wanted. In fact, it fails, saying the package can’t be found.

---

<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:** [January 22, 2025, 3:00am UTC](https://discourse.julialang.org/t/how-can-i-avoid-uselessly-adding-modules-to-an-environment/125043/8 "2025-01-22T03:00:34Z")

</div>

Sure, the Julia auto-install is not a universal solution for all problems 🙂 I don’t doubt you wrote a dedicated package because it better solves some specific issues you have.

The builtin autoinstall is just a setup-free (almost, see/support [these](https://github.com/JuliaLang/julia/issues/46231) [issues](https://github.com/JuliaLang/Pkg.jl/issues/3126)) convenient way to use any packages temporarily without cluttering existing envs. Works seamlessly in my experience, and I agree autocomplete would be useful there (see the [existing issue](https://github.com/JuliaLang/julia/issues/51713)).

With a bit more setup, one can even avoid typing package names or any using/include commands for commonly-used functions. Thanks to @Lilith’s BasicAutoloads.jl.  
This is my startup.jl file using this package: [startup.jl with BasicAutoloads for many useful packages · GitHub](https://gist.github.com/aplavin/4643471069ae2fed8356ebcd73d419d2).  
It’s so nice to just type, eg, `123u"km/h" |> u"m/s"` and it automatically installs and loads Unitful!

---

<div class="post-metadata">

**Author:** ![jlapeyre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jlapeyre/32/4514_2.png) [@jlapeyre](https://discourse.julialang.org/u/jlapeyre)\
**Post date:** [January 24, 2025, 5:19am UTC](https://discourse.julialang.org/t/how-can-i-avoid-uselessly-adding-modules-to-an-environment/125043/9 "2025-01-24T05:19:01Z")

</div>

I use Julia’s auto install and I also use BasicAutoloads. They are great tools.

I also use StackEnvs because it’s a different tool that is useful in different situations. I personally in no way find them a substitute for StackEnvs.

Given that the original question is rather general, it’s definitely worth bringing up auto install and BasicAutoloads in this thread.

---

<div class="post-metadata">

**Author:** ![tb877](https://avatars.discourse-cdn.com/v4/letter/t/d26b3c/32.png) [@tb877](https://discourse.julialang.org/u/tb877)\
**Post date:** [March 17, 2025, 2:23pm UTC](https://discourse.julialang.org/t/how-can-i-avoid-uselessly-adding-modules-to-an-environment/125043/10 "2025-03-17T14:23:19Z")

</div>

Hey all, thanks for the replies. Sorry I had not followed up since I was busy with other things.

Actually I think my question was due to the fact that I’m also relatively new to using virtual environments/project files.

And since I’m also new to Julia, trying out a bunch of plotting modules in the same week made me think the `project.toml` file could become bloated pretty quickly.

Actually any language faces similar issues. I just tried searching for solutions people faced with Python’s `requirements.txt` files. I guess the simplest answer is the remove the modules we don’t use anymore as @ufechner7 mentioned.

Another solution Python folks use is `pipreqs`, which scan a project directory for `import` commands and build a new corresponding `requirements.txt` file. This is actually the opposite of what I had done with `Aqua`, i.e. taking a `project.toml` and scanning for stale dependencies.

Anyway, in my case I think the simplest solution will be to edit the `project.toml` file manually or use `Aqua` again on a semi-regular basis to make sure the list of modules stays minimal. Also, I guess that avoiding to load the project’s environment when it’s not absolutely necessary (e.g., when I’m just doing data post-processing or plotting) might help avoid these issues.

---

<div class="post-metadata">

**Author:** ![tb877](https://avatars.discourse-cdn.com/v4/letter/t/d26b3c/32.png) [@tb877](https://discourse.julialang.org/u/tb877)\
**Post date:** [March 17, 2025, 2:50pm UTC](https://discourse.julialang.org/t/how-can-i-avoid-uselessly-adding-modules-to-an-environment/125043/11 "2025-03-17T14:50:16Z")

</div>

Addendum: I’ve mentioned `pipreqs`, which has no equivalent in Julia. I guess the most straightforward way to generate if needed a (clean) `project.toml` with only the required modules based on explicit imports in a project would be to use `Pkg.instantiate`.
