-5

I found out a code from a script that prevented it to be opened with import

def begin(stdscr):
    stdscr.clear()
    curses.init_pair(1, curses.COLOR_GREEN, curses.COLOR_BLACK)
    curses.init_pair(2, curses.COLOR_RED, curses.COLOR_BLACK)

    init_maze()
    generate_maze()
    mainloop()

if __name__ == '__main__':
    curses.setupterm()
    stdscr = curses.initscr()
    curses.curs_set(False)
    stdscr.keypad(True)

    cols = curses.tigetnum('cols')
    lines = curses.tigetnum('lines')
    while width < cols and height + 5 < lines:
        curses.wrapper(begin)
        width += 10
        height += 6

The main one is if __name__ == '__main__':

Can anyone tell me how to make so that the game can works without if __name__ == '__main__': and works with import?

EDIT: If I delete if __name__ == '__main__':, the script doesn't work anymore

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • JACKDANIELS777 where??? – Smoggy Leaf Mar 06 '21 at 16:15
  • There is nothing in that script that prevents it from being ``import``'ed – provided it contains the code necessary to make it work as claimed. Please [edit] your question to clarify the problem. – MisterMiyagi Mar 06 '21 at 16:16
  • If you’re just trying to run the script, you could run `python myscript.py`. To use an import, you would have to organize the code in functions/clases. You could then call `script.function()` after importing. – Jacob Lee Mar 06 '21 at 16:17
  • What script doesn't work (this one, or the one you've imported into)? What errors are you getting? Do you understand what the if statement does? – OneCricketeer Mar 06 '21 at 16:17
  • @OneCricketeer It works perfectly, it's just that when I do "import file.py" it doesn't import it. Altho, it works when I open it manually – Smoggy Leaf Mar 06 '21 at 17:23
  • ``import`` works via *module* name, not *file* name. The correct way would be ``import file``, not ``import file.py``. – MisterMiyagi Mar 06 '21 at 18:25
  • @MisterMiyagi I always do it WITHOUT .py, I write import file. Not import file.py – Smoggy Leaf Mar 07 '21 at 07:46
  • @SmoggyLeaf And that is exactly why you are being asked to [edit] your question to reflect what *exactly* must be done to reproduce what *exact* problem. We do not know which parts are actually missing, omitted, or misrepresented. Please see the [mcve] page how to best help us help you. – MisterMiyagi Mar 07 '21 at 08:00

1 Answers1

2

As long as your module is in the same directory as the code which you are trying to import the module to, I believe this should work:

def begin(stdscr):
    stdscr.clear()
    curses.init_pair(1, curses.COLOR_GREEN, curses.COLOR_BLACK)
    curses.init_pair(2, curses.COLOR_RED, curses.COLOR_BLACK)

    init_maze()
    generate_maze()
    mainloop()

def main():
    curses.setupterm()
    stdscr = curses.initscr()
    curses.curs_set(False)
    stdscr.keypad(True)

    cols = curses.tigetnum('cols')
    lines = curses.tigetnum('lines')
    while width < cols and height + 5 < lines:
        curses.wrapper(begin)
        width += 10
        height += 6

if __name__ == "__main__":
    main()

From the script which you import this from, you could then do this:

import my_script.py

my_script.main()
Jacob Lee
  • 4,405
  • 2
  • 16
  • 37
  • Also it doesn't have to be in the same directory, but it has to be in a directory which is included in the paths variable that python is going to look through for libraries to import – user14518362 Mar 06 '21 at 16:25
  • @user14518362 Yes, that is correct. – Jacob Lee Mar 06 '21 at 16:26
  • An interesting thing to know is that you can add path variables either permanently for python or within your code temporarily – user14518362 Mar 06 '21 at 16:26
  • @user14518362 it is in the same directory – Smoggy Leaf Mar 06 '21 at 17:27
  • @SmoggyLeaf What he's trying to say is that you can import it, but it's not going to do anything if it's in the __name__ == '__main'__ if. you should look into what this if statement does, here's a related question that should explain it to you: https://stackoverflow.com/questions/419163/what-does-if-name-main-do – user14518362 Mar 06 '21 at 17:32