166

I have recently started learning Python and I have 2 questions relating to modules.

  1. Is there a way to obtain a list of Python modules available (i.e. installed) on a machine?
  2. 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)?
Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
morpheous
  • 16,270
  • 32
  • 89
  • 120
  • 1
    Possible duplicate: http://stackoverflow.com/questions/739993/unable-to-get-a-list-of-installed-python-modules – Xavier Ho May 28 '10 at 09:56
  • 1
    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 09 '21 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](https://docs.python.org/3/library/sys.html#sys.path) – Tom Saleeba Apr 21 '21 at 16:44

12 Answers12

158
  1. Is there a way to obtain a list of Python modules available (i.e. installed) on a machine?

This works for me:

help('modules')
  1. Where is the module code actually stored on my machine?

Usually in /lib/site-packages in your Python folder. (At least, on Windows.)

You can use sys.path to find out what directories are searched for modules.

wjandrea
  • 28,235
  • 9
  • 60
  • 81
Xavier Ho
  • 17,011
  • 9
  • 48
  • 52
  • 65
    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 '10 at 10:14
  • 2
    @NoufalIbrahim your answer is worth like the answer itself. TY. you can append it to make it bold for users. – behkod May 25 '17 at 08:16
136

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
Sahil Gupta
  • 58
  • 1
  • 7
PanNik
  • 1,564
  • 1
  • 8
  • 11
30

If you are using pip:

pip show <package name>

Sample output of pip show tensorflow:

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.

lollalolla
  • 454
  • 5
  • 10
19

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

C:\Users\Administrator\AppData\Local\Programs\Python\Python38\Lib
Trect
  • 2,759
  • 2
  • 30
  • 35
12

You can find module code by first listing the modules:

help("modules")

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
user3424230
  • 151
  • 1
  • 4
5
  1. You can iterate through directories listed in sys.path to find all modules (except builtin ones).
  2. 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.
Cat Plus Plus
  • 125,936
  • 27
  • 200
  • 224
3

If you are using conda or pip to install modules you can use

pip list

or

conda list

to display all the modules. This will display all the modules in the terminal itself and is much faster than

>>> help('modules')

Trect
  • 2,759
  • 2
  • 30
  • 35
2

1) Using the help function

Get into the python prompt and type the following command:

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

Graham
  • 3,153
  • 3
  • 16
  • 31
Abrar Ahmad
  • 109
  • 1
  • 3
0

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
questionto42
  • 7,175
  • 4
  • 57
  • 90
0

On my local machine (Win 10), it has the following path:

c:\Users\administrator\AppData\Roaming\Python\Python38\
rihekopo
  • 3,241
  • 4
  • 34
  • 63
0

Run python CLI for info

python -c "import sys; print('\n'.join(sys.path))"
General Grievance
  • 4,555
  • 31
  • 31
  • 45
karthik nair
  • 134
  • 1
  • 9
0

sys.meta_path is another source of modules, in addition to sys.path. It contains info that help('module') doesn't.

meta_path can explain unexpected import behaviors - if a module isn't on sys.path, it doesn't guarantee it won't be imported. Moreover, if a package is on sys.path, but a package with same name but different directory is in sys.meta_path, Python will import from both, prioritizing sys.path. This can be troublesome if you've intentionally removed something from the package in sys.path - Python will keep importing it from sys.meta_path. This includes certain non-Python files, like Cython's .pyx builds.

OverLordGoldDragon
  • 1
  • 9
  • 53
  • 101