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.
 
    