Issue
I have two google.protobuf
modules on my Debian(stretch).
- /usr/local/lib/python2.7/dist-packages/google
- /home/myuser/.local/lib/python2.7/site-packages/google (installed with pip --user)
I'd like to import 2, but python
always gives me 1, while ipython
imports 2. I've tried set PYTHONPATH such that /home/myuser/.local/lib/python2.7/site-packages/
is the first in it, but not working.
Is there any way I can force python to search my $HOME/.local/lib/python2.7/site-packages/ first?
Solution
Yes. See here for an official description of how python determines which module to import first: https://docs.python.org/2/tutorial/modules.html#the-module-search-pathkk
See here for a way to change that default behavior: http://www.hasenkopf2000.net/wiki/python/how-override-pythons-module-import-order/
Of the two solutions provided on the hasenkopf site, the second will be less problematic if you change your mind on which module you want to use. You just edit the file rather than having to remember which symbolic links you created. Briefly, the code is:
import sys
# Assume path to module is
# /path/to/recent/version/of/module.py
sys.path.insert(0,"/path/to/recent/version/of")
import module
You place it at the top of your script.
Answered By - neallred