# 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:** 20\
**Page:** 2

<div class="post-metadata">

**Author:** ![mhinsch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mhinsch/32/26676_2.png) [@mhinsch](https://discourse.julialang.org/u/mhinsch)\
**Post date:** [December 5, 2024, 6:01pm UTC](https://discourse.julialang.org/t/using-or-import-search-current-directory/123463/21 "2024-12-05T18:01:15Z")

</div>

Ok, let’s make this more concrete, hopefully it will become clearer this way.

I have three modules in three separate files:

`Base.jl`:

```Julia
module Base

export do_stuff

function do_stuff end

end

```

`Support.jl`:

```Julia
module Support
# I would like to do this, but I can't:
# import Base
# so this is what I do instead:
import ..Base

Base.do_stuff(x::Float64) = ...
Base.do_stuff(x::Int)=...
end

```

`Macro.jl` (can’t remember why I called it `D` before):

```Julia
module Macro
export @mymacro
# I would like to do this, but I can't:
# using Base
# so this is what I do instead:
using ..Base

function helper(x)
   # this needs to call a method of Base.do_stuff
    do_stuff(x)
end

macro mymacro(...)
...
# does complicated stuff, generated code calls helper on user-provided type
end
end

```

In order to make this work I need a fourth file:

`Global.jl`:

```Julia
module Global

include("Base.jl")
include("Support.jl")
include("Macro.jl")

end

```

Edit: I forgot to add, that all of these are part of a package.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [December 5, 2024, 7:05pm UTC](https://discourse.julialang.org/t/using-or-import-search-current-directory/123463/22 "2024-12-05T19:05:12Z")

</div>

> [@world-peace](#):
>
> It’s so we can dynamically replace modules, at runtime.

Yes, but why? In what context is this (replacing at runtime an internal module used by another module) a useful thing you want to do?

The only thing that comes to mind is during code development / live debugging, but in that case we have Revise.jl.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [December 5, 2024, 7:07pm UTC](https://discourse.julialang.org/t/using-or-import-search-current-directory/123463/23 "2024-12-05T19:07:55Z")

</div>

> [@mhinsch](#):
>
> In order to make this work I need a fourth file: `Global.jl`:

Yes, as I said, the top-level file in a Julia package is analogous to a “makefile” in another language. It’s where you list all of the files that make up your package. I guess I’m used to makefiles from other languages so this doesn’t seem so bad?

---

<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:** [December 5, 2024, 9:19pm UTC](https://discourse.julialang.org/t/using-or-import-search-current-directory/123463/24 "2024-12-05T21:19:36Z")

</div>

> [@mhinsch](#):
>
> ```julia
> module Support
> # I would like to do this, but I can't:
> # import Base
> # so this is what I do instead:
> import ..Base
> 
> Base.do_stuff(x::Float64) = ...
> Base.do_stuff(x::Int)=...
> end
> 
> ```

If you dislike the `..` notation, note that, assuming this to be part of the `Global` package like in your post, you can also replace

```julia
import ..Base

```

with

```julia
import Global.Base

```

It may be a matter of taste but I tend to find the latter easier to read.

---

<div class="post-metadata">

**Author:** ![mhinsch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mhinsch/32/26676_2.png) [@mhinsch](https://discourse.julialang.org/u/mhinsch)\
**Post date:** [December 5, 2024, 9:25pm UTC](https://discourse.julialang.org/t/using-or-import-search-current-directory/123463/25 "2024-12-05T21:25:51Z")

</div>

> [@stevengj](#):
>
> > [@mhinsch](#):
> >
> > In order to make this work I need a fourth file: `Global.jl`:
> 
> Yes, as I said, the top-level file in a Julia package is analogous to a “makefile” in another language. It’s where you list all of the files that make up your package. I guess I’m used to makefiles from other languages so this doesn’t seem so bad?

If it actually was some kind of makefile equivalent I wouldn’t even mind so much, as a makefile represents the dependencies within a project. However, this is just a flat list of files with essentially no information content and that could be trivially generated.

I think the problem is that Julia’s module system tries to be completely orthogonal to the file system but because that would be too inconvenient ends up in a weird place that’s neither nor. If using/import did really only handle administration of available namespaces and their contents then a separate (potentially flexible and powerful) system would have to exist that tells the compiler which files to load the module from. As it is we have a hybrid, though, where using/import a) do stuff with modules that the compiler already knows about, b) search through the file system for files that might contain an unknown but requested module and c) negotiate with the package system to find an entire directory structure that represents the requested module. Unfortunately all of that is context dependent as well, so stuff that works in one context (e.g. plain directory) does not work in another (package directory).

Anyway, none of that is really terrible and workarounds exist. I guess even a really well-designed language such as Julia will end up with a wart or two (see also e.g. issues [#40717](https://github.com/JuliaLang/julia/issues/40717) or [#269](https://github.com/JuliaLang/julia/issues/269)). However, I think a simple local import would already solve 90% of the issues people have.

---

<div class="post-metadata">

**Author:** ![mhinsch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mhinsch/32/26676_2.png) [@mhinsch](https://discourse.julialang.org/u/mhinsch)\
**Post date:** [December 5, 2024, 9:26pm UTC](https://discourse.julialang.org/t/using-or-import-search-current-directory/123463/26 "2024-12-05T21:26:30Z")

</div>

> [@ffevotte](#):
>
> If you dislike the `..` notation, note that, assuming this to be part of the `Global` package like in your post, you can also replace
> 
> ```julia
> import ..Base
> 
> ```
> 
> with
> 
> ```julia
> import Global.Base
> 
> ```
> 
> It may be a matter of taste but I tend to find the latter easier to read.

Good idea, that’s actually a bit nicer.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [December 5, 2024, 9:31pm UTC](https://discourse.julialang.org/t/using-or-import-search-current-directory/123463/27 "2024-12-05T21:31:01Z")

</div>

> [@mhinsch](#):
>
> However, this is just a flat list of files with essentially no information content and that could be trivially generated.

It could be generated from what? By assuming there is a one-to-one correspondence between files and submodules? That isn’t typical for Julia packages. Or just assuming every `.jl` file in the package src tree should be included? That seems a bit dicey.

Or are you just referring to the old suggestion, mentioned above, that `using .Foo` should search the current directory for `Foo.jl`? That’s possible, but it wouldn’t eliminate most usage of `include`, because most packages seem to use multiple files per module.

---

<div class="post-metadata">

**Author:** ![mhinsch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mhinsch/32/26676_2.png) [@mhinsch](https://discourse.julialang.org/u/mhinsch)\
**Post date:** [December 5, 2024, 9:52pm UTC](https://discourse.julialang.org/t/using-or-import-search-current-directory/123463/28 "2024-12-05T21:52:30Z")

</div>

In first approximation a list of all .jl files whose names begin with capital letters would probably come quite close, but that’s besides the point. You are right, of course, that there are many different scenarios where this list could not actually be generated. That doesn’t change my main point, however, that (as opposed to a makefile) this file contains very little useful information and its only purpose really is to compensate for a weakness of the module system.

---

<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:** [December 5, 2024, 10:04pm UTC](https://discourse.julialang.org/t/using-or-import-search-current-directory/123463/29 "2024-12-05T22:04:26Z")

</div>

> [@mhinsch](#):
>
> I think the problem is that Julia’s module system tries to be completely orthogonal to the file system but because that would be too inconvenient ends up in a weird place that’s neither nor.

I agree that the module system tries to be orthogonal to the file system, but I’m not sure I completely understand why you say Julia does not achieve this goal.

> [@mhinsch](#):
>
> That doesn’t change my main point, however, that (as opposed to a makefile) this file contains very little useful information and its only purpose really is to compensate for a weakness of the module system.

So if the module system is meant to be completely orthogonal to the filesystem, then there needs to exist both a list of modules (defining where names are to be looked for) and a list of source files (defining where the source code lives in the filesystem).

Currently, this list of source files is built from two sources of information:

1. the `Pkg` system, and
2. the set of `include` calls.

In that regard, a list of `include`d files can surely be considered to contain useful information. Or did I misunderstand your point?

---

<div class="post-metadata">

**Author:** ![mhinsch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mhinsch/32/26676_2.png) [@mhinsch](https://discourse.julialang.org/u/mhinsch)\
**Post date:** [December 5, 2024, 10:08pm UTC](https://discourse.julialang.org/t/using-or-import-search-current-directory/123463/30 "2024-12-05T22:08:28Z")

</div>

> [@ffevotte](#):
>
> So if the module system is meant to be completely orthogonal to the filesystem, then there needs to exist both a list of modules (defining where names are to be looked for) and a list of source files (defining where the source code lives in the filesystem).
> 
> Currently, this list of source files is built from two sources of information:
> 
> 1. the `Pkg` system, and
> 2. the set of `include` calls.
> 
> In that regard, a list of `include`d files can surely be considered to contain useful information. Or did I misunderstand your point?

Include has a very different function though, that only partially (and I would argue, accidentally) overlaps with the module system. Julia’s include works more or less like C’s, so it has the same strengths and weaknesses. It’s super flexible because it allows you to chop up your code however you like. At the same time it’s very stupid, however, and simply copies and pastes the code without much in terms of sanity or dependency checking.

I think the module system should be completely independent of include.

---

<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:** [December 5, 2024, 10:15pm UTC](https://discourse.julialang.org/t/using-or-import-search-current-directory/123463/31 "2024-12-05T22:15:18Z")

</div>

> [@mhinsch](#):
>
> I think the module system should be completely independent of include.

And it already is.

You can define a complex hierarchy of dozens of modules in a single package without having to call `include` once. Or you can have only one module in your package and split it in dozens of files with `include` (like the majority of Julia packages). If that isn’t independence, what is?

---

<div class="post-metadata">

**Author:** ![mhinsch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mhinsch/32/26676_2.png) [@mhinsch](https://discourse.julialang.org/u/mhinsch)\
**Post date:** [December 5, 2024, 10:38pm UTC](https://discourse.julialang.org/t/using-or-import-search-current-directory/123463/32 "2024-12-05T22:38:39Z")

</div>

> [@ffevotte](#):
>
> You can define a complex hierarchy of dozens of modules in a single package without having to call `include` once. Or you can have only one module in your package and split it in dozens of files with `include` (like the majority of Julia packages). If that isn’t independence, what is?

Maybe I’m being dense, but I don’t see how you would do that unless either all of these modules exist in a single file or none of them refer to any of the others.

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [December 5, 2024, 11:07pm UTC](https://discourse.julialang.org/t/using-or-import-search-current-directory/123463/33 "2024-12-05T23:07:32Z")

</div>

> [@mhinsch](#):
>
> In order to make this work I need a fourth file:
> 
> `Global.jl`:

This isn’t unusual among the wider languages, and Julia didn’t come up with it.

The only ways your example’s `import ..Base` (putting aside the dangerous name conflict with `Base` for now) would work without `Global.jl` specifying the modules structure is if files encapsulated modules, making the surrounding `module ... end` expressions entirely redundant in your example. It works for Python because files-as-modules are designed to heavily import from each other. Heavy use of `from OtherMod import *` can mimic `include`-ing a dozen files into 1 Julia `module`, but the imported names still come from a dozen other modules and that difference can matter.

To make multiple files evaluate into 1 module from the start, we must be able to evaluate files into a global scope (`include`) separate from importing modules. At that point, we might still treat files as modules, we just `include` some into others that we import. But how obvious will that be? Say `Macro.jl` gets long enough we split the helper functions into `Helpers.jl`. We can’t tell just from the file names how they’re related, we have to open both of them to find out `Macro.jl` runs `include("Helpers.jl")`. If we had \>10 files, where do we even start? Note that Python has ways to evaluate files (and this is crucial for IDEs running scripts because `import` does not reload code on its own), and it’s almost never used alongside `import` this way because of how much of a nightmare it is to handle.

The `module` expression becomes useful because files can no longer tell us what should or shouldn’t be imported. 1 conventionally named file per directory gives us a starting point for the code’s module structure (Python packages’ directories also have this). That pretty much gets us to the status quo.

---

<div class="post-metadata">

**Author:** ![mhinsch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mhinsch/32/26676_2.png) [@mhinsch](https://discourse.julialang.org/u/mhinsch)\
**Post date:** [December 6, 2024, 9:37am UTC](https://discourse.julialang.org/t/using-or-import-search-current-directory/123463/34 "2024-12-06T09:37:38Z")

</div>

I’m not entirely sure which point you are trying to make (maybe because my experience with Python is quite limited). In any case, I think it might be useful to be a bit more precise WRT terminology (I certainly wasn’t).

You are right, of course, that there is no file ↔ module correspondence in Julia and that, therefore, strictly speaking there can be no straightforward way to import local modules. There is, however, under certain conditions a correspondence file ↔ package. By adding a directory containing single-file packages to the load path you can _effectively_ import modules corresponding to files. Unfortunately that doesn’t work as soon as you are in a project environment, since then only explicit dependencies can be loaded.

So, again trying to keep the terminology straight, I think what is missing in Julia is the ability to add the current (or any other “local”) directory as a package directory within a project environment. If that was possible, then - going back to my example - `Base.jl` could be loaded by a simple `using` or `import`.

---

<div class="post-metadata">

**Author:** ![world-peace](https://avatars.discourse-cdn.com/v4/letter/w/9f8e36/32.png) [@world-peace](https://discourse.julialang.org/u/world-peace)\
**Post date:** [December 6, 2024, 10:23am UTC](https://discourse.julialang.org/t/using-or-import-search-current-directory/123463/35 "2024-12-06T10:23:20Z")

</div>

> [@Benny](#):
>
> And it won’t happen in Julia because files do not encapsulate modules, `module` expressions do. Note that Python doesn’t even have a `module` expression, it automatically considers each file its own module.

Yes I know

> [@Benny](#):
>
> That means if you change the file’s contents

Who said anything about changing the file contents? This isn’t what I am doing

> [@Benny](#):
>
> You need `importlib.reload` to actually reload the module

This is the exact behaviour I am trying to avoid. Re-loading from disk

> [@Benny](#):
>
> and that is awful to work with

We have reasons for wanting to do this

---

<div class="post-metadata">

**Author:** ![world-peace](https://avatars.discourse-cdn.com/v4/letter/w/9f8e36/32.png) [@world-peace](https://discourse.julialang.org/u/world-peace)\
**Post date:** [December 6, 2024, 10:27am UTC](https://discourse.julialang.org/t/using-or-import-search-current-directory/123463/36 "2024-12-06T10:27:17Z")

</div>

> [@stevengj](#):
>
> Yes, but why? In what context is this (replacing at runtime an internal module used by another module) a useful thing you want to do?

You don’t need to know why - that’s not important. What matters is that someone wants to do it. If there is no way to do it, then that’s fine. I will go tell them it cannot be done.

---

<div class="post-metadata">

**Author:** ![world-peace](https://avatars.discourse-cdn.com/v4/letter/w/9f8e36/32.png) [@world-peace](https://discourse.julialang.org/u/world-peace)\
**Post date:** [December 6, 2024, 10:34am UTC](https://discourse.julialang.org/t/using-or-import-search-current-directory/123463/37 "2024-12-06T10:34:43Z")

</div>

I don’t know why they didn’t just do the Rust/Python thing which works really well.

I really like your response, it helped to clarify a few things in my own mind. It’s really bizarre that `using`/`include` can both load from file and perform operations on the current (in memory) module system.

If Julia wanted to go the C++ route and have `include`s everywhere to explicitly load code from disk (something that Python specifically tried to avoid) then `using` and `include` should have only been for module manipulation and `include` should have worked when targeted at a package directory as well as just a single file.

The other big problem with it is you can `include` more than once. Which is a disaster, and experience has demonstrated to me that devs will do this without realizing the consequences of what they are doing.

---

<div class="post-metadata">

**Author:** ![world-peace](https://avatars.discourse-cdn.com/v4/letter/w/9f8e36/32.png) [@world-peace](https://discourse.julialang.org/u/world-peace)\
**Post date:** [December 6, 2024, 10:36am UTC](https://discourse.julialang.org/t/using-or-import-search-current-directory/123463/38 "2024-12-06T10:36:13Z")

</div>

> [@stevengj](#):
>
> By assuming there is a one-to-one correspondence between files and submodules? That isn’t typical for Julia packages

Arguably this was a bad design decision. It’s pretty rare that you want to nest modules within the same file.

I appreciate it as a feature, but the reality is it’s rarely - if ever(?) used.

---

<div class="post-metadata">

**Author:** ![world-peace](https://avatars.discourse-cdn.com/v4/letter/w/9f8e36/32.png) [@world-peace](https://discourse.julialang.org/u/world-peace)\
**Post date:** [December 6, 2024, 10:41am UTC](https://discourse.julialang.org/t/using-or-import-search-current-directory/123463/39 "2024-12-06T10:41:58Z")

</div>

> [@ffevotte](#):
>
> You can define a complex hierarchy of dozens of modules in a single package without having to call `include` once.

Err, really? How? You must be talking about something different to what I’m envisioning by saying this. Can you provide a demo of this?

---

<div class="post-metadata">

**Author:** ![mhinsch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mhinsch/32/26676_2.png) [@mhinsch](https://discourse.julialang.org/u/mhinsch)\
**Post date:** [December 6, 2024, 10:42am UTC](https://discourse.julialang.org/t/using-or-import-search-current-directory/123463/40 "2024-12-06T10:42:43Z")

</div>

I think in parts it’s a UI issue. If there was a clear separation between commands that operate on modules, files and packages respectively the distinction would be immediately obvious and it would be clear to everyone that Julia modules do _not_ behave like the Python (or whatever) equivalent. By using `using` (heh) for everything the distinction becomes muddled and people get confused.

[Previous page](https://discourse.julialang.org/t/using-or-import-search-current-directory/123463.md?page=1)

[Next page](https://discourse.julialang.org/t/using-or-import-search-current-directory/123463.md?page=3)
