{"id":1514,"date":"2019-05-07T19:08:47","date_gmt":"2019-05-07T11:08:47","guid":{"rendered":"https:\/\/www.techcoil.com\/blog\/?p=1514"},"modified":"2020-05-12T10:46:26","modified_gmt":"2020-05-12T02:46:26","slug":"how-to-prepare-a-virtual-environment-to-run-a-gpiozero-based-project-on-raspbian-stretch","status":"publish","type":"post","link":"https:\/\/www.techcoil.com\/blog\/how-to-prepare-a-virtual-environment-to-run-a-gpiozero-based-project-on-raspbian-stretch\/","title":{"rendered":"How to prepare a virtual environment to run a gpiozero based project on Raspbian Stretch"},"content":{"rendered":"<p>When you use a <a href=\"https:\/\/docs.python.org\/3\/library\/venv.html\" rel=\"noopener\" target=\"_blank\">virtual environment<\/a> to run your application, you isolate the dependencies in its own container.<\/p>\n<p>In such a situation, you can ensure that your application can run alongside other applications with incompatible dependencies.<\/p>\n<p>Therefore, I always favour the use of virtual environment for running my Python applications.<\/p>\n<p>With this in mind, this is a story about preparing a virtual environment to run a gpiozero based project on Raspbian Stretch.<\/p>\n<h2>Setting up Raspbian Stretch for Python development<\/h2>\n<p>In case you need it, you can follow the guide on <a href=\"https:\/\/www.techcoil.com\/blog\/how-to-setup-raspbian-stretch-on-raspberry-pi-3-for-developing-python-3-applications\/\" rel=\"noopener\" target=\"_blank\">how to setup Raspbian Stretch on Raspberry Pi 3 for developing Python 3 applications<\/a>.<\/p>\n<p>After you had followed through the guide, you can then use <a href=\"https:\/\/docs.python.org\/3\/library\/venv.html\" rel=\"noopener\" target=\"_blank\">python3-venv<\/a> to create your virtual environment.<\/p>\n<h2>Creating a sample project that is based on the GPIO Zero library<\/h2>\n<p>Over at the <a href=\"https:\/\/gpiozero.readthedocs.io\" rel=\"noopener\" target=\"_blank\">GPIO Zero reference site<\/a>, we can get a sample script to turn an LED on and off with a push button:<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nfrom gpiozero import LED, Button\r\nfrom signal import pause\r\n\r\nled = LED(17)\r\nbutton = Button(3)\r\n\r\nbutton.when_pressed = led.on\r\nbutton.when_released = led.off\r\n\r\npause()\r\n<\/pre>\n<p>Let's save the file as <code>on_off_led.py<\/code> in the home directory of the <code>pi<\/code> user.<\/p>\n<h2>Creating a virtual environment for running sample application<\/h2>\n<p>After creating your Python script, proceed to create the Python 3 virtual environment.<\/p>\n<p>In order to do so, open up a terminal program and type the following command:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\npython3 -m venv my-gpiozero-venv\r\n<\/pre>\n<p>After the command completes, you should find a directory named <code>my-gpiozero-venv<\/code> in the home directory of your current user. You can find the artefacts to activate the Python 3 virtual environment in that directory.<\/p>\n<p>Next, run the following command to activate the virtual environment in the current terminal program:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nsource my-gpiozero-venv\/bin\/activate\r\n<\/pre>\n<p>When the command completes, your current terminal program will be configured to use your virtual environment for activities related to Python 3. Given that, install the Python dependencies for your GPIO Zero project into your virtual environment:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\npip install RPi.GPIO\r\npip install gpiozero\r\n<\/pre>\n<p>After you had installed the Python dependencies, you will be able to run your Python script when your virtual environment is activated:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\npython on_off_led.py\r\n<\/pre>\n<h3>Creating a script to start your Python script with your virtual environment<\/h3>\n<p>When you want to make it easy to start your Python script with your virtual environment, you can create a bash script:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n#!\/bin\/bash\r\nsource my-gpiozero-venv\/bin\/activate\r\npython on_off_led.py\r\ndeactivate\r\n<\/pre>\n<p>Suppose you name your bash script as <code>run_app.sh<\/code>, run the following command to make it executable:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nchmod +x run_app.sh\r\n<\/pre>\n<p>After you had done so, you can then run your Python script with the following command:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n.\/run_app.sh\r\n<\/pre>\n<p>When you terminate your Python script, the <code>deactivate<\/code> command will deactivate the virtual environment in your terminal program.<\/p>\n\n      <ul id=\"social-sharing-buttons-list\">\n        <li class=\"facebook\">\n          <a href=\"https:\/\/www.facebook.com\/sharer\/sharer.php?u=https%3A%2F%2Fwp.me%2Fp245TQ-oq\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n            <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Facebook.png\" alt=\"Facebook icon\"> Share\n          <\/a>\n        <\/li>\n        <li class=\"twitter\">\n          <a href=\"https:\/\/twitter.com\/intent\/tweet?text=&url=https%3A%2F%2Fwp.me%2Fp245TQ-oq&via=Techcoil_com\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Twitter.png\" alt=\"Twitter icon\"> Tweet\n          <\/a>\n        <\/li>\n        <li class=\"linkedin\">\n          <a href=\"https:\/\/www.linkedin.com\/shareArticle?mini=1&title=&url=https%3A%2F%2Fwp.me%2Fp245TQ-oq&source=https:\/\/www.techcoil.com\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/linkedin.png\" alt=\"Linkedin icon\"> Share\n          <\/a>\n        <\/li>\n        <li class=\"pinterest\">\n          <a href=\"https:\/\/pinterest.com\/pin\/create\/button\/?url=https%3A%2F%2Fwww.techcoil.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F1514&description=\" class=\"pin-it-button\" target=\"_blank\" role=\"button\" rel=\"nofollow\" count-layout=\"horizontal\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Pinterest.png\" alt=\"Pinterest icon\"> Save\n          <\/a>\n        <\/li>\n      <\/ul>\n    ","protected":false},"excerpt":{"rendered":"<p>When you use a <a href=\"https:\/\/docs.python.org\/3\/library\/venv.html\" rel=\"noopener\" target=\"_blank\">virtual environment<\/a> to run your application, you isolate the dependencies in its own container.<\/p>\n<p>In such a situation, you can ensure that your application can run alongside other applications with incompatible dependencies.<\/p>\n<p>Therefore, I always favour the use of virtual environment for running my Python applications.<\/p>\n<p>With this in mind, this is a story about preparing a virtual environment to run a gpiozero based project on Raspbian Stretch.<\/p>\n","protected":false},"author":1,"featured_media":1144,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"footnotes":""},"categories":[4],"tags":[637,226,233,240,412,557,638,195,438],"jetpack_featured_media_url":"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/GPIO-pins-of-a-Raspberry-Pi-3-B.jpg","jetpack_shortlink":"https:\/\/wp.me\/p245TQ-oq","jetpack-related-posts":[],"jetpack_likes_enabled":true,"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts\/1514"}],"collection":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/comments?post=1514"}],"version-history":[{"count":0,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts\/1514\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/media\/1144"}],"wp:attachment":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/media?parent=1514"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/categories?post=1514"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/tags?post=1514"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}