# Generate Temporary Environment like Pkg.test

**URL:** https://discourse.julialang.org/t/generate-temporary-environment-like-pkg-test/87981
**Category:** General Usage
**Tags:** pkg
**Created:** [September 29, 2022, 11:28am UTC](https://discourse.julialang.org/t/generate-temporary-environment-like-pkg-test/87981 "2022-09-29T11:28:21Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![MatthijsCox](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/matthijscox/32/42831_2.png) [@MatthijsCox](https://discourse.julialang.org/u/MatthijsCox)
#### Post date: [September 29, 2022, 11:28am UTC](https://discourse.julialang.org/t/generate-temporary-environment-like-pkg-test/87981/1 "2022-09-29T11:28:21Z")

</div>

The [Pkg.test documentation](https://pkgdocs.julialang.org/v1/api/#Pkg.test) states:  
_The tests are run by generating a temporary environment with only `pkg` and its (recursive) dependencies in it._

I love this ability, it’s a entire setup and teardown of an environment, completely isolated from the environment you are currently in. I want to know how I can do this myself from within a Julia REPL?

It’s not the same as doing `Pkg.activate()`, because that activated environment still exists within the current global REPL environment and all of it’s history.

I will try to read the source code of Pkg.test to find the answer, but maybe someone here knows the answer already.

I guess I would need to:

- Choose an environment (directory with Project.toml and optionally Manifest.toml)
- Pkg.instantiate it
- Startup Julia from within Julia with that environment activated, without enabling a global environment like `@v1.8`? Maybe also without startup.jl…
- run my desired code
- Shutdown the environment and return back to original Julia environment

For example I would like to do this for doctesting.

---

<div class="post-metadata">

### Author: ![ericphanson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ericphanson/32/215186_2.png) [@ericphanson](https://discourse.julialang.org/u/ericphanson)
#### Post date: [September 29, 2022, 12:28pm UTC](https://discourse.julialang.org/t/generate-temporary-environment-like-pkg-test/87981/2 "2022-09-29T12:28:23Z")

</div>

You can use the environment variable `JULIA_LOAD_PATH` and the `--startup-file` command-line flag to get this behavior.

In bash, it looks like this:

```bash

JULIA_LOAD_PATH="@" julia --project=. --startup-file=no run.jl

```

What this does:

- sets [`JULIA_LOAD_PATH`](https://docs.julialang.org/en/v1/manual/environment-variables/#JULIA_LOAD_PATH) so that Julia will only use the local Project.toml for finding packages, rather than also the global environment.

- sets [`--startup-file=no`](https://docs.julialang.org/en/v1/manual/command-line-options/#Command-line-switches-for-Julia) so that the user’s `startup.jl` won’t affect the results.

- sets the project to the current directory.

---

<div class="post-metadata">

### Author: ![digital\_carver](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/digital_carver/32/33818_2.png) [@digital\_carver](https://discourse.julialang.org/u/digital_carver)
#### Post date: [September 29, 2022, 12:32pm UTC](https://discourse.julialang.org/t/generate-temporary-environment-like-pkg-test/87981/3 "2022-09-29T12:32:27Z")

</div>

You can create a temporary environment with

```julia
]activate --temp

```

or

```julia
Pkg.activate(; temp = true)

```

> [@MatthijsCox](#):
>
> It’s not the same as doing `Pkg.activate()`, because that activated environment still exists within the current global REPL environment and all of it’s history.

I’m not sure what you mean here. The base Julia environment wouldn’t be active anymore since you’ve activated a different (temp) one, but maybe you mean the package `using`s and objects created in the REPL so far?  
In that case, yeah, you’d have to create a new fresh Julia process for this environment.

You can have a file, let’s say `doctestsetup.jl`, with something like:

```julia
import Pkg
Pkg.activate(; temp = true)
Pkg.add("SomePackage")

```

and then execute

```julia
julia --startup-file=no -L doctestsetup.jl yourcodetotest.jl

```

(the `startup-file` option shouldn’t be strictly necessary, since any code in startup.jl that you don’t want to run for normal files should be inside an `atreplinit` block anyway, but let’s put that aside here.)

The temporary environment will be automatically cleaned up when that Julia process exits.

---

<div class="post-metadata">

### Author: ![MatthijsCox](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/matthijscox/32/42831_2.png) [@MatthijsCox](https://discourse.julialang.org/u/MatthijsCox)
#### Post date: [September 29, 2022, 3:03pm UTC](https://discourse.julialang.org/t/generate-temporary-environment-like-pkg-test/87981/4 "2022-09-29T15:03:29Z")

</div>

Maybe I should simplify my question.

I want to have a function like:

```julia
sandboxed_doctest("MyPackage")

```

With behavior similar to `Pkg.test("MyPackage")`

Which we could probably generalize to something like:

```julia
run_in_sandbox_environment(project="MyPackage", subproject="/docs", file="doctest.jl")

```

Where doctest.jl calls [Documenter.doctest](https://juliadocs.github.io/Documenter.jl/stable/lib/public/#Documenter.doctest) or something.

I’m now down into [Pkg.Operations.Test](https://github.com/JuliaLang/Pkg.jl/blob/0fae7809dbaa400d99cbe3a5b39c82332a1381d1/src/Operations.jl#L1575) which sets up a `sandbox` which seems to use the JULIA\_LOAD\_PATH trick proposed by @ericphanson as you can see on [line 1506 in operations.jl](https://github.com/JuliaLang/Pkg.jl/blob/0fae7809dbaa400d99cbe3a5b39c82332a1381d1/src/Operations.jl#L1506).

P.S. I will also look into whether [Documenter.doctest](https://juliadocs.github.io/Documenter.jl/stable/lib/public/#Documenter.doctest) already sets up a sandboxed environment…
