# How to activate a package in a script?

**URL:** <https://discourse.julialang.org/t/how-to-activate-a-package-in-a-script/28185>\
**Category:** General Usage\
**Tags:** pkg\
**Created:** [August 29, 2019, 10:29pm UTC](https://discourse.julialang.org/t/how-to-activate-a-package-in-a-script/28185 "2019-08-29T22:29:22Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![joefowler](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joefowler/32/889_2.png) [@joefowler](https://discourse.julialang.org/u/joefowler)\
**Post date:** [August 29, 2019, 10:29pm UTC](https://discourse.julialang.org/t/how-to-activate-a-package-in-a-script/28185/1 "2019-08-29T22:29:22Z")

</div>

I admit that the following problem might arise only because my team is mixing two concepts–Package and Application–in a single Project. Still, let me pose the problem:

I have a [package “Pope”](https://github.com/usnistgov/Pope.jl) that serves mainly as a library of analysis tools (for superconducting x-ray microcalorimeter data) but also includes a few scripts to be run stand-alone, say, `scripts/quick_look_analysis.jl`. Suppose this script begins like this:

```julia
#!/usr/bin/env julia

using ArgParse
# Here follows code that parses command line arguments, in order to fail as quickly as possible.
# And now we do a "using" on the other needed packages
using Pope
# Then the code that uses the arguments and does physics.
Pope.dostuff()

```

Now, if I run the above, assuming I have not done a `Pkg.add("ArgParse")` in my default Julia 1.x environment, then the `using ArgParse` line will fail.

Here, on the other hand, is a 2-line fix that seems to work. It assumes that I can activate the package in which the script lives and thereby get access to `ArgParse`.

```julia
#!/usr/bin/env julia

using Pkg
Pkg.activate(normpath(joinpath(@ __DIR__ , "..")))
using ArgParse
# Here follows code that parses command line arguments, in order to fail as quickly as possible.
# And now we do a "using" on the other needed packages
using Pope
# Then the code that uses the arguments and does physics.
Pope.dostuff()

```

Does this seem a sensible way to get access to my package’s dependencies? Is there a better way? Is there a canonical way?

---

<div class="post-metadata">

**Author:** ![00vareladavid](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/00vareladavid/32/23521_2.png) [@00vareladavid](https://discourse.julialang.org/u/00vareladavid)\
**Post date:** [August 30, 2019, 3:15am UTC](https://discourse.julialang.org/t/how-to-activate-a-package-in-a-script/28185/2 "2019-08-30T03:15:43Z")

</div>

You can just make a regular old temporary directory to put your environment in. One of `Pkg`’s many strengths is that it separates the set of installed packages from the active environment. This means creating new environments is virtually free!

You can think of `Pkg.add` as “make sure these packages are available”. If they are already installed somewhere else, `Pkg` will know and you don’t have to reinstall them.

```julia
#!/usr/bin/env julia
import Pkg

tempdir = mktempdir()
Pkg.activate(tempdir)
Pkg.add(["ArgParse", "Pope"])

using ArgParse, Pope
Pope.dostuff()

```

---

<div class="post-metadata">

**Author:** ![ggggggggg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ggggggggg/32/265_2.png) [@ggggggggg](https://discourse.julialang.org/u/ggggggggg)\
**Post date:** [August 30, 2019, 3:02pm UTC](https://discourse.julialang.org/t/how-to-activate-a-package-in-a-script/28185/3 "2019-08-30T15:02:52Z")

</div>

Both of these solutions print out a lot of unwanted text… for example @00vareladavid’s solution on my computers starts by outputting:

```julia
>julijulia oneilg$ julia script.jl
  Updating registry at `~/.julia/registries/General`
  Updating git-repo `https://github.com/JuliaRegistries/General.git`
  Updating registry at `~/.julia/registries/QSGRegistry`
  Updating git-repo `https://github.com/ggggggggg/QSGRegistry.jl`
 Resolving package versions...

```

is there an easy way to suppress this output?

Also I think there is a `julia --project` flag, but I can’t find documentation for it.

---

<div class="post-metadata">

**Author:** ![00vareladavid](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/00vareladavid/32/23521_2.png) [@00vareladavid](https://discourse.julialang.org/u/00vareladavid)\
**Post date:** [August 30, 2019, 3:08pm UTC](https://discourse.julialang.org/t/how-to-activate-a-package-in-a-script/28185/4 "2019-08-30T15:08:49Z")

</div>

Not on the official binaries yet, but [https://github.com/JuliaLang/Pkg.jl/pull/1053](https://github.com/JuliaLang/Pkg.jl/pull/1053) just merged a few days ago. In the near future you will be able to do `Pkg.add("Example"; io=io)` to capture all `Pkg` output.

---

<div class="post-metadata">

**Author:** ![ggggggggg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ggggggggg/32/265_2.png) [@ggggggggg](https://discourse.julialang.org/u/ggggggggg)\
**Post date:** [August 30, 2019, 3:13pm UTC](https://discourse.julialang.org/t/how-to-activate-a-package-in-a-script/28185/5 "2019-08-30T15:13:39Z")

</div>

I notice you’re the author of the PR, so thanks. I can’t tell from the PR, will `Pkg.activate("ABC"; io=io)` work as well?

---

<div class="post-metadata">

**Author:** ![00vareladavid](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/00vareladavid/32/23521_2.png) [@00vareladavid](https://discourse.julialang.org/u/00vareladavid)\
**Post date:** [August 30, 2019, 3:16pm UTC](https://discourse.julialang.org/t/how-to-activate-a-package-in-a-script/28185/6 "2019-08-30T15:16:38Z")

</div>

Sure! It should work for all `Pkg` API commands.

---

<div class="post-metadata">

**Author:** ![joefowler](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joefowler/32/889_2.png) [@joefowler](https://discourse.julialang.org/u/joefowler)\
**Post date:** [August 30, 2019, 3:30pm UTC](https://discourse.julialang.org/t/how-to-activate-a-package-in-a-script/28185/7 "2019-08-30T15:30:35Z")

</div>

Thanks for the tips on the behavior of new Pkg. It has been difficult for me to make sense of the many changes, particularly the changes in philosophy, but I am getting there.

I like your temporary directory solution, and I will use it with or without the extra output. I’ll also look forward to the `Pkg.func("name"; io=io)` feature.

---

<div class="post-metadata">

**Author:** ![joefowler](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joefowler/32/889_2.png) [@joefowler](https://discourse.julialang.org/u/joefowler)\
**Post date:** [August 30, 2019, 4:11pm UTC](https://discourse.julialang.org/t/how-to-activate-a-package-in-a-script/28185/8 "2019-08-30T16:11:09Z")

</div>

One extra note: @00vareladavid didn’t know this, but`Pope.jl` happens to be a “local project”: it’s _not_ in the main Julia Registry. That means that the tempdir solution will not work in this specific case, because `Pkg` doesn’t know how to find my project. We would need to add our local registry with `Pkg.Registry.add()` for a complete solution (Julia 1.1 or later).

What to do for local projects like this? I see at least three solutions, but I only know how to make the first two work:

1. You could just accept that you are the kind of person who runs scripts with certain package dependencies (here, `ArgParse` and `Pope`), and you can add them to your default environment with `Pkg.add()` or `Pkg.dev()`. That one-time step should make this problem go away.
2. If don’t want to rely on everything being in the default environment, and if you are actively developing your local project and want to run the latest version of the script, then I’d suggest the solution I proposed in the original post: `Pkg.activate(normpath(joinpath(@ __DIR__ , "..")))`
3. If you have a local registry, and you are willing to use the latest _registered_ version instead of some local working copy, then the solution is going to require some registry wizardry. I do not have the foo I need to make this work. (And anyway, that really should be a separate discussion.)

---

<div class="post-metadata">

**Author:** ![hendri54](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hendri54/32/9621_2.png) [@hendri54](https://discourse.julialang.org/u/hendri54)\
**Post date:** [August 30, 2019, 7:32pm UTC](https://discourse.julialang.org/t/how-to-activate-a-package-in-a-script/28185/9 "2019-08-30T19:32:56Z")

</div>

On point 3 (custom registry), see [Creating a custom registry](https://discourse.julialang.org/t/creating-a-custom-registry/28007). One of the contributors has worked out a step by step guide.

---

<div class="post-metadata">

**Author:** ![joefowler](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joefowler/32/889_2.png) [@joefowler](https://discourse.julialang.org/u/joefowler)\
**Post date:** [August 30, 2019, 7:42pm UTC](https://discourse.julialang.org/t/how-to-activate-a-package-in-a-script/28185/10 "2019-08-30T19:42:56Z")

</div>

Thanks @hendri54. And I should point out that @ggggggggg is my Julia-using partner. He used this very guide (and your help here on Discourse) to get our private registry going. I think he and I could make point 3 work. Or at least, he could.

If choosing between idea 1 and 2, I guess the key question is whether you plan to use the script only on your own machine or you need to distribute it to a bunch of computers in various labs. Idea 1 is easiest and works the fastest at run-time, provided that you’re working at the same REPL day after day. Idea 2 is probably better if you need to set up the script to work immediately on multiple computers.

---

<div class="post-metadata">

**Author:** ![tkf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkf/32/17635_2.png) [@tkf](https://discourse.julialang.org/u/tkf)\
**Post date:** [August 30, 2019, 7:52pm UTC](https://discourse.julialang.org/t/how-to-activate-a-package-in-a-script/28185/11 "2019-08-30T19:52:04Z")

</div>

I usually have a script header like this

```julia
#!/bin/bash
#=
JULIA="${JULIA:-julia}"
JULIA_CMD="${JULIA_CMD:-$JULIA --color=yes --startup-file=no}"
export JULIA_PROJECT="$(dirname ${BASH_SOURCE[0]})"
export JULIA_LOAD_PATH=@:@stdlib # exclude default environment
exec $JULIA_CMD -e 'include(popfirst!(ARGS))' "${BASH_SOURCE[0]}" "$@"
=#

using MyPackage
...

```

It’s equivalent to `Pkg.activate` but:

- Setting up `JULIA_PROJECT` outside Julia makes it easy to invoke the script in different ways (e.g., using another project)
- `JULIA_LOAD_PATH=@:@stdlib` is important if you want to avoid accidentally relying on your default environment.

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [August 30, 2019, 8:27pm UTC](https://discourse.julialang.org/t/how-to-activate-a-package-in-a-script/28185/12 "2019-08-30T20:27:09Z")

</div>

> [@tkf](#):
>
> `JULIA_LOAD_PATH=@:@stdlib` is important if you want to avoid accidentally relying on your default environment.

You might even consider leaving out stdlib here.

---

<div class="post-metadata">

**Author:** ![00vareladavid](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/00vareladavid/32/23521_2.png) [@00vareladavid](https://discourse.julialang.org/u/00vareladavid)\
**Post date:** [August 30, 2019, 8:28pm UTC](https://discourse.julialang.org/t/how-to-activate-a-package-in-a-script/28185/13 "2019-08-30T20:28:46Z")

</div>

Ah, I see.

In the case where the package is unregistered and you want to distribute a _single_ script file, you can just add by URL directly:

```julia
tempdir = mktempdir()
Pkg.activate(tempdir) # activate a temporary directory
Pkg.add("ArgParse")
Pkg.add(Pkg.PackageSpec(url="https://github.com/usnistgov/Pope.jl")) # add by URL directly

```

If you want to distribute the whole directory, then I would agree with your option 2.

---

<div class="post-metadata">

**Author:** ![hendri54](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hendri54/32/9621_2.png) [@hendri54](https://discourse.julialang.org/u/hendri54)\
**Post date:** [August 31, 2019, 1:18am UTC](https://discourse.julialang.org/t/how-to-activate-a-package-in-a-script/28185/14 "2019-08-31T01:18:56Z")

</div>

The tricky bit of that last proposal is to ensure that the right version of `Pope.jl` is used.  
The first time this is run for a given Julia installation, the `Pkg.add` will download the package from github. But when you run the same script for the second time, I don’t think one can guarantee that the package is re-downloaded and recompiled.  
Two approaches that seem to work:

1. `rsync` the code to the remote machine and `dev` it everywhere.
2. Create your own registry and `Pkg.update` to the latest version (or `Pkg.add` a fixed version, if desired).

If there is an easier way, I would love to learn it.

---

<div class="post-metadata">

**Author:** ![tkf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkf/32/17635_2.png) [@tkf](https://discourse.julialang.org/u/tkf)\
**Post date:** [August 31, 2019, 1:40am UTC](https://discourse.julialang.org/t/how-to-activate-a-package-in-a-script/28185/15 "2019-08-31T01:40:59Z")

</div>

Why not use `Manifest.toml`? You don’t even need to create registries for non-registered packages.

---

<div class="post-metadata">

**Author:** ![hendri54](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hendri54/32/9621_2.png) [@hendri54](https://discourse.julialang.org/u/hendri54)\
**Post date:** [August 31, 2019, 2:14pm UTC](https://discourse.julialang.org/t/how-to-activate-a-package-in-a-script/28185/16 "2019-08-31T14:14:51Z")

</div>

In my experience, I cannot get Julia to update unregistered packages (unless they are `dev`’d of course). That would be on the remote computer.

The workflow that I have in mind is: Edit on the local computer. Upload main package and unregistered dependencies to the remote (using `rsync`). Launch computations on the remote via a script that ensures (somehow) that the most recently uploaded versions of all dependencies are used.

Again, there may well be a way to do all this without a registry that I don’t know about.

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [August 31, 2019, 2:22pm UTC](https://discourse.julialang.org/t/how-to-activate-a-package-in-a-script/28185/17 "2019-08-31T14:22:25Z")

</div>

> [@hendri54](#):
>
> In my experience, I cannot get Julia to update unregistered packages (unless they are `dev` 'd of course). That would be on the remote computer.

`Pkg.update()` updates all packages that are not deved so that should work just fine.

---

<div class="post-metadata">

**Author:** ![hendri54](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hendri54/32/9621_2.png) [@hendri54](https://discourse.julialang.org/u/hendri54)\
**Post date:** [August 31, 2019, 2:48pm UTC](https://discourse.julialang.org/t/how-to-activate-a-package-in-a-script/28185/18 "2019-08-31T14:48:11Z")

</div>

My comment referred to

> [@00vareladavid](#):
>
> In the case where the package is unregistered and you want to distribute a _single_ script file, you can just add by URL directly

There, the package is unregistered and simply using `add` on the remote would not ensure that the latest uploaded code is compiled. And `update` would ignore the unregistered dependency.

Hence, I either need to register all the dependencies (and maintain a private registry); then I can update.  
Or I need to `dev` the dependencies on the remote as well.

Unless there is a simpler way that I am unaware of… such as forcing a recompile of certain packages.

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [August 31, 2019, 3:11pm UTC](https://discourse.julialang.org/t/how-to-activate-a-package-in-a-script/28185/19 "2019-08-31T15:11:18Z")

</div>

> [@hendri54](#):
>
> There, the package is unregistered and simply using `add` on the remote would not ensure that the latest uploaded code is compiled.

Why not?

> [@hendri54](#):
>
> And `update` would ignore the unregistered dependency.

No, if you have added by URL as David suggested Pkg will update it to latest master, otherwise it is a bug.

---

<div class="post-metadata">

**Author:** ![hendri54](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hendri54/32/9621_2.png) [@hendri54](https://discourse.julialang.org/u/hendri54)\
**Post date:** [August 31, 2019, 3:40pm UTC](https://discourse.julialang.org/t/how-to-activate-a-package-in-a-script/28185/20 "2019-08-31T15:40:28Z")

</div>

Sorry about that misunderstanding. I had convinced myself (by running examples) that `add` did not always download and recompile the latest master. This simplifies matters quite a bit.

[Next page](https://discourse.julialang.org/t/how-to-activate-a-package-in-a-script/28185.md?page=2)
