
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Locating Modules in Python
When you import a module, the Python interpreter searches for the module in the following sequences −
- The current directory.
- If the module isn't found, Python then searches each directory in the shell variable PYTHONPATH.
- If all else fails, Python checks the default path. On UNIX, this default path is normally /usr/local/lib/python/.
The module search path is stored in the system module sys as the sys.path variable. The sys.path variable contains the current directory, PYTHONPATH, and the installation-dependent default.
The PYTHONPATH Variable
The PYTHONPATH is an environment variable, consisting of a list of directories. The syntax of PYTHONPATH is the same as that of the shell variable PATH.
Here is a typical PYTHONPATH from a Windows system −
set PYTHONPATH = c:\python20\lib;
And here is a typical PYTHONPATH from a UNIX system −
set PYTHONPATH = /usr/local/lib/python
- Related Articles
- Locating and executing Python modules (runpy)
- Locating File Positions in Python
- Reloading modules in Python?
- XML Processing Modules in Python
- How do Python modules work?
- URL handling Python modules (urllib)
- How to install Python modules in Cygwin?
- XMLRPC server and client modules in Python
- Where are the python modules stored?
- How to use remote python modules?
- Can we keep Python modules in compiled format?
- How we can import Python modules in Jython?
- How does variable scopes work in Python Modules?
- Locating child nodes of WebElements in selenium.
- How to encapsulate Python modules in a single file?

Advertisements