1. Home
  2. Python
  3. Python bins fail to import library

Python bins fail to import library


A binary (bin) file installed as part of a Python package, e.g. django-admin from Django will fail upon execution – even after successful installation via pip – because it cannot locate its corresponding Python library.


[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


Bin helpers, like django-admin, will bootstrap itself to make it an executable shell script by injecting the interpreter used to run pip into itself on installation. pyenv, which provides support for multiple Python interpreters to coexist on an account, looks for a control file named .python-version file; then, if found, executes the corresponding Python interpreter. These interpreters reside under /.socket/python/versions and it is these Python interpreters that are accidentally injected into the first line to make an executable shell script.


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

You can confirm the path by using which(1):

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

Explanation: the Python interpreter is replaced with pyenv’s wrapper script, python that resides under /.socket/python/shims/python. We use env as a trick to propagate environment variables and execute “python” under the current PATH, which is shimmed with /.socket/python/shims before /usr/bin or any other system path. python is sourced from this directory, because it is the first directory to appear in the PATH environment variable that contains the command, “python” resulting in running pyenv’s version of python.

Updated on July 8, 2019

Was this article helpful?

Related Articles

Need Support?
Can’t find the answer you’re looking for? Don’t worry we’re here to help! If you get an error, visit https://lithiumhosting.com/support instead.
Contact Support