{"id":8671,"date":"2015-02-23T17:35:13","date_gmt":"2015-02-23T17:35:13","guid":{"rendered":"https:\/\/wp.okra.host\/article\/python-bins-fail-to-import-library\/"},"modified":"2021-03-07T14:44:11","modified_gmt":"2021-03-07T13:44:11","slug":"python-bins-fail-to-import-library","status":"publish","type":"ht_kb","link":"https:\/\/kb.okra.host\/de\/article\/python-bins-fail-to-import-library\/","title":{"rendered":"Python-Bins k\u00f6nnen die Bibliothek nicht importieren"},"content":{"rendered":"<h2 id=\"overview\" >Overview<\/h2>\n<p>A binary (bin) file installed as part of a Python package, e.g.\u00a0<code>django-admin<\/code> from <a title=\"Django quickstart\" href=\"https:\/\/kb.okra.host\/python\/django-quickstart\/\">Django<\/a>\u00a0will fail upon execution &#8211; even after successful installation via <code>pip<\/code> &#8211;\u00a0because it cannot locate its corresponding Python library.<\/p>\n<p><strong>Example:<\/strong><\/p>\n<pre style=\"padding-left: 30px\">[myadmin@sol www]$ django-admin\r\nTraceback (most recent call last):\r\n File \"\/usr\/local\/bin\/django-admin\", line 7, in &lt;module&gt;\r\n from django.core.management import execute_from_command_line\r\n portError: No module named django.core.management<\/pre>\n<h2 id=\"cause\" >Cause<\/h2>\n<p>Bin helpers, like <code>django-admin<\/code>, will bootstrap itself to make it\u00a0an executable shell script by injecting the interpreter used to run pip\u00a0into itself on installation. <a title=\"Changing Python versions\" href=\"https:\/\/kb.okra.host\/python\/changing-python-versions\/\">pyenv<\/a>, which provides support for multiple Python interpreters to coexist on an account, looks for a control file named\u00a0<code>.python-version<\/code> file; then, if found, executes the corresponding Python interpreter. These interpreters reside under <code>\/.socket\/python\/versions<\/code> <em>and<\/em> it is these\u00a0Python interpreters that are accidentally injected into the first line to make an executable shell script.<\/p>\n<h2 id=\"solution\" >Solution<\/h2>\n<p>Edit the file, usually located under <code>\/usr\/local\/bin<\/code>, and replace the first line (shebang) that begins with <code>#!\/.socket\/python\/<\/code> with\u00a0<code>#!\/usr\/bin\/env python<\/code>.<\/p>\n<p>You can confirm the path by using <a href=\"http:\/\/apnscp.com\/linux-man\/man1\/which.1.html\">which(1)<\/a>:<\/p>\n<pre>[myadmin@sol www]$ which django-admin\r\n\/usr\/local\/bin\/django-admin\r\n[myadmin@sol www]$ nano \/usr\/local\/bin\/django-admin\r\n# Now edit the first line of django-admin to correct the shebang<\/pre>\n<p><span style=\"color: #0000ff\"><strong>Explanation:\u00a0<\/strong><span style=\"color: #000000\">the Python interpreter is replaced with pyenv&#8217;s wrapper script, <code>python<\/code> that resides under <code>\/.socket\/python\/shims\/python<\/code>. We use <code>env<\/code>\u00a0as a trick to\u00a0propagate environment variables and execute &#8220;<em>python<\/em>&#8221; under the current <code>PATH<\/code>, which is shimmed with <code>\/.socket\/python\/shims<\/code>\u00a0before <code>\/usr\/bin<\/code> or any other system path.\u00a0<code>python<\/code> is sourced from this directory, because it is the first directory to appear in the\u00a0<code>PATH<\/code>\u00a0environment variable that contains the command, &#8220;<em>python<\/em>&#8221; resulting in running\u00a0pyenv&#8217;s version of <code>python<\/code>.<\/span><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Overview A binary (bin) file installed as part of a Python package, e.g.\u00a0django-admin from Django\u00a0will fail upon execution &#8211; even after successful installation via pip &#8211;\u00a0because it cannot locate its corresponding Python library. Example: [myadmin@sol www]$ django-admin Traceback (most recent call last): File &#8220;\/usr\/local\/bin\/django-admin&#8221;, line 7, in &lt;module&gt; from django.core.management&#8230;<\/p>\n","protected":false},"author":1,"comment_status":"open","ping_status":"closed","template":"","format":"standard","meta":{"footnotes":""},"ht-kb-category":[65],"ht-kb-tag":[],"class_list":["post-8671","ht_kb","type-ht_kb","status-publish","format-standard","hentry","ht_kb_category-python"],"_links":{"self":[{"href":"https:\/\/kb.okra.host\/de\/wp-json\/wp\/v2\/ht-kb\/8671","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kb.okra.host\/de\/wp-json\/wp\/v2\/ht-kb"}],"about":[{"href":"https:\/\/kb.okra.host\/de\/wp-json\/wp\/v2\/types\/ht_kb"}],"author":[{"embeddable":true,"href":"https:\/\/kb.okra.host\/de\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kb.okra.host\/de\/wp-json\/wp\/v2\/comments?post=8671"}],"version-history":[{"count":1,"href":"https:\/\/kb.okra.host\/de\/wp-json\/wp\/v2\/ht-kb\/8671\/revisions"}],"predecessor-version":[{"id":8672,"href":"https:\/\/kb.okra.host\/de\/wp-json\/wp\/v2\/ht-kb\/8671\/revisions\/8672"}],"wp:attachment":[{"href":"https:\/\/kb.okra.host\/de\/wp-json\/wp\/v2\/media?parent=8671"}],"wp:term":[{"taxonomy":"ht_kb_category","embeddable":true,"href":"https:\/\/kb.okra.host\/de\/wp-json\/wp\/v2\/ht-kb-category?post=8671"},{"taxonomy":"ht_kb_tag","embeddable":true,"href":"https:\/\/kb.okra.host\/de\/wp-json\/wp\/v2\/ht-kb-tag?post=8671"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}