Michael Goldshteyn Michael Goldshteyn - 1 month ago 14
Python Question

Why is Python 3 looking in my Python 2.7 package directory for packages?

OS (Linux): Ubuntu 14.04.4 LTS (Trusty Tahr)

For some reason, my Python 3.5.2 is looking into the Python 2.7 packages directory instead of its own:

] python3 -m ensurepip

Ignoring indexes: https://pypi.python.org/simple
Requirement already satisfied (use --upgrade to upgrade):
setuptools in /usr/local/lib/python3.5/site-packages
Requirement already satisfied (use --upgrade to upgrade):
pip in /usr/local/lib/python2.7/dist-packages


More details:

] python3
Python 3.5.2 (default, Jul 29 2016, 09:41:38)
[GCC 6.1.1 20160511] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import site; site.getsitepackages()
['/usr/local/lib/python3.5/site-packages']
>>>


^^^-- That seems correct and does not mention anything about the 2.7 packages directory.

It looks like it should only be looking in
/usr/local/lib/python3.5/site-packages
, but for some reason, it is also looking in
/usr/local/lib/python2.7/dist-packages
where it has no business in looking.

For example, look at what happens when I try to install
psycopg2
as a Python 3 module:

] python3 -m pip install psycopg2
Requirement already satisfied (use --upgrade to upgrade):
psycopg2 in /usr/local/lib/python2.7/dist-packages


It is finding it as an installed package in the 2.7 distribution and failing to install its Python 3 version in
/usr/local/lib/python3.5/site-packages
.

To add even more confusion into the mix, I try going straight for pip 3, but to no avail:

] pip3 install psycopg2
Requirement already satisfied (use --upgrade to upgrade):
psycopg2 in /usr/local/lib/python2.7/dist-packages

] cat `which pip3`
#!/usr/local/bin/python3

# -*- coding: utf-8 -*-
import re
import sys

from pip import main

if __name__ == '__main__':
sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0])
sys.exit(main())


Update:
PYTHONPATH
was set to
/usr/local/lib/python2.7/dist-packages
. This was the cause of the issue above. Credit goes to user be_good_do_good for helping me figure out which screw to turn to get things to work as they should.

Answer

PYTHONPATH might have been set to 2.7 distribution packages, which might be causing this.

Comments