How can I get a list of locally installed Python modules?

前端 未结 30 2590
庸人自扰
庸人自扰 2020-11-22 04:32

I would like to get a list of Python modules, which are in my Python installation (UNIX server).

How can you get a list of Python modules installed in your computer?

30条回答
  •  孤城傲影
    2020-11-22 05:08

    Warning: Adam Matan discourages this use in pip > 10.0. Also, read @sinoroc's comment below

    This was inspired by Adam Matan's answer (the accepted one):

    import tabulate
    try:
      from pip import get_installed_distributions
    except:
      from pip._internal.utils.misc import get_installed_distributions
    
    tabpackages = []
    for _, package in sorted([('%s %s' % (i.location, i.key), i) for i in get_installed_distributions()]):
      tabpackages.append([package.location, package.key, package.version])
    
    print(tabulate.tabulate(tabpackages))
    

    which then prints out a table in the form of

    19:33 pi@rpi-v3 [iot-wifi-2] ~/python$ python installed_packages.py
    -------------------------------------------  --------------  ------
    /home/pi/.local/lib/python2.7/site-packages  enum-compat     0.0.2
    /home/pi/.local/lib/python2.7/site-packages  enum34          1.1.6
    /home/pi/.local/lib/python2.7/site-packages  pexpect         4.2.1
    /home/pi/.local/lib/python2.7/site-packages  ptyprocess      0.5.2
    /home/pi/.local/lib/python2.7/site-packages  pygatt          3.2.0
    /home/pi/.local/lib/python2.7/site-packages  pyserial        3.4
    /usr/local/lib/python2.7/dist-packages       bluepy          1.1.1
    /usr/local/lib/python2.7/dist-packages       click           6.7
    /usr/local/lib/python2.7/dist-packages       click-datetime  0.2
    /usr/local/lib/python2.7/dist-packages       construct       2.8.21
    /usr/local/lib/python2.7/dist-packages       pyaudio         0.2.11
    /usr/local/lib/python2.7/dist-packages       tabulate        0.8.2
    -------------------------------------------  --------------  ------
    

    which lets you then easily discern which packages you installed with and without sudo.


    A note aside: I've noticed that when I install a packet once via sudo and once without, one takes precedence so that the other one isn't being listed (only one location is shown). I believe that only the one in the local directory is then listed. This could be improved.

提交回复
热议问题