Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 615
Andrzej Pronobis Avatar asked Nov 09 '22 12:11

Andrzej Pronobis


1 Answers

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.

like image 158
Ethan Furman Avatar answered Nov 12 '22 03:11

Ethan Furman