# Problem with importing a module after include

**URL:** <https://discourse.julialang.org/t/problem-with-importing-a-module-after-include/44719>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [August 11, 2020, 12:39am UTC](https://discourse.julialang.org/t/problem-with-importing-a-module-after-include/44719 "2020-08-11T00:39:56Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Stranglet\_A](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stranglet_a/32/17057_2.png) [@Stranglet\_A](https://discourse.julialang.org/u/Stranglet_A)\
**Post date:** [August 11, 2020, 12:39am UTC](https://discourse.julialang.org/t/problem-with-importing-a-module-after-include/44719/1 "2020-08-11T00:39:56Z")

</div>

Hi,

I am getting this warning after I run include and then import my module  
So I created a folder with two julia scripts Mymod.jl and test.jl :

MyMod.jl contains:

```julia
module MyMod
x=1
end

```

And in test.jl :

```julia
include("MyMod.jl")
import .MyMod
MyMod.x

```

After the import I get the following:  
`WARNING: could not import Main.MyMod into Main`  
And I can’t use MyMod.x

If I try “using”:

```julia
ERROR: UndefVarError: MyMod not defined
Stacktrace:
 [1] include_string(::Function, ::Module, ::String, ::String) at ./loading.jl:1088

```

I started Julia from the same directory where I store the two scripts.

---

<div class="post-metadata">

**Author:** ![N5N3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/n5n3/32/17663_2.png) [@N5N3](https://discourse.julialang.org/u/N5N3)\
**Post date:** [August 11, 2020, 12:54am UTC](https://discourse.julialang.org/t/problem-with-importing-a-module-after-include/44719/2 "2020-08-11T00:54:46Z")

</div>

```julia
include("MyMod.jl")
MyMod.x

```

is enough.

For `using MyMod`, you must put the .jl in

```julia
ENV["JULIA_LOAD_PATH"]

```

---

<div class="post-metadata">

**Author:** ![Stranglet\_A](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stranglet_a/32/17057_2.png) [@Stranglet\_A](https://discourse.julialang.org/u/Stranglet_A)\
**Post date:** [August 11, 2020, 1:11am UTC](https://discourse.julialang.org/t/problem-with-importing-a-module-after-include/44719/3 "2020-08-11T01:11:34Z")

</div>

> [@N5N3](#):
>
> `ENV["JULIA_LOAD_PATH"]`

Thanks  
I tried only writing include('MyMod.jl")  
and then MyMod.x and it didn’t work:

```julia
ERROR: UndefVarError: MyMod not defined
Stacktrace:
 [1] top-level scope at REPL[2]:1

```

---

<div class="post-metadata">

**Author:** ![Stranglet\_A](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stranglet_a/32/17057_2.png) [@Stranglet\_A](https://discourse.julialang.org/u/Stranglet_A)\
**Post date:** [August 11, 2020, 1:17am UTC](https://discourse.julialang.org/t/problem-with-importing-a-module-after-include/44719/4 "2020-08-11T01:17:29Z")

</div>

I also tried with writing first:

```julia
push!(LOAD_PATH, pwd())

```

and then:

```julia
using MyMod

```

and it didn’t work ☹ :

```julia
using MyMod
[Info: Precompiling MyMod [top-level]
WARNING: --output requested, but no modules defined during run
┌ Warning: The call to compilecache failed to create a usable precompiled cache file for MyMod [top-level]
│ exception = ArgumentError: Invalid header in cache file /home/maah/.julia/compiled/v1.5/MyMod.ji.
└ @ Base loading.jl:1042
ERROR: KeyError: key MyMod [top-level] not found
Stacktrace:
 [1] getindex at ./dict.jl:467 [inlined]
 [2] root_module at ./loading.jl:968 [inlined]
 [3] require(::Base.PkgId) at ./loading.jl:934
 [4] require(::Module, ::Symbol) at ./loading.jl:923

```

---

<div class="post-metadata">

**Author:** ![N5N3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/n5n3/32/17663_2.png) [@N5N3](https://discourse.julialang.org/u/N5N3)\
**Post date:** [August 11, 2020, 1:27am UTC](https://discourse.julialang.org/t/problem-with-importing-a-module-after-include/44719/5 "2020-08-11T01:27:47Z")

</div>

```julia
julia> versioninfo()
Julia Version 1.5.0
Commit 96786e22cc (2020-08-01 23:44 UTC)
Platform Info:
  OS: Windows (x86_64-w64-mingw32)
  CPU: Intel(R) Core(TM) i5-8300H CPU @ 2.30GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-9.0.1 (ORCJIT, skylake)
Environment:
  JULIA_EDITOR = "C:\Users\GNSS-BSAR\AppData\Local\atom\app-1.49.0\atom.exe" -a
  JULIA_HOME = C:\Users\GNSS-BSAR\AppData\Local\Programs\Julia 1.5.0
  JULIA_LOAD_PATH = G:\Julia\ToolBox;
  JULIA_NUM_THREADS = 4
  JULIA_PKG_SERVER = https://cn-northeast.pkg.juliacn.com/

julia> pwd()
"G:\\Julia\\TestScript"

julia> pwd() |> readdir
1-element Array{String,1}:
 "MyMod.jl"

julia> MyMod
ERROR: UndefVarError: MyMod not defined

julia> include("MyMod.jl")
Main.MyMod

julia> MyMod
Main.MyMod

julia> MyMod.x
1

```

The code above works on my laptop.  
Strange bug, have you checked the `pwd()`'s result?

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [August 11, 2020, 1:39am UTC](https://discourse.julialang.org/t/problem-with-importing-a-module-after-include/44719/6 "2020-08-11T01:39:20Z")

</div>

You don’t need to edit any paths to get relative import using `.MyMod` works.

All error messages, especially the warning you got with `using MyMod` points to that you didn’t actually have the content you expect in your `MyMod.jl`. Did you save the file, and saved it to the right path?
