Adding another module to gamelib

so lets say I created another .py file in gamelib. this file is called test
now in that file I have defined a function called hello()
when I go back to algo_strategy and say groupings.hello()
NameError: name 'groupings' is not defined

You need to import the function. from .groupings import hello Or, from gamelib.groupings import hello, depending on your directory structure.

Edited to say from gamelib.groupings instead of from .gamelib.groupings.

using the standard starter-algo style we have

  • algo_strategy (says import gamelib here)
  • gamelib (folder)
    • groupings (has hello function)

Why does this not work?
And what if I want to import everything?
Why do all of the other modules work fine?

Is hello a .py file? Or is it a function contained in groupings.py?

If you have import gamelib in your top level then you should be able to do gamelib.groupings.hello() or from gamelib.groupings import hello.

If you want to import everything: from module import *.

The other modules work fine because the imports and directory structure are set up correctly.

it is a groupings.py file, What do you mean by their directory structure is set up correctly, how can I do that for my custom module? and if we do from module import * can’t we just do import module?
Thanks for the help so far!

You can do it correctly by following the prescriptions given above and in e.g here or here.

import module only imports module, it does not import anything from inside module. from module import * specifies to import everything from inside. In the former case, import module, you need to access the functions via module.func; whereas in the latter case, from module import *, you can just call func().

It is also recommended to avoid importing everything from within a module. If you have 3 functions, f1, f2, f3 that you want to import from the module mod you should call from mod import (f1, f2, f3), instead of from mod import *. Importing * clutters the namespace and inevitably leads to avoidable confusion.

1 Like

followed the links, still a bit confused on what to do in order to correctly set up the directory. Could you explain it a bit more?

There is nothing that complicated about the directory hierarchy, if you just put a .py file inside gamelib/ you will be fine.

I… did.
and it doesn’t work…
my groupings.py file is inside of gamelib

Provide more information, what isn’t working?

as I wrote at the top, it says groupings is not found

I slightly misread your original post. Is your file called test.py? groupings.py? Or neither?

hmm, let me try to redo the entire thing:
I have created a new module inside of the gamelib folder called groupings.py.
now when I go into algo_strategy (which already has import gamelib at the top)
I have a function that is groupings.hello() but I receive an error:

NameError: name 'groupings' is not defined

If I created that module inside of gamelib, I do not see what I am doing wrong

there is a line inside of groupings
def hello():
the only thing different I can think of is it is not a class but instead a bunch of functions, I do not see how that makes a difference as game_state has a function called is_stationary() which is outside of the GameState class

Have you tried from gamelib import groupings?

well how come all of the other modules do not need to do that?

open the __init__.py file in gamelib/, it contains a bunch of import statements. This is probably the source of your confusion.

so do I just add
from .groupings import *
or is there a different way to do that?

I would use from gamelib.groupings import hello.

how come the other modules just have a .module?

You need to just read the docs and experiment. import in Python is confusing.