Search code examples
pythonpython-2.7canopy

Need to re-load libraries in Python, with Canopy


I am working on a python project with Canopy, using my own library, which I modify from time to time to change or add functions inside.

At the beginning of myfile.py I have from my_library import * but if I change a function in this library and compute again myfile.py it keep using the previous version of my function.

I tried the reload function :

import my_library
reload(my_library)
from other_python_file import *
from my_library import *

and it uses my recently changed library.

But if it is :

import my_library
reload(my_library)
from my_library import *
from other_python_file import *

It gives me the result due to the version loaded the first time I launched myfile.py.

Why is there a different outcome inverting the 3rd and 4th line ?


Solution

  • Without seeing the source code, it's hard to be certain. (For future reference, it is most useful to post a minimal example, which I suspect would be about 10 lines of code in this case.)

    However from your description of the problem, my guess is that your other_python_file also imports my_library. So when you do from other_python_file import *, you are also importing everything that it has already imported from my_library, which in your second example, will override the imports directly from my_library (Since you didn't reload other_python_file, it will still be using the previous version of my_library.)

    This is one out of approximately a zillionteen reasons why you should almost never use the form from xxx import * except on the fly in interactive mode (and even there it can be dangerous but can be worth the tradeoff for the convenience). In a python source file, there's no comparable justification for this practice. See the final point in the Imports section of PEP-8.