I have a python module ('my_module.py') and in it two functions 'A' and 'B'.
They both require a 3rd module (say pandas).
Is there a way to load pandas as part of the module so it'll be accessible to both functions? (trying to avoid importing pandas per function call...)
Or do I have to convert this into a package and load pandas as part of the init? (trying to avoid that since I don't want an extra directory...)
2 Answers 2
Just import pandas at the start of the module.
It will be imported (and initialized) when my_module.py is imported or run, and it will be available for both functions. If several modules use pandas, import it at the start of each one - it will be imported only once per interpreter session.
Mockup example of mymodule.py:
import pandas
def A(foo):
return pandas.do_something(foo)
def B(bar):
return pandas.do_something_else(bar)
3 Comments
Use import pandas in your module my_module.py.
Inside your my_module.py
def A(sth):
return pandas.module(sth)
And similarly for B