# How to load a file-as-a-module when an Environment is present

**URL:** <https://discourse.julialang.org/t/how-to-load-a-file-as-a-module-when-an-environment-is-present/122680>\
**Category:** General Usage\
**Created:** [November 15, 2024, 2:03pm UTC](https://discourse.julialang.org/t/how-to-load-a-file-as-a-module-when-an-environment-is-present/122680 "2024-11-15T14:03:04Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)\
**Post date:** [November 15, 2024, 2:03pm UTC](https://discourse.julialang.org/t/how-to-load-a-file-as-a-module-when-an-environment-is-present/122680/1 "2024-11-15T14:03:04Z")

</div>

I need to load a module defined in a local file without using `includet()` (because of [an issue with nested includet](https://discourse.julialang.org/t/revise-with-nested-include/97916)).

I tried then LOAD\_PATH and at the beginning this works:

**a.jl** :

```julia
using Pkg
Pkg.activate(@ __DIR__ )
push!(LOAD_PATH,@ __DIR__ )
using Foo
foo(1)

```

**Foo.jl** :

```julia
module Foo
export foo
foo(x) = x + 1
end

```

However, as soon as the environment where the file `a.jl` resides is defined (i.e. has a Project/Manifest.toml) the `using Foo` line returns the error that `Foo` is not found. For example, adding the `CSV` package to force the creation of an environment:

**a.jl** :

```julia
using Pkg
Pkg.activate(@ __DIR__ )
Pkg.add("CSV")
push!(LOAD_PATH,@ __DIR__ )
using Foo #`ERROR: ArgumentError: Package Foo not found in current path.`
foo(1)

```

How can I load Foo.jl ?

---

<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:** [November 15, 2024, 2:19pm UTC](https://discourse.julialang.org/t/how-to-load-a-file-as-a-module-when-an-environment-is-present/122680/2 "2024-11-15T14:19:37Z")

</div>

Do you need `a.jl` and `Foo.jl` to live in the same directory?

If not, you could create a specific directory for `Foo.jl`:

```julia
project/
├── Project.toml
├── a.jl
└── lib/
    └── Foo.jl

```

and then `a.jl` could `push!(LOAD_PATH, joinpath(@ __DIR__ , "lib"))` so that I think your trick would continue working.

  

But better yet, if you don’t have strong constraints on the file organization, you could give `Foo.jl` a real package structure:

```julia
project/
├── Project.toml
├── a.jl
└── Foo/
    ├── Project.toml
    └── src/
        └── Foo.jl

```

In this case, you’ll be able to `Pkg.develop(path=joinpath(@ __DIR__ , "Foo"))` in the top-level project, and everything would work without having to tamper with `LOAD_PATH`.

---

<div class="post-metadata">

**Author:** ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)\
**Post date:** [November 15, 2024, 2:45pm UTC](https://discourse.julialang.org/t/how-to-load-a-file-as-a-module-when-an-environment-is-present/122680/3 "2024-11-15T14:45:26Z")

</div>

Oh I see. The env is looked up in the folder of the “imported” file.  
Solution 1 seems to work for me, even if in my application Foo.jl shares the environment with a.jl, and so I had to trickle a bit:

**lib/Foo.jl** :

```julia
module Foo
using Pkg
Pkg.activate(joinpath(@ __DIR__ ,".."))
export foo
using CSV # or whatever pkg I need (defined in the a.jl folder env)
foo(x) = x + 1 
end

```

The solution of converting Foo.jl in a full package is a bit too complicate for the people I am working with (e.g. two separate environments)… I feel like Julia somehow push to use Packages when this would not be always needed…
