Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 342
Michael Goldshteyn Avatar asked Jul 29 '16 14:07

Michael Goldshteyn


People also ask

Where are Python3 packages installed?

When a package is installed globally, it's made available to all users that log into the system. Typically, that means Python and all packages will get installed to a directory under /usr/local/bin/ for a Unix-based system, or \Program Files\ for Windows.

Where does Python look for site-packages?

there is a site package directory in a virtualenv. You can get the directory for site-specific modules inside/outside virtualenv using python -c "from distutils. sysconfig import get_python_lib; print(get_python_lib())" (it works on both Python 2 and 3 too).

Which Python is pip installing to?

PIP is automatically installed with Python 2.7. 9+ and Python 3.4+ and it comes with the virtualenv and pyvenv virtual environments. Before you install PIP on Windows, check if PIP is already installed. 1.


1 Answers

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

like image 143
be_good_do_good Avatar answered Oct 21 '22 04:10

be_good_do_good