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
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", ie, 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.
...by
import
ingpandas
from the__init__.py
call I can define somepandas
' options there (likepandas.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']
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.