How do I find the location of my Python site-packa

2018-12-31 10:10发布

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

18条回答
深知你不懂我心
2楼-- · 2018-12-31 10:26

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.

查看更多
残风、尘缘若梦
3楼-- · 2018-12-31 10:27

This is what worked for me:

python -m site --user-site
查看更多
荒废的爱情
4楼-- · 2018-12-31 10:29

From "How to Install Django" documentation (though this is useful to more than just Django installation) - execute the following from the shell:

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())
查看更多
栀子花@的思念
5楼-- · 2018-12-31 10:29

If it is already added to the PYTHONPATH you can also do something like

import sys
print('\n'.join(sys.path))
查看更多
皆成旧梦
6楼-- · 2018-12-31 10:30

There are two types of site-packages directories, global and per user.

  1. Global site-packages ("dist-packages") directories are listed in sys.path when you run:

    python -m site
    

    For a more concise list run getsitepackages from the site module in Python code:

    python -c "import site; print(site.getsitepackages())"
    

    Note: With virtualenvs getsitepackages is not available, sys.path from above will list the virtualenv's site-packages directory correctly, though.

  2. The per user site-packages directory (PEP 370) is where Python installs your local packages:

    python -m site --user-site
    

    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 or pip freeze --user gives you a list of all installed per user site-packages.

查看更多
公子世无双
7楼-- · 2018-12-31 10:34

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 where site-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.

import sys
import os
from distutils.command.install import INSTALL_SCHEMES

if os.name == 'nt':
    scheme_key = 'nt'
else:
    scheme_key = 'unix_prefix'

print(INSTALL_SCHEMES[scheme_key]['purelib'].replace('$py_version_short', (str.split(sys.version))[0][0:3]).replace('$base', ''))

That should print something like /Lib/site-packages or /lib/python3.6/site-packages.

查看更多
登录 后发表回答