How do I find the location of my site-packages directory?
相关问题
- How does the setup bootstrapper detect if prerequi
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Evil ctypes hack in python
A side-note: The proposed solution (
distutils.sysconfig.get_python_lib()
) does not work when there is more than one site-packages directory (as recommended by this article). It will only return the main site-packages directory.Alas, I have no better solution either. Python doesn't seem to keep track of site-packages directories, just the packages within them.
This is what worked for me:
From "How to Install Django" documentation (though this is useful to more than just Django installation) - execute the following from the shell:
Formatted for readability (rather than use as a one-liner), that looks like the following:
If it is already added to the
PYTHONPATH
you can also do something likeThere are two types of site-packages directories, global and per user.
Global site-packages ("dist-packages") directories are listed in
sys.path
when you run:For a more concise list run
getsitepackages
from the site module in Python code:Note: With virtualenvs getsitepackages is not available,
sys.path
from above will list the virtualenv's site-packages directory correctly, though.The per user site-packages directory (PEP 370) is where Python installs your local packages:
If this points to a non-existing directory check the exit status of Python and see
python -m site --help
for explanations.Hint: Running
pip list --user
orpip freeze --user
gives you a list of all installed per user site-packages.I had to do something slightly different for a project I was working on: find the relative site-packages directory relative to the base install prefix. If the site-packages folder was in
/usr/lib/python2.7/site-packages
, I wanted the/lib/python2.7/site-packages
part. I have, in fact, encountered systems wheresite-packages
was in/usr/lib64
, and the accepted answer did NOT work on those systems.Similar to cheater's answer, my solution peeks deep into the guts of Distutils, to find the path that actually gets passed around inside
setup.py
. It was such a pain to figure out that I don't want anyone to ever have to figure this out again.That should print something like
/Lib/site-packages
or/lib/python3.6/site-packages
.