简体   繁体   中英

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

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)

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM