The Python Oracle

How do I find the location of my Python site-packages directory?

--------------------------------------------------
Hire the world's top talent on demand or became one of them at Toptal: https://topt.al/25cXVn
and get $2,000 discount on your first invoice
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Puzzle Game 3 Looping

--

Chapters
00:00 How Do I Find The Location Of My Python Site-Packages Directory?
00:13 Answer 1 Score 383
00:46 Answer 2 Score 109
01:32 Answer 3 Score 84
01:41 Answer 4 Score 703
01:51 Thank you

--

Full question
https://stackoverflow.com/questions/1223...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python

#avk47



ANSWER 1

Score 703


>>> import site; site.getsitepackages()
['/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages']

(or just first item with site.getsitepackages()[0])




ANSWER 2

Score 383


A solution that:

  • outside of virtualenv - provides the path of global site-packages,
  • insidue a virtualenv - provides the virtualenv's site-packages

...is this one-liner:

python -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())"

Formatted for readability (rather than use as a one-liner), that looks like the following:

from distutils.sysconfig import get_python_lib
print(get_python_lib())


Source: an very old version of "How to Install Django" documentation (though this is useful to more than just Django installation)




ANSWER 3

Score 109


For Ubuntu,

python -c "from distutils.sysconfig import get_python_lib; print get_python_lib()"

...is not correct.

It will point you to /usr/lib/pythonX.X/dist-packages

This folder only contains packages your operating system has automatically installed for programs to run.

On ubuntu, the site-packages folder that contains packages installed via setup_tools\easy_install\pip will be in /usr/local/lib/pythonX.X/dist-packages

The second folder is probably the more useful one if the use case is related to installation or reading source code.

If you do not use Ubuntu, you are probably safe copy-pasting the first code box into the terminal.




ANSWER 4

Score 84


This is what worked for me:

python -m site --user-site