# Using or import search current directory?

**URL:** <https://discourse.julialang.org/t/using-or-import-search-current-directory/123463>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [December 4, 2024, 4:06pm UTC](https://discourse.julialang.org/t/using-or-import-search-current-directory/123463 "2024-12-04T16:06:17Z")\
**Posts on this page:** 1\
**Showing post:** 66

<div class="post-metadata">

**Author:** ![danielwe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/danielwe/32/35657_2.png) [@danielwe](https://discourse.julialang.org/u/danielwe)\
**Post date:** [December 9, 2024, 8:20pm UTC](https://discourse.julialang.org/t/using-or-import-search-current-directory/123463/66 "2024-12-09T20:20:10Z")

</div>

> [@mhinsch](#):
>
> There is no obvious, lightweight solution for the (in my mind very common) use case “encapsulate a local chunk of code and load it like a package”

The lightest-weight solution is probably to modify `LOAD_PATH` manually. Although this has been discouraged previously in the thread, there’s currently no alternative if you want to load modules like packages without dealing with the overhead of creating a new package.

Although it’s not framed this way, the manual section on [package directories](https://docs.julialang.org/en/v1/manual/code-loading/#Package-directories) is really an explanation of how Julia deals with entries in `LOAD_PATH` that don’t have the full structure of a package. It’s not written in a user-friendly way, so here’s the CliffsNotes version:

- Put your code in a file
- Wrap it in a top-level module such that the module name exactly matches the file name
- Put the file in the package directory\[1\]

Now the module can be loaded like a package. Example:

- My package directory is `./code`
- It contains a single file `code/Foo.jl` with the following contents:

```julia
module Foo

export foo

foo() = println("Hello from Foo.jl")

end

```

- Now let’s load `Foo` like a package:

```julia
julia> push!(LOAD_PATH, "code"); # add ./code to LOAD_PATH

julia> using Foo
[Info: Precompiling Foo [top-level]

julia> foo()
Hello from Foo.jl

```

I think this is the closest you can currently get to a python-like `import .file` experience. The only extra overhead is making sure the module and file names match, and a single push to `LOAD_PATH`.

* * *

1. Alternatively, put the file in a package-like folder structure inside the package directory, that is, instead of adding `Foo.jl` you can add `Foo/src/Foo.jl`

---

_[View the full topic](https://discourse.julialang.org/t/using-or-import-search-current-directory/123463)._
