Reputation: 15405
In some code I'm looking at there is the following statement:
from math import exp, sqrt, ceil
however there is no folder called math in the project folder and no modules named exp, sqrt, and ceil. My question is basically where are these modules being imported from and how do I see them and other files like them? Thanks in advance.
Upvotes: 1
Views: 236
Reputation: 491
When a module named spam is imported, the interpreter first searches for a built-in module with that name. If not found, it then searches for a file named spam.py in a list of directories given by the variable sys.path. sys.path is initialized from these locations:
- The directory containing the input script (or the current directory).
- PYTHONPATH (a list of directory names, with the same syntax as the shell variable PATH).
- the installation-dependent default.
After initialization, Python programs can modify sys.path. The directory containing the script being run is placed at the beginning of the search path, ahead of the standard library path. This means that scripts in that directory will be loaded instead of modules of the same name in the library directory. This is an error unless the replacement is intended. See section Standard Modules for more information.
From a shell you can type the following to get the default sys.path
>>> import sys
>>> print sys.path
['', '/usr/lib/python2.6', '/usr/lib/python2.6/plat-linux2', '/usr/lib/python2.6/lib-tk', '/usr/lib/python2.6/lib-old', '/usr/lib/python2.6/lib-dynload', '/usr/lib/python2.6/dist-packages', '/usr/lib/python2.6/dist-packages/PIL', '/usr/lib/python2.6/dist-packages/gst-0.10', '/usr/lib/pymodules/python2.6', '/usr/lib/python2.6/dist-packages/gtk-2.0', '/usr/lib/pymodules/python2.6/gtk-2.0', '/usr/local/lib/python2.6/dist-packages']
Upvotes: 0
Reputation: 26080
You have some confused terminology. In this case, math
is the module, and exp, sqrt, ceil
are functions it defines. Usually it is from <module> import <function/class>
. math
is a base module which is included with every Python installation. Python has set of specific places it will look for the module. In this case, math
will be a dynamically loaded module written in C.
You can find out where it comes from by doing:
import math
math.__file__
Note that this will give an error for anything that is inbuilt into the interpreter, however.
Upvotes: 1
Reputation: 46203
The math
module is part of Python's standard library and is always available in any Python installation. However, since the functions are not builtins, they do need to be imported.
Upvotes: 0
Reputation: 963
You are seeing the Python standard libraries. They are resolved by searching the PYTHONPATH for matching modules. In addition to the PYTHONPATH, you can import from any subfolders of your python script that contain a file called __init__.py
Upvotes: 0
Reputation: 186068
You are importing a standard Python module. See here for the documentation for the math module, and here for the full standard library.
The location of the module on your filesystem varies across environments. Don't bother trying to locate stuff there. Just bookmark the documentation.
Upvotes: 0