pythonpandasimportmodin

Top level imports supersede lower level imports?


In a jupyter notebook, I have

import modin.pandas as pd
import utils

utils.py has import pandas as pd

Does the pd in utils.py import pandas, or modin.pandas? If the former, is there a way for me to make utils.py use modin.pandas from the jupyter notebook, without changing it in the code of utils.py


Solution

  • The utils module will always import pandas as pd, using the utils module in another module will not change this even if you import modins.pandas as pd in another module. This is because the symbol pd is tied to the module dictionary and this module dictionary is isolated from another module's dictionary, which essentially represents the core idea of having different namespaces for different modules.

    The way to use modins.pandas in utils is by updating the symbol pd. You can easily do this by replacing the old symbol with new symbol using the setattr method:

    import modin.pandas as pd
    import utils
    
    setattr(utils, 'pd', pd)