1

I am trying to make my python script more modular -- it works properly when everything is in just one lengthy .py file, but I want to use the main python file to call other files to streamline the flow and make upgrades easier.

I'm struggling with package imports. One package I'm using is os, which I import in the main file:

import os
import pandas as pd
import numpy as np
from code_module_1 import *

if __name__ == '__main__':
    code_module_1()

I also import it at the top of the python file that is called, code_module_1.py:

import os
import glob
import pandas as pd
import numpy as np

def function_called_from_main():
    for root, dirs, files in os.walk('...file_path'):
        etc.

When I do this, I receive an error saying the name 'os' is not defined, which I've taken to mean that the package isn't being imported in code_module_1.

I've attempted to fix this by placing the lines of code that import packages inside of the function that I'm calling from the main script, but I still run into the same error. Where should I be importing packages, and how do I make sure that other python files that are called have the packages that they need to run?

Jadon
  • 124
  • 9

1 Answers1

0

With the following line

from code_module_1 import *

you import everything defined in the module. So basically, all function definitions are available.

Next, you can't execute a module. You can only execute a function or a statement. Thus, the following line

if __name__ == '__main__':
    code_module_1()

should be something like this:

if __name__ == '__main__':
    function_called_from_main()

where you execute the function function_called_from_main that was previously imported.

momo
  • 3,313
  • 2
  • 19
  • 37
  • Ok, thanks for clarifying that. I've edited the code accordingly. `function_called_from_main()` requires several packages to run, such as `os`. There are three locations where I could import them: (1) In the main file, which is already taking place; (2) In `code_module_1`, outside of `function_called_from_main()`; and (3) In `code_module_1`, inside of `function_called_from_main()`. Which option ensures that the packages are available? – Jadon Mar 18 '20 at 17:01
  • Modules should be independent which means they should contain everything that they need in order to execute. Don't worry if you already imported the module earlier. The Python interpreter will take a reference and not load the module again. Importing a module inside a function points usually to a bad design. – momo Mar 18 '20 at 17:14
  • Ok, awesome. I guess my confusion stems from the fact that I'm only calling a function from within the module, so the lines in the module that actually import packages are never explicitly being called. When the python interpreter loads a module, does it read the import lines? – Jadon Mar 18 '20 at 17:19
  • Yes, that's correct. All modules that are declared in ```code_module_1``` are loaded as soon as ```from code_module_1 import *``` gets executed. – momo Mar 18 '20 at 17:46
  • Thank you so much, I'm not sure how to rephrase my top question, but that's the exact answer I was looking for! Is that also the case if I write `import code_module_1` instead of `from code_module_1 import *`? – Jadon Mar 18 '20 at 18:11
  • Yes, it is the same case. However, the way you access the function would be different. It must be something like ```code_module_1.function_called_from_main()```. James explains it well: https://stackoverflow.com/a/21547572/4033690 – momo Mar 18 '20 at 18:23
  • Wonderful. Thank you so much for your help! – Jadon Mar 18 '20 at 18:35