Python local and global imports

I don't see an answer to this, so I apologize if this is a duplicate. Basically, I'm trying to figure out how to get my interpreter (2.7) to import a module from site packages if there is a conflict. For example, imagine you are using python from the (top_level) directory, which has the following structure:

top_level
----cool_mod
    ----init.py
    ----sweet_module.py

      

but you have already installed weak site modules. If in this directory (but no others), if you run:

from cool_mod.sweet_module import *

      

you are importing from a local module, not a global one. Can I change this?

This situation can arise from the case:

top_level

setup.py
----cool_mod
    ----init.py
    ----sweet_module.py

      

You can run cool_mod.sweet_module before installation if the working directory is top_level. But after installation, you can import cool_mod.sweet_module from anywhere. However, if you ever import from that directory, even after installation, you still import the local copy

+3


source to share


1 answer


Insert the site package directory at the beginning of sys.path and then import.



Or use imp.load_source

to load a module from the specified path.

+1


source







All Articles