{"id":3827,"date":"2023-07-03T17:03:37","date_gmt":"2023-07-03T17:03:37","guid":{"rendered":"https:\/\/geekmungus.co.uk\/?p=3827"},"modified":"2023-07-03T17:04:13","modified_gmt":"2023-07-03T17:04:13","slug":"python-virtual-environment","status":"publish","type":"post","link":"https:\/\/geekmungus.co.uk\/?p=3827","title":{"rendered":"Python Virtual Environment"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">A very quick overview of how you can use Python virtual environments.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/packaging.python.org\/en\/latest\/guides\/installing-using-pip-and-virtual-environments\/\">https:\/\/packaging.python.org\/en\/latest\/guides\/installing-using-pip-and-virtual-environments\/<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Install the Virtual Environment<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">First install the virtual environment.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>pip install virtualenv\nsudo apt install python3.10-venv<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Create the Virtual Environment<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Let&#8217;s say I have a project called &#8220;python-static-site&#8221;, so we&#8217;ll create the virtual environment within this project directory along with all the source files.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cd ~\/python-static-site\/\npython3 -m venv env<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The second argument is the location to create the virtual environment. Generally, you can just create this in your project and call it env.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">venv will create a virtual Python installation in the &#8220;env&#8221; folder.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><em>NOTE: You should exclude your virtual environment directory from your version control system using .gitignore or similar.<\/em><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Activating a Virtual Environment<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">To use your virtual environment you need to activate it.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cd ~\/python-static-site\/\nsource env\/bin\/activate\nwhich python<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This should be showing &#8220;python&#8221; installed within your virtual environment, rather than somewhere centrally on the machine, as long as you are in this virtual environment any specific environment changes will only affect this virtual environment.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">When you need to deactivate your virtual environment just run:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>deactivate<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>A very quick overview of how you can use Python virtual environments. https:\/\/packaging.python.org\/en\/latest\/guides\/installing-using-pip-and-virtual-environments\/ Install the Virtual Environment First install the virtual environment. Create the Virtual Environment Let&#8217;s say I have a project called &#8220;python-static-site&#8221;, so we&#8217;ll create the virtual environment within this project directory along with all the source files. The second argument is the &#8230; <a title=\"Python Virtual Environment\" class=\"read-more\" href=\"https:\/\/geekmungus.co.uk\/?p=3827\" aria-label=\"Read more about Python Virtual Environment\">Read more<\/a><\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[42,44],"tags":[],"class_list":["post-3827","post","type-post","status-publish","format-standard","hentry","category-devops","category-python"],"_links":{"self":[{"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/3827","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3827"}],"version-history":[{"count":2,"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/3827\/revisions"}],"predecessor-version":[{"id":3829,"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/3827\/revisions\/3829"}],"wp:attachment":[{"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3827"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3827"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3827"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}