{"id":8403,"date":"2018-09-23T10:58:32","date_gmt":"2018-09-23T10:58:32","guid":{"rendered":"http:\/\/blog.bachi.net\/?p=8403"},"modified":"2020-05-19T13:10:10","modified_gmt":"2020-05-19T13:10:10","slug":"python-virtual-environments","status":"publish","type":"post","link":"https:\/\/blog.bachi.net\/?p=8403","title":{"rendered":"Python Virtual Environments"},"content":{"rendered":"<p><a href=\"https:\/\/realpython.com\/python-virtual-environments-a-primer\/\">Python Virtual Environments: A Primer<\/a><\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\n# Install\r\n$ pip install virtualenv\r\n\r\n# Python 3 using apt install\r\n$ python3 -m venv myapp\r\n\r\n# Python 3 using pip install\r\n$ virtualenv -p python3 myapp\r\n\r\n$ source myapp\/bin\/activate\r\n(env) $\r\n\r\n(env) $ deactivate\r\n$\r\n<\/pre>\n<pre class=\"brush: plain; title: virtualenv not installed; notranslate\" title=\"virtualenv not installed\">\r\n$ python3 -m venv myapp\r\nThe virtual environment was not created successfully because ensurepip is not\r\navailable.  On Debian\/Ubuntu systems, you need to install the python3-venv\r\npackage using the following command.\r\n\r\n    apt-get install python3-venv\r\n\r\nYou may need to use sudo with that command.  After installing the python3-venv\r\npackage, recreate your virtual environment.\r\n\r\nFailing command: &#x5B;'\/home\/XXX\/myapp\/bin\/python3', '-Im', 'ensurepip', '--upgrade', '--default-pip']\r\n<\/pre>\n<pre class=\"brush: plain; collapse: true; light: false; title: pip install virtualenv; toolbar: true; notranslate\" title=\"pip install virtualenv\">\r\n$ pip install virtualenv\r\nDefaulting to user installation because normal site-packages is not writeable\r\nCollecting virtualenv\r\n  Downloading virtualenv-20.0.20-py2.py3-none-any.whl (4.7 MB)\r\nCollecting distlib&lt;1,&gt;=0.3.0\r\n  Downloading distlib-0.3.0.zip (571 kB)\r\nCollecting importlib-metadata&lt;2,&gt;=0.12; python_version &lt; &quot;3.8&quot;\r\n  Downloading importlib_metadata-1.6.0-py2.py3-none-any.whl (30 kB)\r\nCollecting appdirs&lt;2,&gt;=1.4.3\r\n  Downloading appdirs-1.4.4-py2.py3-none-any.whl (9.6 kB)\r\nCollecting filelock&lt;4,&gt;=3.0.0\r\n  Downloading filelock-3.0.12-py3-none-any.whl (7.6 kB)\r\nCollecting importlib-resources&lt;2,&gt;=1.0; python_version &lt; &quot;3.7&quot;\r\n  Downloading importlib_resources-1.5.0-py2.py3-none-any.whl (21 kB)\r\nRequirement already satisfied: six&lt;2,&gt;=1.9.0 in \/usr\/lib\/python3\/dist-packages (from virtualenv) (1.11.0)\r\nCollecting zipp&gt;=0.5\r\n  Downloading zipp-3.1.0-py3-none-any.whl (4.9 kB)\r\nBuilding wheels for collected packages: distlib\r\n  Building wheel for distlib (setup.py) ... done\r\n  Created wheel for distlib: filename=distlib-0.3.0-py3-none-any.whl size=336972 sha256=09fc154d5bab1f792599644aa5c06884f122de197a99cc8545827150f1ca945a\r\n  Stored in directory: \/home\/bacr\/.cache\/pip\/wheels\/33\/d9\/71\/e4e3cac73529e1947df418af0f140cd7589d5d9ec0e17ecfc2\r\nSuccessfully built distlib\r\nInstalling collected packages: distlib, zipp, importlib-metadata, appdirs, filelock, importlib-resources, virtualenv\r\nSuccessfully installed appdirs-1.4.4 distlib-0.3.0 filelock-3.0.12 importlib-metadata-1.6.0 importlib-resources-1.5.0 virtualenv-20.0.20 zipp-3.1.0\r\n<\/pre>\n<pre class=\"brush: plain; title: .local\/bin not in PATH; notranslate\" title=\".local\/bin not in PATH\">\r\nWARNING: The script virtualenv is installed in '\/home\/bacr\/.local\/bin' which is not on PATH.\r\nConsider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.\r\n<\/pre>\n<pre class=\"brush: plain; title: Old pip version; notranslate\" title=\"Old pip version\">\r\nWARNING: You are using pip version 20.0.2; however, version 20.1.1 is available.\r\nYou should consider upgrading via the '\/usr\/bin\/python3 -m pip install --upgrade pip' command.\r\n<\/pre>\n<pre class=\"brush: plain; title: without virtualenv; notranslate\" title=\"without virtualenv\">\r\n$ which python\r\n\/usr\/bin\/python\r\n<\/pre>\n<pre class=\"brush: plain; title: with virtualenv; notranslate\" title=\"with virtualenv\">\r\n(env) $ which python\r\n\/home\/andreas\/my_python_project\/venv\/bin\/python\r\n<\/pre>\n<pre class=\"brush: python; title: without virtualenv; notranslate\" title=\"without virtualenv\">\r\n&gt;&gt;&gt; import sys\r\n&gt;&gt;&gt; sys.prefix\r\n'\/usr'\r\n\r\n&gt;&gt;&gt; import site\r\n&gt;&gt;&gt; site.getsitepackages()\r\n&#x5B;\r\n  '\/usr\/local\/lib\/python3.6\/dist-packages',\r\n  '\/usr\/lib\/python3\/dist-packages',\r\n  '\/usr\/lib\/python3.6\/dist-packages'\r\n]\r\n<\/pre>\n<pre class=\"brush: python; title: with virtualenv; notranslate\" title=\"with virtualenv\">\r\n&gt;&gt;&gt; import sys\r\n&gt;&gt;&gt; sys.prefix\r\n'\/home\/andreas\/my_python_project\/venv'\r\n\r\n&gt;&gt;&gt; import site\r\n&gt;&gt;&gt; site.getsitepackages()\r\n&#x5B;\r\n  '\/home\/andreas\/my_python_project\/venv\/lib\/python3.6\/site-packages',\r\n  '\/home\/andreas\/my_python_project\/venv\/local\/lib\/python3.6\/dist-packages',\r\n  '\/home\/andreas\/my_python_project\/venv\/lib\/python3\/dist-packages',\r\n  '\/home\/andreas\/my_python_project\/venv\/lib\/python3.6\/dist-packages'\r\n]\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Python Virtual Environments: A Primer # Install $ pip install virtualenv # Python 3 using apt install $ python3 -m venv myapp # Python 3 using pip install $ virtualenv -p python3 myapp $ source myapp\/bin\/activate (env) $ (env) $ deactivate $ $ python3 -m venv myapp The virtual environment was not created successfully because [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-8403","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/blog.bachi.net\/index.php?rest_route=\/wp\/v2\/posts\/8403","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.bachi.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.bachi.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.bachi.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.bachi.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=8403"}],"version-history":[{"count":9,"href":"https:\/\/blog.bachi.net\/index.php?rest_route=\/wp\/v2\/posts\/8403\/revisions"}],"predecessor-version":[{"id":11072,"href":"https:\/\/blog.bachi.net\/index.php?rest_route=\/wp\/v2\/posts\/8403\/revisions\/11072"}],"wp:attachment":[{"href":"https:\/\/blog.bachi.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=8403"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.bachi.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=8403"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.bachi.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=8403"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}