Andrzej Pronobis
Andrzej Pronobis

Reputation: 36076

Order of imports and paths in sys.path in Python 2 and Python 3

I have a question regarding the order of imports and paths placed by default in sys.path by Python 2 and Python 3. I compared the default paths in my sys.path in both Python 2.7 and 3.4 with PYTHONPATH set to empty. I'm on Ubuntu 14.10.

I also have numpy installed both system-wide through apt-get in /usr/lib (version 1.8.2) and locally in /home/user/.local/lib (version 1.9.2) for both Python 2 and Python 3.

I get the following results:

Python 2.7

>>> print('\n'.join(sys.path))

/usr/lib/python2.7
/usr/lib/python2.7/plat-x86_64-linux-gnu
/usr/lib/python2.7/lib-dynload
/home/user/.local/lib/python2.7/site-packages
/usr/local/lib/python2.7/dist-packages
/usr/lib/python2.7/dist-packages
/usr/lib/pymodules/python2.7

>>> import numpy
>>> numpy.version 
<module 'numpy.version' from  /home/user/.local/lib/python2.7/site-packages/numpy/version.pyc'>
>>> numpy.version.version
'1.9.2'

Python 3.4

>>> print('\n'.join(sys.path))

/usr/lib/python3/dist-packages
/usr/lib/python3.4
/usr/lib/python3.4/plat-x86_64-linux-gnu
/usr/lib/python3.4/lib-dynload
/home/user/.local/lib/python3.4/site-packages
/usr/local/lib/python3.4/dist-packages

>>> import numpy
>>> numpy.version 
<module 'numpy.version' from '/usr/lib/python3/dist-packages/numpy/version.py'>
>>> numpy.version.version
'1.8.2'

Could you help me understand why in Python 3 /usr/lib/python3/dist-packages is higher than /home/user/.local/lib/python3.4/site-packages which results in my older system installation of numpy being imported by default, while it's the other way around in Python 2?

Upvotes: 5

Views: 1581

Answers (1)

Ethan Furman
Ethan Furman

Reputation: 69021

I'm pretty sure this is an ubuntu thing, not a Python thing. Check the global site customize (in /etc/python2.7 and /etc/python3.4) as well as the site.py files in /usr/lib/python2.7 and /usr/lib/python3.4.

Upvotes: 1

Related Questions