Importing pandas into main and modules?

huangapple go评论51阅读模式
英文:

Python: Importing pandas into main and modules?

问题

You can import pandas in both the main.py and fbm.py without a significant impact on memory usage. Python's import system is designed to handle this efficiently. Each module will have access to the same instance of pandas, so you won't be loading multiple copies into memory. This approach is acceptable and ensures that your code is more modular and maintainable.

Importing pandas as pd in both files is a common practice and helps maintain consistency throughout your codebase. There is no need to worry about memory usage or inefficiency in this case.

英文:

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.

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?

答案1

得分: 2

只返回翻译好的部分:

在主脚本和每个需要它的模块中都import pandas(或任何其他库)是合适的。这是一种常见的做法,可以确保每个模块都可以独立运行,从而使代码更模块化且更易于维护。

关于您对内存使用的担忧:Python足够智能,可以高效处理导入。当首次导入模块时,它会加载并缓存在sys.modules字典中。因此,如果再次导入相同的模块(即使使用不同的别名),Python将使用缓存的模块,而不是再次加载它,因此不会对内存使用造成重大影响。

英文:

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.

huangapple
  • 本文由 发表于 2023年4月20日 07:07:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/76059444.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定