# How to import a module and keep it's properties I saved in different file?

**URL:** <https://discourse.julialang.org/t/how-to-import-a-module-and-keep-its-properties-i-saved-in-different-file/15684>\
**Category:** General Usage\
**Tags:** question\
**Created:** [September 30, 2018, 6:16am UTC](https://discourse.julialang.org/t/how-to-import-a-module-and-keep-its-properties-i-saved-in-different-file/15684 "2018-09-30T06:16:11Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![TheOnlyArtz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/theonlyartz/32/12685_2.png) [@TheOnlyArtz](https://discourse.julialang.org/u/TheOnlyArtz)\
**Post date:** [September 30, 2018, 6:16am UTC](https://discourse.julialang.org/t/how-to-import-a-module-and-keep-its-properties-i-saved-in-different-file/15684/1 "2018-09-30T06:16:11Z")

</div>

I got 3 files:  
**Request.jl**

```julia
module Request

  setToken(t) = (global token = t)
end

```

**Client.jl**

```julia
module Client
  include("../API/Request.jl")
  import .Request

  function init(token::String) # This function is being called in other file.
    Request.setToken(token)
    # Now Client.Request.token will be it and it's set
end

```

**User.jl**

```julia
module User
  include("../API/Request.jl")
  println(Request.token) # How will I be able to get the token I set in Client module?
end

```

---

<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:** [September 30, 2018, 7:46pm UTC](https://discourse.julialang.org/t/how-to-import-a-module-and-keep-its-properties-i-saved-in-different-file/15684/2 "2018-09-30T19:46:11Z")

</div>

1. `setToken` isn’t a function, did you check that your code actually run?

2. I see what you mean but it’s unclear what you want to do. You’ve created 2 different `Request` modules so the change you did in one won’t be visible from the other. You need to create one instead and it is unclear what’s your design constraint. In general, don’t call include twice since each of them will give you a different module. There are different ways to call include only once and you didn’t give enough info to fix which one would work for you.

---

<div class="post-metadata">

**Author:** ![TheOnlyArtz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/theonlyartz/32/12685_2.png) [@TheOnlyArtz](https://discourse.julialang.org/u/TheOnlyArtz)\
**Post date:** [September 30, 2018, 7:51pm UTC](https://discourse.julialang.org/t/how-to-import-a-module-and-keep-its-properties-i-saved-in-different-file/15684/3 "2018-09-30T19:51:43Z")

</div>

Thank you very much for that detailed answer, taking a look at the source code will help you get the idea of what I’m trying to do?

EDIT: I’ve edited the code above so it is a valid function

---

<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:** [September 30, 2018, 8:13pm UTC](https://discourse.julialang.org/t/how-to-import-a-module-and-keep-its-properties-i-saved-in-different-file/15684/4 "2018-09-30T20:13:18Z")

</div>

> [@TheOnlyArtz](#):
>
> taking a look at the source code will help you get the idea of what I’m trying to do?

I mean, I understand your code, but I don’t understand what exactly is your design. As I mentioned, you need to make sure you only `include` and therefore create the module once and let everyone access that single module. What I don’t know is **how** you want that single instance of the module to be created. I’ve given you the options and you’ll have to decide on your own which one to take. Do you want to make `Request` a package? Part of a package imported by both `Client.jl` and `User.jl`? Or part of a package that is either `Client.jl` or `User.jl`? Are everything in a single top level script rather than packages?

---

<div class="post-metadata">

**Author:** ![TheOnlyArtz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/theonlyartz/32/12685_2.png) [@TheOnlyArtz](https://discourse.julialang.org/u/TheOnlyArtz)\
**Post date:** [September 30, 2018, 8:15pm UTC](https://discourse.julialang.org/t/how-to-import-a-module-and-keep-its-properties-i-saved-in-different-file/15684/5 "2018-09-30T20:15:12Z")

</div>

> imported by both `Client.jl` and `User.jl`

Yep exactly it. and everything is in the same package, I got this file right [here](https://github.com/Julicord/Julicord/blob/master/src/Julicord.jl)  
And I’m trying to get all the modules in the package to use it but it just throws load errors so I can use the modules in one instance and not always creating more of them, what am I doing wrong?

---

<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:** [September 30, 2018, 8:22pm UTC](https://discourse.julialang.org/t/how-to-import-a-module-and-keep-its-properties-i-saved-in-different-file/15684/6 "2018-09-30T20:22:49Z")

</div>

OK. That’s better. `include` + `import` is not the same as `import` (it’s called `import` right?) in python. The file structure does not have anything to do with the module structure and `include`ing file multiple times is the same as executing the file multiple times.

What you need to do is to decide a module structure, include the file in the right place so that you create that desired module structure (i.e. `include` the script that create module `A`: `A.jl`, in module `B` if you want `A` to be created as and access by `B.A`) and then use relative import to access the other modules in the same package.

As an example involving only the three modules (apart from the top level package one) above, your toplevel script should look like,

```julia
include("API/Request.jl") # The only reason this comes first is because the other two script uses it.
include("Client.jl")
include("User.jl")

```

Then in the `Client` and `User` module defined in `Client.jl` and `User.jl` respectively, instead of `include("API/Request.jl")`, import the one you’ve created with `import ..Request`. You might need to change the path (e.g. more `.`) if the relative path between the modules are more complicated…

---

<div class="post-metadata">

**Author:** ![TheOnlyArtz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/theonlyartz/32/12685_2.png) [@TheOnlyArtz](https://discourse.julialang.org/u/TheOnlyArtz)\
**Post date:** [September 30, 2018, 8:25pm UTC](https://discourse.julialang.org/t/how-to-import-a-module-and-keep-its-properties-i-saved-in-different-file/15684/7 "2018-09-30T20:25:43Z")

</div>

Oh I think I get you now so the `Julicord.jl` file/Module `includes` all of the modules in the package, right?  
So that means i can do `import ..moduleName` in the desired module I want to import the other module to (dots depends on position) and it will work just fine?

---

<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:** [September 30, 2018, 8:27pm UTC](https://discourse.julialang.org/t/how-to-import-a-module-and-keep-its-properties-i-saved-in-different-file/15684/8 "2018-09-30T20:27:09Z")

</div>

Correct. You just need to make sure the files (and therefore the modules) are included in the right order so that the dependencies are created before the users. And also adjust the relative `import` paths accordingly if/when you have more nested module structures.

---

<div class="post-metadata">

**Author:** ![TheOnlyArtz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/theonlyartz/32/12685_2.png) [@TheOnlyArtz](https://discourse.julialang.org/u/TheOnlyArtz)\
**Post date:** [September 30, 2018, 8:28pm UTC](https://discourse.julialang.org/t/how-to-import-a-module-and-keep-its-properties-i-saved-in-different-file/15684/9 "2018-09-30T20:28:04Z")

</div>

Okay I got you I will test it right now and reply to you with an update, you don’t even understand how much I appreciate this! ❤

---

<div class="post-metadata">

**Author:** ![TheOnlyArtz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/theonlyartz/32/12685_2.png) [@TheOnlyArtz](https://discourse.julialang.org/u/TheOnlyArtz)\
**Post date:** [September 30, 2018, 8:34pm UTC](https://discourse.julialang.org/t/how-to-import-a-module-and-keep-its-properties-i-saved-in-different-file/15684/10 "2018-09-30T20:34:42Z")

</div>

I’m getting errors such as  
`WARNING: could not import Main.Client into Main`  
And: `ERROR: LoadError: UndefVarError: Client not defined`  
When trying to do `import .Client` In a file that is in the same directory as [Julicord.jl](https://github.com/Julicord/Julicord/blob/master/src/Julicord.jl)

This is the file’s code:

```julia
import .Client

function ready()
    println("good morning")
end

Client.init("<TOKEN>")

```

I’m pretty sure I did everything you said  
And for example trying with Client.jl  
When I’m doing `import .Request` it just says it =\> `can't import Client.Request into Client`

---

<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:** [September 30, 2018, 8:59pm UTC](https://discourse.julialang.org/t/how-to-import-a-module-and-keep-its-properties-i-saved-in-different-file/15684/11 "2018-09-30T20:59:02Z")

</div>

> [@TheOnlyArtz](#):
>
> When I’m doing `import .Request` it just says it =\> `can't import Client.Request into Client`

`import ..Request`. The error with `Main` can only happen if you did it in the `Main` module, either a script or the REPL so I’m not exactly sure what you are trying to do there…

---

<div class="post-metadata">

**Author:** ![TheOnlyArtz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/theonlyartz/32/12685_2.png) [@TheOnlyArtz](https://discourse.julialang.org/u/TheOnlyArtz)\
**Post date:** [September 30, 2018, 8:59pm UTC](https://discourse.julialang.org/t/how-to-import-a-module-and-keep-its-properties-i-saved-in-different-file/15684/12 "2018-09-30T20:59:42Z")

</div>

Okay yea I see I’ve done `import ..Request` in the client and it says `couldn't import Main.Request into Client`  
Is there a possibility it treats the test file I’m using as Main instead of the Julicord file? if so I think I understand the issue

 ![image](https://global.discourse-cdn.com/julialang/original/3X/a/c/acb3500b98a79dfe5fe34ca3f26831b29ed6ed9f.png)  
I’m running this file ^

---

<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:** [September 30, 2018, 9:06pm UTC](https://discourse.julialang.org/t/how-to-import-a-module-and-keep-its-properties-i-saved-in-different-file/15684/13 "2018-09-30T21:06:49Z")

</div>

If you run `Test.jl`, julia won’t know anything about `Julicord.jl`. If `Test.jl` looks like `include("API/Request.jl"); include("Client.jl")` it should work though.

---

<div class="post-metadata">

**Author:** ![TheOnlyArtz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/theonlyartz/32/12685_2.png) [@TheOnlyArtz](https://discourse.julialang.org/u/TheOnlyArtz)\
**Post date:** [September 30, 2018, 9:08pm UTC](https://discourse.julialang.org/t/how-to-import-a-module-and-keep-its-properties-i-saved-in-different-file/15684/14 "2018-09-30T21:08:06Z")

</div>

So should I run my test client through Julicord.jl? just for testing stuff? when I will publish the library I will delete the testing code

---

<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:** [September 30, 2018, 9:15pm UTC](https://discourse.julialang.org/t/how-to-import-a-module-and-keep-its-properties-i-saved-in-different-file/15684/15 "2018-09-30T21:15:55Z")

</div>

Not sure what you mean by “run my test client”.

Are you writing a package or a collection of script? Or in another word, are you expecting your user to use the package or individual script? Are these files meant to be used without the other ones? What do you want to test for (i.e. as a package or as a script collection)?

If the files here are never meant to be used separately and only as part of the module then you should of course always use the module, including in the test. If it is allowed to be included in a different way other than a package then you can test with that setup instead. (The latter is a fairly strange design though.)

---

<div class="post-metadata">

**Author:** ![TheOnlyArtz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/theonlyartz/32/12685_2.png) [@TheOnlyArtz](https://discourse.julialang.org/u/TheOnlyArtz)\
**Post date:** [September 30, 2018, 9:17pm UTC](https://discourse.julialang.org/t/how-to-import-a-module-and-keep-its-properties-i-saved-in-different-file/15684/16 "2018-09-30T21:17:06Z")

</div>

Sorry for the confusing latter. I’m expecting the user to use it as a **package**  
Anyways I’m pretty sure I know what to do now I really really appreciate your help!
