{"id":3765,"date":"2024-10-03T14:05:04","date_gmt":"2024-10-03T13:05:04","guid":{"rendered":"https:\/\/raspberry-projects.com\/pi\/?p=3765"},"modified":"2025-05-23T11:07:00","modified_gmt":"2025-05-23T10:07:00","slug":"virtual-environment-for-python-programs","status":"publish","type":"post","link":"https:\/\/raspberry-projects.com\/pi\/programming-in-python\/virtual-environment\/virtual-environment-for-python-programs","title":{"rendered":"Creating a virtual environment for python programs"},"content":{"rendered":"\n<p>Raspberry Pi OS manages Python packages a bit differently to some Linux installs and will insist on using a virtual environment (e.g. you get an error: &#8220;error: externally-managed-environment&#8221;) <\/p>\n\n\n\n<p>Creating a virtual environment is good practice anyway for Python development in general, as it helps to manage dependencies and avoid conflicts.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Update system and ensure virtualenv is installed<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo apt-get update &amp;&amp; sudo apt-get upgrade\nsudo apt-get install virtualenv<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Create a new virtual environment<\/h4>\n\n\n\n<h5 class=\"wp-block-heading\">Virtual environment name<\/h5>\n\n\n\n<p>&#8220;venv&#8221; is often used, but it doesn&#8217;t matter, it can be anything<\/p>\n\n\n\n<h5 class=\"wp-block-heading\">Navigate to where you want it to be stored<\/h5>\n\n\n\n<p>Navigate to the directory within which you want the virtual environment to be created. For example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cd \/home\/myfolder<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">Create the virtual environment <\/h5>\n\n\n\n<p>NOTE: The virtual environment will be created using whatever version of Python you create it using.<br>If you just use &#8220;python3&#8221;, it will be the system python version.<br>If you have other versions installed you can instead specify the path to them (e.g. &#8220;\/usr\/bin\/python3.8&#8221;) so that version version of Python is used instead (useful if your system OS python version is old and you have installed a newer version you want to use for the new virtual environment)<\/p>\n\n\n\n<p>Create a virtual environment called &#8220;venv&#8221;:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>python3 -m virtualenv venv --system-site-packages<\/code><\/pre>\n\n\n\n<p>The &#8220;&#8211;system-site-packages&#8221; is an optional argument which means that the virtual environment will also have access to the system site-packages directory (see <a href=\"https:\/\/docs.python.org\/3\/library\/venv.html\" target=\"_blank\" rel=\"noreferrer noopener\">here<\/a>).<\/p>\n\n\n\n<p>The venv\/ folder will be created and all of the requried python files added into it.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Create an app\/ folder<\/h4>\n\n\n\n<p>You don&#8217;t have to, but its a good idea to create a directory within it for your app\/apps to go in:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>mkdir venv\/app\nchmod 0755 venv\/app<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h4 class=\"wp-block-heading\">Using the virtual environment, method 1 &#8211; Via the command line and in scripts<\/h4>\n\n\n\n<h5 class=\"wp-block-heading\">Activate the virtual environment so you can use it (needs to be done each time the venv is used)<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>source venv\/bin\/activate<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">Use the virtual environment (run your Python code here)<\/h5>\n\n\n\n<p>Once the virtual environment has been activated Python is used as normal. Running python or pip will be done in the context of the virtual environment. Modules installed with pip will be placed in the local venv folders (<strong><em>sudo should not be used<\/em><\/strong>).<\/p>\n\n\n\n<h5 class=\"wp-block-heading\">Install a package wihin it using pip<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>\/home\/myfolder\/venv\/bin\/pip3 install mypackagename<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">Run your app<\/h5>\n\n\n\n<p>You can copy your app files into the virtual environment if you want, for instance into a folder called \\app.<\/p>\n\n\n\n<p>Running it from the command line after activating the virtual environment:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>python3 venv\/app\/myappname.py<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Deactivate the virtual environment (optional)<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>deactivate<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h4 class=\"wp-block-heading\">Using the virtual environment, method 2 &#8211; from exec commands etc<\/h4>\n\n\n\n<h5 class=\"wp-block-heading\">Create the virtual environment <\/h5>\n\n\n\n<p>(Same as before, but you can do this from exec commands etc too if you specify full paths):<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/usr\/bin\/python3 -m virtualenv \/home\/myfolder\/venv --system-site-packages\nmkdir \/home\/myfolder\/venv\/app\nchmod 0755 \/home\/myfolder\/venv\/app<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">Activate the virtual environment so you can use it<\/h5>\n\n\n\n<p>You don&#8217;t need to, instead you&#8217;ll specify the path to the virtual environments python binary<\/p>\n\n\n\n<h5 class=\"wp-block-heading\">Install a package wihin it using pip<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>\/home\/myfolder\/venv\/bin\/pip3 install mypackagename<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">Run your app<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>\/home\/myfolder\/venv\/bin\/python3 \/home\/myfolder\/venv\/app\/myappname.py<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h4 class=\"wp-block-heading\">Delete a virtual environment<\/h4>\n\n\n\n<p>As long as nothing is running within it, just delete the venv\/ folder.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">More info on virtual environments<\/h4>\n\n\n\n<p><a href=\"https:\/\/learn.adafruit.com\/python-virtual-environment-usage-on-raspberry-pi\/automatically-running-at-boot\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/learn.adafruit.com\/python-virtual-environment-usage-on-raspberry-pi\/automatically-running-at-boot<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Raspberry Pi OS manages Python packages a bit differently to some Linux installs and will insist on using a virtual environment (e.g. you get an error: &#8220;error: externally-managed-environment&#8221;) Creating a virtual environment is good practice anyway for Python development in general, as it helps to manage dependencies and avoid conflicts. Update system and ensure virtualenv [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[192],"tags":[],"class_list":["post-3765","post","type-post","status-publish","format-standard","hentry","category-virtual-environment"],"_links":{"self":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/3765","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/comments?post=3765"}],"version-history":[{"count":16,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/3765\/revisions"}],"predecessor-version":[{"id":3982,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/3765\/revisions\/3982"}],"wp:attachment":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/media?parent=3765"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/categories?post=3765"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/tags?post=3765"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}