Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python: Importing pandas into main and modules?

I have a python script with a structure that looks like the following:

/
  __init__.py
  main.py
  /modules
    fbm.py

I am attempting to split some functions out from main.py into fbm.py and then import fbm.py as a module using.

sys.path.extend([f'{item[0]}' for item in os.walk("./app/modules/") if os.path.isdir(item[0])])
import fbm

This is working.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

What I am having trouble with is where I need to call pandas (or another import) from a module.
I’m currently importing pandas in main.py with import pandas as pd.
When I call the function from fbm an error is thrown as soon as it hits a reference to pd stating that NameError: name 'pd' is not defined. pd is defined in main.py, but not fbm.py. It thus works in main, but not in fbm.

So, my question is: Is it good and appropriate to import pandas as pd in both the main.py and each module which requires pandas?

  • Will this have an impact on memory usage, ie loading multiple copies of pandas
  • is there a better way to handle this?

>Solution :

It is appropriate to import pandas (or any other library) in both the main script and each module that requires it. This is a common practice and ensures that each module can function independently, which makes the code more modular and easier to maintain.

Regarding your concerns about memory usage: Python is smart enough to handle imports efficiently. When a module is imported for the first time, it’s loaded and cached in the sys.modules dictionary. So, if the same module is imported again (even with a different alias), Python will use the cached module instead of loading it again and again. So, there won’t be any significant impact on memory usage.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading