---
title: "Python bins fail to import library"
date: "2015-02-23"
category: "python"
slug: "python-bins-fail-import-library"
---

## Symptom

A binary installed as part of a Python package (e.g. `django-admin` from [[django-quickstart]]) fails with an import error despite successful installation via pip:

[myadmin@sol www]$ django-admin
Traceback (most recent call last):
 File "/usr/local/bin/django-admin", line 7, in <module>
 from django.core.management import execute\_from\_command\_line
 portError: No module named django.core.management

## Cause

When pip installs a binary helper like `django-admin`, it injects the Python interpreter path used during installation into the file's shebang line. With [[changing-python-versions]] enabled, pip may write the internal pyenv interpreter path (under `/.socket/python/versions`) into the shebang instead of the pyenv shim path. This causes the binary to use the wrong interpreter, which cannot locate the installed libraries.

## Solution

Edit the binary (usually located under `/usr/local/bin`) and replace the first line (shebang) that begins with `#!/.socket/python/` with `#!/usr/bin/env python`.

Confirm the file path using [which](http://apnscp.com/linux-man/man1/which.1.html):

[myadmin@sol www]$ which django-admin
/usr/local/bin/django-admin
[myadmin@sol www]$ nano /usr/local/bin/django-admin
# Now edit the first line of django-admin to correct the shebang

### Why This Works

`/usr/bin/env python` looks up `python` in the current `PATH`, which includes `/.socket/python/shims` before system paths. This ensures pyenv's shim is used, which then resolves the correct Python interpreter based on the `.python-version` file in the current directory.
