# \`using\` and \`import\` can load files from the CWD? Surprising because it's not documented?

**URL:** <https://discourse.julialang.org/t/using-and-import-can-load-files-from-the-cwd-surprising-because-its-not-documented/122851>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [November 20, 2024, 2:40pm UTC](https://discourse.julialang.org/t/using-and-import-can-load-files-from-the-cwd-surprising-because-its-not-documented/122851 "2024-11-20T14:40:53Z")\
**Posts on this page:** 11\
**Page:** 1

<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:** [November 20, 2024, 2:40pm UTC](https://discourse.julialang.org/t/using-and-import-can-load-files-from-the-cwd-surprising-because-its-not-documented/122851/1 "2024-11-20T14:40:53Z")

</div>

A colleague of mine has just noticed some behavior relating to `using` and `import` which we found surprising.

`include` will load a file from disk and (effectively) insert the code at the point where the `include` line ran.

We were not aware that `using` and `import` could load code files from disk.

It seems that they can. I don’t recall reading anything about this in the documentation and having a quick search through the section on modules I didn’t find anything about this either.

Here is an example which shows what we have discovered.

Directory structure

```julia
tmp/
  JuliaProject/
    main.jl
    MyModule.jl

```

Example

```julia
# MyModule.jl
println("my module")

```

```julia
julia> using MyModule
my module

```

Is anyone able to clarify? Should we have found this behavior surprising?

From my point of view, I thought that the way to combine multiple source files together was using an `include` statement. In other words, this statement allows splitting one source file into multiple files.

I also thought that `import` and `using` were statements which related to the importing of modules (namespaces) from one namespace path to another.

- `include` → file behaviour
- `import`, `using` → module behaviour

Please let me know! Thanks

---

<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:** [November 20, 2024, 2:50pm UTC](https://discourse.julialang.org/t/using-and-import-can-load-files-from-the-cwd-surprising-because-its-not-documented/122851/2 "2024-11-20T14:50:47Z")

</div>

> [@world-peace](#):
>
> I don’t recall reading anything about this in the documentation and having a quick search through the section on modules

It’s in a different spot talking about packages, which uses modules for the necessary encapsulation:  
[Code Loading · The Julia Language](https://docs.julialang.org/en/v1/manual/code-loading/)  
The import statements for packages do not use leading dots.

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [November 20, 2024, 3:02pm UTC](https://discourse.julialang.org/t/using-and-import-can-load-files-from-the-cwd-surprising-because-its-not-documented/122851/3 "2024-11-20T15:02:21Z")

</div>

To be frank, I’ve never used this form of defining a package, where the main “entry file” is just in some directory. It’s specifically the first point for the “Package Directories” list:

> **[Code Loading · The Julia Language](https://docs.julialang.org/en/v1/manual/code-loading/#Package-directories)**
>
> Documentation for The Julia Language.

I always use an explicit `src/X.jl` form, to keep things more consistent and easier to navigate.

---

<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:** [November 20, 2024, 3:09pm UTC](https://discourse.julialang.org/t/using-and-import-can-load-files-from-the-cwd-surprising-because-its-not-documented/122851/4 "2024-11-20T15:09:48Z")

</div>

Thanks for the replies, I think there’s two pieces of information which are useful to know. (The following is quoted from the docs.)

- [Code Loading · The Julia Language](https://docs.julialang.org/en/v1/manual/code-loading/)

1. **A package directory** is a directory containing the source trees of a set of packages as subdirectories, and forms an _implicit environment_. If `X` is a subdirectory of a package directory and `X/src/X.jl` exists, then the package `X` is available in the package directory environment and `X/src/X.jl` is the source file by which it is loaded.

2. **Package loading:** e.g. `import X` or `using X`. The import mechanism allows you to load a package—i.e. an independent, reusable collection of Julia code, wrapped in a module—and makes the resulting module available by the name `X` inside of the importing module. If the same `X` package is imported multiple times in the same Julia session, it is only loaded the first time—on subsequent imports, the importing module gets a reference to the same module. Note though, that `import X` can load different packages in different contexts: `X` can refer to one package named `X` in the main project but potentially to different packages also named `X` in each dependency. More on this below.

**However** this is not the complete story. Perhaps someone can help fill in the gaps?

If I run `using MyModule`, then Julia will search for several things:

- `MyModule.jl/src/MyModule.jl`
- `MyModule/src/MyModule.jl`
- `MyModule.jl`

I _ **think** _ all of the above are searched (from the current working directory).

However, I think there are also other alternative paths - but I don’t recall the details.

Can anyone help to fill in the gaps or direct me to a doc page about this?

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [November 20, 2024, 3:17pm UTC](https://discourse.julialang.org/t/using-and-import-can-load-files-from-the-cwd-surprising-because-its-not-documented/122851/5 "2024-11-20T15:17:48Z")

</div>

This only works if you’ve done `push!(LOAD_PATH, ".")`, right?

---

<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:** [November 20, 2024, 4:08pm UTC](https://discourse.julialang.org/t/using-and-import-can-load-files-from-the-cwd-surprising-because-its-not-documented/122851/6 "2024-11-20T16:08:05Z")

</div>

Good questions - I am not sure actually. Let me check again…

---

<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:** [November 20, 2024, 4:12pm UTC](https://discourse.julialang.org/t/using-and-import-can-load-files-from-the-cwd-surprising-because-its-not-documented/122851/7 "2024-11-20T16:12:33Z")

</div>

I’ve just checked and found that I do not have any environment variable containing the word `LOAD`.

When in Julia `ENV` does not have a key `LOAD_PATH`.

I can still run `using MyModule` and get `MyModule.jl` to load.

However the following does not work:

- `using MyModule2` for `MyModule2.jl/src/MyModule2.jl`
- `using MyModule3` for `MyModule3/src/MyModule3.jl`

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [November 20, 2024, 4:16pm UTC](https://discourse.julialang.org/t/using-and-import-can-load-files-from-the-cwd-surprising-because-its-not-documented/122851/8 "2024-11-20T16:16:17Z")

</div>

`LOAD_PATH` is just a global variable, not an environmental variable.

I strongly suspect you have something akin to `push!(LOAD_PATH, ".")` in a `startup.jl` file or similar.

---

<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:** [November 20, 2024, 4:18pm UTC](https://discourse.julialang.org/t/using-and-import-can-load-files-from-the-cwd-surprising-because-its-not-documented/122851/9 "2024-11-20T16:18:02Z")

</div>

Thanks I’ll check.

Just found this too:

> Package directories  
> Package directories provide a simpler kind of environment without the ability to handle name collisions. In  
> a package directory, the set of top-level packages is the set of subdirectories that “look like” packages. A  
> package X exists in a package directory if the directory contains one of the following “entry point” files:  
> • X.jl  
> • X/src/X.jl  
> • X.jl/src/X.jl

So I wasn’t imagining this. Let me check my `LOAD_PATH`.

---

<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:** [November 20, 2024, 4:18pm UTC](https://discourse.julialang.org/t/using-and-import-can-load-files-from-the-cwd-surprising-because-its-not-documented/122851/10 "2024-11-20T16:18:54Z")

</div>

Yes! You’re correct. `.` is in my load path.

Is there a convenient way to print all the global variables? I’d like to check something else…

---

<div class="post-metadata">

**Author:** ![carstenbauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carstenbauer/32/4981_2.png) [@carstenbauer](https://discourse.julialang.org/u/carstenbauer)\
**Post date:** [November 24, 2024, 5:43pm UTC](https://discourse.julialang.org/t/using-and-import-can-load-files-from-the-cwd-surprising-because-its-not-documented/122851/11 "2024-11-24T17:43:46Z")

</div>

Julia related environment variables typically start with “JULIA\_”, e.g. “JULIA\_LOAD\_PATH” or “JULIA\_NUM\_THREADS”. And `versioninfo()` does display all environment variables with this prefix.
