The Python Oracle

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

Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn

--

Music by Eric Matyas
https://www.soundimage.org
Track title: Cosmic Puzzle

--

Chapters
00:00 Question
00:20 Accepted answer (Score 968)
02:41 Answer 2 (Score 697)
02:57 Answer 3 (Score 370)
03:43 Answer 4 (Score 108)
04:45 Thank you

--

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

Accepted answer links:
[dist-packages]: https://stackoverflow.com/questions/9387...
[site module]: https://docs.python.org/3/library/site.h...
[getsitepackages is not available]: https://github.com/pypa/virtualenv/issue...
[older versions of ]: https://github.com/pypa/virtualenv/pull/...
[sysconfig module]: https://docs.python.org/3/library/syscon...
[PEP 370]: https://www.python.org/dev/peps/pep-0370/
[details]: https://stackoverflow.com/questions/2699...
[difference]: https://softwareengineering.stackexchang...

Answer 3 links:
["How to Install Django" documentation]: http://web.archive.org/web/2010072310572...

--

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