How can I import from the standard library, when my project has a module with the same name? (How can I control where Python looks for modules?)

It’s not necessary to rename the module. Instead, in Python 2.5 and above, use absolute_import to change the importing behavior.

For example, to import the standard library socket module, even if there is a socket.py in the project:

from __future__ import absolute_import
import socket

In Python 3.x, this behaviour is the default. Pylint will complain about the code, but it’s perfectly valid.

Leave a Comment