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
(or just first item with
site.getsitepackages()[0]
)As others have noted,
distutils.sysconfig
has the relevant settings:...though the default
site.py
does something a bit more crude, paraphrased below:(it also adds
${sys.prefix}/lib/site-python
and adds both paths forsys.exec_prefix
as well, should that constant be different).That said, what's the context? You shouldn't be messing with your
site-packages
directly; setuptools/distutils will work for installation, and your program may be running in a virtualenv where your pythonpath is completely user-local, so it shouldn't assume use of the system site-packages directly either.All the answers (or: the same answer repeated over and over) are inadequate. What you want to do is this:
The final line shows you the installation dir. Works on Ubuntu, whereas the above ones don't. Don't ask me about windows or other dists, but since it's the exact same dir that easy_install uses by default, it's probably correct everywhere where easy_install works (so, everywhere, even macs). Have fun. Note: original code has many swearwords in it.
For Ubuntu,
...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.
This should work on all distributions in and out of virtual environment due to it's "low-tech" nature. The os module always resides in the parent directory of 'site-packages'
To change dir to the site-packages dir I use the following alias (on *nix systems):