Importing external package once in my module without it being added to the namespace

不羁岁月 提交于 2021-01-27 13:14:38

问题


I apologize for not being able to phrase my question more easily. I am writing a large package that makes extensive use of pandas in almost every function. My first instinct, naturally, was to create an __init__.py as

import pandas
# then import my own submodules and other things

And then, every time I use pandas in a function, call it from the submodules as from . import pandas as pd or from .. import pandas, or something like that.

However, if I do this, when I load my package, pandas appears as a "submodule", i.e., there is a mypackage.pandas. Which doesn't hurt anyone, but I'm guessing is not correct. A way to avoid this would be adding a del pandas at the end of __init__.py, which also doesn't seem like the correct approach.

So from now on I don't import pandas in my __init__ and import it separately inside every -function-, which works fine, but is too repetitive and prevents me from setting global pandas settings.

What is the preferred approach here? Is there a method which I am missing?

Thank you.


回答1:


...by importing pandas from the __init__.py call I can define some pandas' options there (like pandas.options.display.expand_frame_repr) and it will be valid throughout the module.

They will be anyway. The module is only loaded the first time you call import pandas. At that point a reference to the module is stored in a module dictionary accessible via sys.modules. Any subsequent calls to import pandas in any other module will re-use the same reference from sys.modules, so any options you changed will also apply.

Furthermore, re-importing the same package from scratch seems to me that takes longer, but I'm not sure it that is correct.

It should actually be marginally faster, since it doesn't have to resolve relative paths. Once the module has been loaded, subsequent calls work like...

import pandas          # pandas = sys.modules['pandas']
import pandas as pd    # pd = sys.modules['pandas']


来源:https://stackoverflow.com/questions/38125727/importing-external-package-once-in-my-module-without-it-being-added-to-the-names

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!