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

I got 3 files:

module Request

  setToken(t) = (global token = t)


module Client
  import .Request

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


module User
  println(Request.token) # How will I be able to get the token I set in Client module?
  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.

    1. Create Request as a package. using with absolute name will automatically make sure you only include once.
    2. Put Request as a sub module of a package, or if you are just running a script, just make sure you only include once and let everyone else import that. This is not much different from the previous one except that you will call the include yourself.

    In short, you need to decide where to put it.

1 Like

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

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?

imported by both Client.jl and User.jl

Yep exactly it. and everything is in the same package, I got this file right here
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?

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 includeing 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,

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

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…

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?

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.

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! :heart:

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

This is the file’s code:

import .Client

function ready()
    println("good morning")


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

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…

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

I’m running this file ^

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.

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

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.)

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!