-1

I have a main.py and a module named gui.py which im want to compile to cython and then exe which includes:

import gui

if __name__ == '__main__':
    gui()

then in the gui.py I have a code with using if __name__ == '__main__': as follows:

def find_regex_1(k, s):
    reg = f'{k}\s*=\s*(\S+)'
    return re.search(reg, s).group(1)

def read_configs(config_file):
    with open(config_file, "r") as f:
        content = f.read()
        license_code = find_regex_1('LICENSE CODE', content)
        activation_code = find_regex_1('ACTIVATION CODE ', content)
    return license_code, activation_code

if __name__ == '__main__':
    license_code, activation_code = read_configs('config.txt')

the second if __name__ == '__main__': in the gui.py interrupt the process what I should do to avoid that? I think I should use something else rather than if __name__ == '__main__': but i don't know what to replace with the same effect

error I'm getting:

C:\Users\Administrator\Desktop\cython>python main.py
Traceback (most recent call last):
  File "C:\Users\Administrator\Desktop\cython\main.py", line 24, in <module>
    import gui
  File "gui.py", line 530, in init gui
    status, msg = activate_license(sys.argv[0])
  File "gui.py", line 113, in gui.activate_license
    "key": license_code,
NameError: name 'license_code' is not defined

when I run the module gui.py itself it will run with no issue but with main.py I will get this error

CatChMeIfUCan
  • 569
  • 1
  • 7
  • 26
  • `gui()` doesn't look right. Do you understand at what point different pieces of code run when you import a module? – Mad Physicist Jul 05 '21 at 04:50
  • it will compile with pyinstaller and runs successfully is it wrong? but the problem is something else – CatChMeIfUCan Jul 05 '21 at 04:52
  • And do you understand the purpose of `if __name__ == '__main__':`? – Klaus D. Jul 05 '21 at 04:53
  • not exactly :D another guy just helped me in SO so I decided to put it in my code – CatChMeIfUCan Jul 05 '21 at 04:55
  • Why do you have `license_code, activation_code = read_configs('config.txt')` in an imported module? Do you want this code to run when main.py does `import gui`? – tdelaney Jul 05 '21 at 04:59
  • yes but is in the middle of code but I know the issue is from this line of code in gui module because whenever reach this step will break – CatChMeIfUCan Jul 05 '21 at 05:02
  • As you don't know what `__name__ == '__main__'` does, please read the duplicate. If you then still have problems understanding the behavior of your program, please update your question with more details, like what "breaks" exactly means, i.e. which behavior you expect and which behavior you are observing. If there is no behavior differences between pure and cythonized version, drop cython from your question, if difference due to usage of cython, please add information how you cythonize your code exactly. – ead Jul 05 '21 at 05:09
  • Add the error you mentioned to this question. It helps us see the problem you face. – tdelaney Jul 05 '21 at 05:26
  • i did update the post with error – CatChMeIfUCan Jul 05 '21 at 05:39

2 Answers2

1

When you import the module gui.py you can call its functions in the program main.py, as follows:

import gui


if __name__ == '__main__':
    license_code, activation_code = gui.read_configs('config.txt')

There is no issue at all the module gui.py also contains an if __name__ == '__main__':

Bruno Vermeulen
  • 2,970
  • 2
  • 15
  • 29
  • `Traceback (most recent call last): File "C:\Users\Administrator\Desktop\cython\main.py", line 24, in import gui File "gui.py", line 530, in init gui File "gui.py", line 113, in gui.activate_license "scope": { NameError: name 'license_code' is not defined` – CatChMeIfUCan Jul 05 '21 at 05:09
  • You have to learn to read and understand error messages. Obviously your code in `gui.activate_license` has a problem, but you didn't show this code. – Matthias Jul 05 '21 at 09:38
1

Python scripts are different than python modules. A script is the top level code executed by python and modules are anything else imported later. Scripts are always called __main__ while modules use their module or package name. That's usually any parent directories and the name of the .py file, minus its extension.

if __name__ == "__main__":

is a way to demark code that you only want run when a .py file is run as a top level script. Code inside this if is not run if the module is imported because an imported module can't be called "__main__".

This is import if, say, you run a script on Windows that also uses multiprocessing. Multiprocessing will re-import the top level script and you don't want that code running just on import.

Generally, if you want a .py file to run as a script and an imported module, you play the if __name__ == "__main__": game. If its always an imported module, you don't have that worry.

In your case, it looks like gui.py is supposed to be imported only, so there is no need for the if. If you take away the if the code will always run on import. license_code and activation_code will then be available for the other functions to use.

tdelaney
  • 73,364
  • 6
  • 83
  • 116