There are two ways of doing this: put your module in the right place or tell the interpreter where to look.
1 Put your module in the right place
>>> import sys, pprint
>>> pprint.pprint(sys.path)
2 Tell the interpreter where to look
One way of doing this is to edit sys.path:
>>> import sys
>>> sys.path.append('c:/python') #using the Windows directory
>>> sys.path.append('~/python') #using the linux directory
but that is not a common way to do it. The standard method is to include your module directory (or directories) in the environment variable PYTHONPATH.
In UNIX and Mac OS X, you will probably set environment variables in some shell file that is executed every time you log in. If you use a shell such as bash, the file is .bashrc, found in your home directory. Add the following to that file to add the directory ~/python to your PYTHONPATH:
export PYTHONPATH=$PYTHONPATH:~/python
Note that this usage only work for bash. if you use IDE, you can use some other way to add the new python path.
3)Packages
To structure your modules, you can group them into packages. A package is basically just another type of module. The interesting thing about them is that they can contain other modules. While a module is stored in a file (with the file name extension .py), a package is a directory. To make Python treat it as a package, it must contain a file (module) named __init__.py. The contents of this file will be the contents of the package, if you import it as if it were a plain module. For example, if you had a package named constants, and the file constants/__init__.py contains the statement PI = 3.14, you would be able to do the following:
import constants
print constants.PI
To put modules inside a package, simply put the module files inside the package directory.
4) Explore Modules
1 Using dir
To find out what a module contains, you can use the dir function, which lists all the attributes of an object (and therefore all functions, classes, variables, and so on of a module)
>>> import copy
>>> dir(copy)
2 The __all__ variable
It store the public interfaces of the module.
>>> import copy
>>> copy.__all__
['Error', 'copy', 'deepcopy']
More specifically, it tells the interpreter what it means to import all the names from this module. So if you use this:
from copy import *
you get only the three functions listed in the __all__ variable.
To import PyStringMap(not a default public interface), for example, you would need to be explicit, by either importing copy and using copy.PyStringMap, or by using " from copy import PyStringMap ".If you don’t set __all__, the names exported in a starred import defaults to all global names in the module that don’t begin with an underscore.
3 Get help with help()
>>> help(copy.copy)
4 Use the documentation
>>> print range.__doc__
range([start,] stop[, step]) -> list of integers ……
5 Use the source
>>> print copy.__file__
/usr/lib/python2.7/copy.pyc #linux environment
Reading source code is, in fact, one of the best ways to learn Python—besides coding yourself.