# Is there anything like Python's import or Rust's mod/use in Julia?

**URL:** <https://discourse.julialang.org/t/is-there-anything-like-pythons-import-or-rusts-mod-use-in-julia/124960>\
**Category:** General Usage\
**Tags:** question, package\
**Created:** [January 20, 2025, 12:14pm UTC](https://discourse.julialang.org/t/is-there-anything-like-pythons-import-or-rusts-mod-use-in-julia/124960 "2025-01-20T12:14:28Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tehtiam](https://avatars.discourse-cdn.com/v4/letter/t/8dc957/32.png) [@Tehtiam](https://discourse.julialang.org/u/Tehtiam)\
**Post date:** [January 20, 2025, 12:14pm UTC](https://discourse.julialang.org/t/is-there-anything-like-pythons-import-or-rusts-mod-use-in-julia/124960/1 "2025-01-20T12:14:28Z")

</div>

Hello, everyone. I am asking a question on Julia’s code loading mechanism.

In Python, `import some_module` can be used for both importing some package in site packages and loading a module file in the same directory. However, in Julia, `import` or `using` can only load packages in the registry, but not load source files in the same directory. Julia do provides `include` for code loading, but `include` has the following drawbacks:

- Unlike `import`, `include` brings everything in the same namespace
- Unlike `import`, `include` won’t check whether the code has been loaded

This causes the “including multiple times” problems. For example, when I am writing code for the CAs of the pattern recognition course, I implemented a deep learning framework in Julia, and these are the source files in the project:

- `module.jl` contains the definition and some basic operations of the `Module` type, which is the building block of a deep learning model
- `model.jl` contains the definition of the `Model` type, which represents a deep learning model. It supports very limited autodiff based on Jacobian matrices
- `moduledef.jl` contains definitions of several `Module`s, like `Linear`, `ReLu`, `Sigmoid`
- `train.jl` contains the code to initialize and train a model

Both `model.jl` and `moduledef.jl` include `module.jl` for the definition of `Module`. And `train.jl` includes both `model.jl` and `moduledef.jl`. Since the `include` in Julia behaves just like `#include` in C, `module.jl` will be included twice. If the functions in those files are in the top level, they are just brought to the top level of `train.jl` and defined twice. If all the code are wrapped in a Julia module (say, with the same name of the source file), you will see `Main.model.module` and `Main.moduledef.module`. They are different Julia modules, but with exactly the same content!

`import` / `using` do provide mechanisms to resolve this. However, to use `import` or `using`, you have to:

1. Create a subdiretory with its own `Project.toml`
2. Place you source in the respective subdirectory
3. In you project root, run `] dev subdirectory` for each subdirectory

This is a lot of trouble, especially when the dependency between source files is complicated.

I have read Julia’s official documentation on code loading There’s no example of best practice of a project layout or how to split your code into multiple files.

This really imposes a lot of challenge when you are working with a large Julia project. If Julia supports C like `#ifdef` mechanism, or Python like import, or Rust like `mod` / `use`, this problem could have been elegantly solved. I have done some STFW, but I haven’t found any solutions.

---

<div class="post-metadata">

**Author:** ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)\
**Post date:** [January 20, 2025, 12:36pm UTC](https://discourse.julialang.org/t/is-there-anything-like-pythons-import-or-rusts-mod-use-in-julia/124960/2 "2025-01-20T12:36:59Z")

</div>

Many questions here, I won’t try to give a comprehensive answer (perhaps try with more focused questions in the future?).

Also, I’ll note there’s been many threads on these topics on these forums already.

The first thing I’ll say is: **just create a package** , it’s a great practice to make your deps explicit and will save you a lot of headache in the long run.

> [@Tehtiam](#):
>
> This is a lot of trouble

Use PkgTemplates.jl:

```julia
using PkgTemplates: generate
generate() # starts interactive interface

```

> [@Tehtiam](#):
>
> `import` or `using` can only load packages in the registry, but not load source files in the same directory.

There’s several packages providing the functionality you’re requesting. Personally I don’t find them useful, though. While `include` expressions (the equivalent of the `#include` preprocessor directive in C/C++) are awkward in theory, because of the need to order them correctly and keep them unique; in practice I don’t think that’s a problem at all:

- any failures tend to be loud, use `--warn-overwrite=yes --depwarn=yes`, turned on by default during testing
- the `include`s don’t need to be changed often while developing a package, at least in my experience
- TBH lately I’ve started to prefer to just keep the entire package in a single file

> [@Tehtiam](#):
>
> There’s no example of best practice of a project layout

Here’s my preferred style:

```julia
module MyPackage
    module SubModule1
        # ...
    end

    module SubModule2
        using ..SubModule1
        # ...
    end

    export public_name
    const public_name = ...
end

```

If you want to split the above into multiple files:

- put each top-level submodule into a separate file
- only use `include` at the top level, do _not_ use it from submodules

> [@Tehtiam](#):
>
> If Julia supports C like `#ifdef` mechanism

There’s `@static if` etc. Or just the regular `if`.

---

<div class="post-metadata">

**Author:** ![Raf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raf/32/3383_2.png) [@Raf](https://discourse.julialang.org/u/Raf)\
**Post date:** [January 20, 2025, 12:54pm UTC](https://discourse.julialang.org/t/is-there-anything-like-pythons-import-or-rusts-mod-use-in-julia/124960/3 "2025-01-20T12:54:45Z")

</div>

Just `include` at the top level only.

What is the reason to want includes anywhere else? This is a common discussion but I never understand it. You can import modules from anywhere in any other module.

Making packages is also super easy as @nsajko says, at some point of complexity its usually better to split out functionality anyway.

---

<div class="post-metadata">

**Author:** ![jlapeyre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jlapeyre/32/4514_2.png) [@jlapeyre](https://discourse.julialang.org/u/jlapeyre)\
**Post date:** [January 20, 2025, 4:28pm UTC](https://discourse.julialang.org/t/is-there-anything-like-pythons-import-or-rusts-mod-use-in-julia/124960/4 "2025-01-20T16:28:36Z")

</div>

I’m not sure this solves exactly your problem, but: [FromFile.jl](https://github.com/Roger-luo/FromFile.jl) is a package that implements somewhat python-like per-file module semantics.

---

<div class="post-metadata">

**Author:** ![adienes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adienes/32/37459_2.png) [@adienes](https://discourse.julialang.org/u/adienes)\
**Post date:** [January 20, 2025, 4:46pm UTC](https://discourse.julialang.org/t/is-there-anything-like-pythons-import-or-rusts-mod-use-in-julia/124960/5 "2025-01-20T16:46:23Z")

</div>

> [@Tehtiam](#):
>
> There’s no example of best practice of a project layout or how to split your code into multiple files.

I don’t think Julia provides very good tools for this. and thus I agree with your assessment that there are not many good examples to point at.

I know this is a bit of an unproductive statement, but I’m just saying it to empathize; I don’t think your confusion is your fault.

---

<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:** [January 20, 2025, 5:10pm UTC](https://discourse.julialang.org/t/is-there-anything-like-pythons-import-or-rusts-mod-use-in-julia/124960/6 "2025-01-20T17:10:29Z")

</div>

[modernjuliaworkflows.org](http://modernjuliaworkflows.org) has some best practices, though I don’t see anything specifically about how to split code into files within a package, or about submodules. They do discuss using local packages though: [Writing your code](https://modernjuliaworkflows.org/writing/#local_packages)
