
The Problem
You’ve installed a Python module using pip
, easy_install
, or python setup.py install
. The installation reports success, but when you try to import it, Python throws:
>>> import mechanize
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named mechanize
This is confusing because the installation appeared to succeed.
The Most Likely Cause: Permissions
In many cases, the module did install, but it was placed in a directory where only root has read/write permission.
That means:
- The package exists on disk.
- Your normal user account cannot read it.
- Python, running as your user, can’t import it—hence the error.
This often happens if you run:
sudo pip install mechanize
Instead of installing for your user.
The Fix: Correct Permissions or Reinstall for User
1. Check permissions
Find where the module is installed:
python -m site
# or
pip show mechanize
Inspect the directory with:
ls -ld /usr/local/lib/python2.7/dist-packages/mechanize*
If you see root root
as the owner, that’s the problem.
2. Fix permissions
Option A — Change ownership to your user:
sudo chown -R $USER:$USER /usr/local/lib/python2.7/dist-packages/mechanize*
Option B — Add read permission for all users:
sudo chmod -R a+rX /usr/local/lib/python2.7/dist-packages/mechanize*
3. Safer: Reinstall without sudo
A better long-term fix is to install packages without root privileges:
# Install in your user’s home directory
pip install --user mechanize
This places the module in ~/.local/lib/pythonX.Y/site-packages/
, where your user has full access.
Other Possible Causes
If permissions aren’t the issue, check:
- Wrong Python version
- Installed with
pip3
but runningpython2
, or vice versa.
- Installed with
- Multiple environments
- Virtualenv, Conda, or system Python confusion.
- Path issues
- Module installed in a path not included in
sys.path
.
- Module installed in a path not included in
- Unsupported library versions
- Some packages (like
mechanize
) only support Python 2.7, not Python 3.
- Some packages (like
Conclusion
The most common cause of ImportError: No module named …
after a “successful” install is a permission mismatch: the module is installed, but your user can’t access it because it was written as root.
👉 Fix it by correcting permissions or reinstalling with pip install --user
. If that doesn’t work, double-check your Python version, environments, and sys.path
.
Once permissions are corrected, your import mechanize
(or any other library) will work as expected.