相关文章推荐
有情有义的大白菜  ·  python ...·  3 周前    · 
完美的馒头  ·  python QTreeWidget ...·  3 周前    · 
失眠的烤红薯  ·  python qt textBrowser ...·  2 周前    · 
帅气的领带  ·  【Pyspark ...·  昨天    · 
近视的橙子  ·  python ...·  3 小时前    · 
魁梧的羊肉串  ·  Lecture_P_2_1Dplot slides·  4 月前    · 
坏坏的雪糕  ·  一个小技巧提升 OkHttp ...·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams
  • Is there a way to obtain a list of Python modules available (i.e. installed) on a machine?
  • I am using Ubuntu Karmic and Synaptic for package management. I have just installed a python module.Where is the module code actually stored on my machine? (is there a default [recommended] location that modules are stored)?
  • If you import sys then run sys.path() , it shows all the paths for python. /usr/local/lib/python3.x/dist-packages worked for me. cs1349459 Mar 9, 2021 at 17:01 In python 3.9, path is not a function, it's A list of strings that specifies the search path for modules . docs Tom Saleeba Apr 21, 2021 at 16:44 If you want the location of a specific module, import it and look at it's __file__ attribute. Works for most of them. Noufal Ibrahim May 28, 2010 at 10:14 @NoufalIbrahim your answer is worth like the answer itself. TY. you can append it to make it bold for users. Behrad Khodayar May 25, 2017 at 8:16

    On python command line, first import that module for which you need location.

    import module_name
    

    Then type:

    print(module_name.__file__)
    

    For example to find out "pygal" location:

    import pygal
    print(pygal.__file__)
    

    Output:

    /anaconda3/lib/python3.7/site-packages/pygal/__init__.py
                    When I tried this, I got this error: Unable to initialize device PRN, any idea why? Thanks.
    – Azurespot
                    Sep 25, 2019 at 2:28
    
    Name: tensorflow
    Version: 2.1.1
    Summary: TensorFlow is an open source machine learning framework for everyone.
    Home-page: https://www.tensorflow.org/
    Author: Google Inc.
    Author-email: packages@tensorflow.org
    License: Apache 2.0
    Location: /home/user/.local/lib/python3.6/site-packages
    Requires: termcolor, six, astor, numpy, grpcio, absl-py, protobuf, tensorflow-estimator, tensorboard, gast, keras-applications, opt-einsum, wheel, keras-preprocessing, google-pasta, scipy, wrapt
    Required-by: tf-models-official
    

    The installed location is shown at Location:/home/user/.local/lib/python3.6/site-packages.

    On Windows machine python modules are located at (system drive and python version may vary):

    C:\Users\Administrator\AppData\Local\Programs\Python\Python38\Lib
                    I guess it is best practice to use %appdata% in the address window of the Explorer to get quickly to the Roaming folder and jump back and forth to the Local folder then.
    – questionto42
                    Dec 12, 2020 at 14:08
                    Note: I found mine in C:\Python39\Lib\site-packages, probably because I installed python differently...
    – AndyS
                    Apr 23, 2021 at 15:28
    

    This spits out a list of modules Python can import. At the bottom of this list is a phrase:

    Enter any module name to get more help. Or, type "modules spam" to search for modules whose name or summary contain the string "spam".

    To find module location:

    help("module_Name")
    

    for example:

    help("signal")
    

    A lot of information here. Scroll to the bottom to find its location

    /usr/lib/python3.5/signal.py
    

    Copy link. To see code, after exiting Python REPL:

    nano /usr/lib/python3.5/signal.py
    
  • You can iterate through directories listed in sys.path to find all modules (except builtin ones).
  • It'll probably be somewhere around /usr/lib/pythonX.X/site-packages (again, see sys.path). And consider using native Python package management (via pip or easy_install, plus yolk) instead, packages in Linux distros-maintained repositories tend to be outdated.
  • to display all the modules. This will display all the modules in the terminal itself and is much faster than

    >>> help('modules')
    

    This will list all the modules installed in the system. You don't need to install any additional packages to list them, but you need to manually search or filter the required module from the list.

    2) Using pip freeze

    sudo apt-get install python-pip
    pip freeze
    

    Even though you need to install additional packages to use this, this method allows you to easily search or filter the result with grep command. e.g. pip freeze | grep feed.

    You can use whichever method is convenient for you.

    On Linux, use grep to find a chosen module, no extra installation needed for that, quickly done.

    The -r stands for recursive search in the sub-directories and the l to show only the files, not the directories. Usually you can see the locations from the upcoming list, and you can stop the output with Ctrl-C.

    grep -rl module_name_or_part_of_name /
    

    or, borrowed from the value comment here from this user:

    pip list | grep module_name_or_part_of_name