# Inferring correct environment based on file being run?

**URL:** <https://discourse.julialang.org/t/inferring-correct-environment-based-on-file-being-run/38358>\
**Category:** General Usage\
**Tags:** package, pkg\
**Created:** [April 28, 2020, 8:56am UTC](https://discourse.julialang.org/t/inferring-correct-environment-based-on-file-being-run/38358 "2020-04-28T08:56:47Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jackf](https://avatars.discourse-cdn.com/v4/letter/j/b4bc9f/32.png) [@jackf](https://discourse.julialang.org/u/jackf)\
**Post date:** [April 28, 2020, 8:56am UTC](https://discourse.julialang.org/t/inferring-correct-environment-based-on-file-being-run/38358/1 "2020-04-28T08:56:47Z")

</div>

I have a local package (call it Foo) that I have `dev`d. In the `src` folder I have a file called `main.jl` that I’d like to run by calling `julia src/main.jl`. Currently, I have to call `julia --project main.jl` to use the correct Foo environment (otherwise imports `add`ed to Foo won’t work in `main.jl`). Is there a way to avoid having to specify `--project`, and instead have julia correctly infer the environment given that the file in question is in a the `dev`d directory?

This becomes more annoying when I’m not currently in the Foo directory. If I’m in the parent directory, I’d have to call `julia --project=Foo Foo/src/main.jl`, which is quite verbose.

---

<div class="post-metadata">

**Author:** ![ffevotte](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ffevotte/32/6587_2.png) [@ffevotte](https://discourse.julialang.org/u/ffevotte)\
**Post date:** [April 28, 2020, 6:52pm UTC](https://discourse.julialang.org/t/inferring-correct-environment-based-on-file-being-run/38358/2 "2020-04-28T18:52:29Z")

</div>

You could put something like this at the beginning of `src/main.jl`:

```julia
using Pkg
Pkg.activate(joinpath(@ __DIR__ , ".."))

```

  

* * *
  

FWIW, I tend to put such “main” scripts directly at the project root (i.e. in the same directory as `Project.toml`), in order to make it clear which source files _define_ the package (under `src`), and which source files _use_ the package (for example to turn it into an application, by activate the relevant environment and calling the entry point).

I’m not sure if this is common practice, but this has served me well so far…

---

<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:** [April 28, 2020, 7:15pm UTC](https://discourse.julialang.org/t/inferring-correct-environment-based-on-file-being-run/38358/3 "2020-04-28T19:15:50Z")

</div>

You can `export JULIA_PROJECT=@.` in `.bashrc` (or equivalent for your system). This will effectively mean `--project` at every Julia start.

---

<div class="post-metadata">

**Author:** ![ffevotte](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ffevotte/32/6587_2.png) [@ffevotte](https://discourse.julialang.org/u/ffevotte)\
**Post date:** [April 28, 2020, 7:44pm UTC](https://discourse.julialang.org/t/inferring-correct-environment-based-on-file-being-run/38358/4 "2020-04-28T19:44:13Z")

</div>

IIUC, this activates the project in the current working directory; not the project where the julia source file resides.

I might have misunderstood, but I’m not sure this is the behavior the OP wanted to achieve…

---

<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:** [April 28, 2020, 7:46pm UTC](https://discourse.julialang.org/t/inferring-correct-environment-based-on-file-being-run/38358/5 "2020-04-28T19:46:36Z")

</div>

True, I only read the first part of the question.

---

<div class="post-metadata">

**Author:** ![jackf](https://avatars.discourse-cdn.com/v4/letter/j/b4bc9f/32.png) [@jackf](https://discourse.julialang.org/u/jackf)\
**Post date:** [April 30, 2020, 11:45pm UTC](https://discourse.julialang.org/t/inferring-correct-environment-based-on-file-being-run/38358/6 "2020-04-30T23:45:05Z")

</div>

Thanks both. Good to know about `JULIA_PROJECT`, though that isn’t what I was looking for since it sets a fixed path.

I’ll try the Pkg idea in main.jl.
