{"id":1409,"date":"2018-12-15T18:20:44","date_gmt":"2018-12-15T10:20:44","guid":{"rendered":"https:\/\/www.techcoil.com\/blog\/?p=1409"},"modified":"2019-01-02T15:11:30","modified_gmt":"2019-01-02T07:11:30","slug":"how-to-associate-a-python-3-virtual-environment-with-thonny","status":"publish","type":"post","link":"https:\/\/www.techcoil.com\/blog\/how-to-associate-a-python-3-virtual-environment-with-thonny\/","title":{"rendered":"How to associate a Python 3 virtual environment with Thonny"},"content":{"rendered":"<p>When you are developing a Python 3 application with <a href=\"https:\/\/thonny.org\/\" rel=\"noopener\" target=\"_blank\">Thonny<\/a>, you may want to get Thonny to use a virtual environment to run and debug your application. <\/p>\n<p>With this in mind, this post shows you how to associate a Python 3 virtual environment with Thonny.<\/p>\n<h2>Creating a virtual environment with python3-venv<\/h2>\n<p>This tutorial was created with Thonny running on Raspbian Stretch. The steps to associate a Python 3 virtual environment with Thonny running on Windows should be similar.<\/p>\n<p>Before proceeding further, make sure that you had created a virtual environment. In case you need it, you can follow one of the following tutorials to create your Python 3 virtual environment:<\/p>\n<ul>\n<li><a href=\"https:\/\/www.techcoil.com\/blog\/how-to-create-a-virtual-environment-for-your-python-3-application-with-python3-venv-in-linux-or-unix\/\" rel=\"noopener\" target=\"_blank\">Create a virtual environment with python3-venv on Linux or Unix<\/a>. <\/li>\n<li><a href=\"https:\/\/www.techcoil.com\/blog\/how-to-create-a-python-3-virtual-environment-in-windows-10\/\">Create a Python 3 virtual environment on Windows 10<\/a><\/li>\n<\/ul>\n<p>For the purpose of this post, let's assume that we had created a Python 3 virtual environment at <strong>\/home\/pi\/my-venv<\/strong>.<\/p>\n<h2>Steps to associate a Python 3 virtual environment with Thonny<\/h2>\n<p>Firstly, open up your Thonny IDE. <\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/Thonny-IDE-opened-for-the-first-time-on-Raspbian-Stretch-20181113.jpg\" alt=\"Thonny IDE opened for the first time on Raspbian Stretch 20181113\" \/><\/p>\n<p>If you see that your Thonny IDE does not have the menu bar, click on <strong>Switch to regular mode<\/strong> on the top right corner. After you had done so, Thonny will show you a confirmation message:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/Thonny-on-Raspbian-Stretch-20181113-showing-confirmation-message-after-switching-to-regular-mode.jpg\" alt=\"Thonny on Raspbian Stretch 20181113 showing confirmation message after switching to regular mode\" \/><\/p>\n<p>Click on <strong>Ok<\/strong> and reopen Thonny. After Thonny had been reopened, it will show a menu bar at the top:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/Thonny-on-Raspbian-Stretch-20181113-with-menu-bar.jpg\" alt=\"Thonny on Raspbian Stretch 20181113 with menu bar\" \/><\/p>\n<p>Given that, go to <strong>Tools<\/strong> -> <strong>Options<\/strong>:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/Thonny-on-Raspbian-Stretch-20181113-showing-first-tab-of-Options-.jpg\" alt=\"Thonny on Raspbian Stretch 20181113 showing first tab of Options\" \/><\/p>\n<p>Click on the Interpreter tab:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/Thonny-on-Raspbian-Stretch-20181113-Interpreter-tab-showing-configuration-as-using-the-same-Python-interpreter-which-runs-Thonny.jpg\" alt=\"Thonny on Raspbian Stretch 20181113 Interpreter tab showing configuration as using the same Python interpreter which runs Thonny\" \/><\/p>\n<p>As shown above, Thonny is using the same Python interpreter which runs Thonny to run your Python 3 application. Click on the combo box and choose <strong>Alternative Python 3 interpreter or virtual environment<\/strong>:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/Thonny-on-Raspbian-Stretch-20181113-Interpreter-tab-showing-configuration-screen-for-Alternative-Python-3-interpreter-or-virtual-environment.jpg\" alt=\"Thonny on Raspbian Stretch 20181113 Interpreter tab showing configuration screen for Alternative Python 3 interpreter or virtual environment\" \/><\/p>\n<p>Click on <strong>Locate another python executable...<\/strong> and a file dialog will appear for you to choose the python executable inside your virtual environment:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/Thonny-on-Raspbian-Stretch-20181113-showing-Open-file-dialog.jpg\" alt=\"Thonny on Raspbian Stretch 20181113 showing Open file dialog\" \/><\/p>\n<p>Click <strong>Ok<\/strong> to confirm your selection:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/Thonny-on-Raspbian-Stretch-20181113-showing-that-my-venv-python-executable-being-used-as-the-Interpreter-for-running-Python-3-application.jpg\" alt=\"Thonny on Raspbian Stretch 20181113 showing that my-venv python executable being used as the Interpreter for running Python 3 application\" \/><\/p>\n<p>Click <strong>Ok<\/strong> again to confirm your configuration changes. Once you had done so, you will see that Thonny restarts the shell below with the Python executable from your virtual environment:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/Thonny-on-Raspbian-Stretch-20181113-showing-that-it-had-restarted-its-shell-to-use-the-Python-executable-in-my-venv-virtual-environment.jpg\" alt=\"Thonny on Raspbian Stretch 20181113 showing that it had restarted its shell to use the Python executable in my-venv virtual environment\" \/><\/p>\n<p>Given that, Thonny will run your Python 3 application inside your virtual environment. <\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/How-to-associate-a-Python-3-virtual-environment-with-Thonny.jpg\" alt=\"How to associate a Python 3 virtual environment with Thonny\" \/><\/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-mJ\" 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-mJ&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-mJ&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%2F1409&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 are developing a Python 3 application with <a href=\"https:\/\/thonny.org\/\" rel=\"noopener\" target=\"_blank\">Thonny<\/a>, you may want to get Thonny to use a virtual environment to run and debug your application. <\/p>\n<p>With this in mind, this post shows you how to associate a Python 3 virtual environment with Thonny.<\/p>\n","protected":false},"author":1,"featured_media":1419,"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":[226,233,586,195,599],"jetpack_featured_media_url":"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/Thonny-on-Raspbian-Stretch-20181113-showing-that-it-had-restarted-its-shell-to-use-the-Python-executable-in-my-venv-virtual-environment.jpg","jetpack_shortlink":"https:\/\/wp.me\/p245TQ-mJ","jetpack-related-posts":[],"jetpack_likes_enabled":true,"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts\/1409"}],"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=1409"}],"version-history":[{"count":0,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts\/1409\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/media\/1419"}],"wp:attachment":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/media?parent=1409"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/categories?post=1409"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/tags?post=1409"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}