I wanted to run a python method with another version of random_word
package. Basically, I have 2 versions of this package installed, one is there in '/home/arya/Documents/random-word'
and the other is in '/home/arya/myvenv/lib/python3.6/site-packages'
. So, just to try out, I started with this piece of code.
import sys
print(sys.path)
import random_word
print(random_word.__file__)
del random_word
sys.path[-2],sys.path[-1] = sys.path[-1],sys.path[-2]
print(sys.path)
import random_word
print(random_word.__file__)
On running,
(myvenv) arya@arya-HP-Notebook:~/Desktop$ python test.py
it prints
sys.path : ['', '/home/arya/myvenv/lib/python3.6/site-packages', '/home/arya/Documents/random-word']
location of random_word package: /home/arya/myvenv/lib/python3.6/site-packages/random_word/__init__.py
sys.path : ['', '/home/arya/Documents/random-word', '/home/arya/myvenv/lib/python3.6/site-packages']
location of random_word package: /home/arya/myvenv/lib/python3.6/site-packages/random_word/__init__.py
I have one installation of random_word
package in /home/arya/Documents/random-word
and the other one is there in the site-packages directory of my virtualenv.
So, my doubt is even after I change the order of the directories in sys.path
, why my python program only finds the random_word
in site-packages
?
Note: If I change the order before the first import random_word
statement, then python picks up the random_word
installed in /home/arya/Documents/random-word
. I couldn't understand why doesn't it work for the earlier case.
Thanks.
Basically, what you are seeing is the module cache. When you are importing a module, Python will cache that module internally, so a subsequent import can just refer to the same module instance again. Python does this optimization to prevent having to recompile modules again. This also effectively makes modules singletons, since there can only ever be a single instance of it.
By calling del random_word
, you are just removing a reference to that module but that will not unload the module. So when you later import the random_word
module again, Python does not actually look inside of the import path again but will just reuse the module it already has.
That is why changing the path order after having imported the module once will not have any effect.
There are some questions covering the idea of explicitly reloading already imported modules but I would generally recommend not to rely on that. The cache is there for a good reason, and you should name your modules accordingly, so that you do not run into conflicts you get from having the same module name in multiple locations.