{"id":1408,"date":"2018-12-15T12:22:09","date_gmt":"2018-12-15T04:22:09","guid":{"rendered":"https:\/\/www.techcoil.com\/blog\/?p=1408"},"modified":"2018-12-15T12:22:09","modified_gmt":"2018-12-15T04:22:09","slug":"how-to-create-a-virtual-environment-for-your-python-3-application-with-python3-venv-in-linux-or-unix","status":"publish","type":"post","link":"https:\/\/www.techcoil.com\/blog\/how-to-create-a-virtual-environment-for-your-python-3-application-with-python3-venv-in-linux-or-unix\/","title":{"rendered":"How to create a virtual environment for your Python 3 application with python3-venv in Linux or Unix"},"content":{"rendered":"<p>The <a href=\"https:\/\/docs.python.org\/3\/library\/venv.html\" rel=\"noopener\" target=\"_blank\">python3-venv<\/a> module allows us to create lightweight \u201cvirtual environments\u201d with their own site directories, optionally isolated from system site directories. Given that, we will be able to run multiple Python 3 environments with varying dependencies on the same computer. <\/p>\n<p>This post shows how to create a virtual environment for your Python 3 application with python3-venv in Linux or Unix.<\/p>\n<p>When you want to create a virtual environment for your Python 3 application inside <code>~\/my_py3_venv<\/code>, you can run the following command in your terminal shell:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\npython3 -m venv ~\/my_py3_venv\r\n<\/pre>\n<p>After the command complete, you should be able to find the virtual environment inside the ~\/my_py3_venv directory.<\/p>\n<h2>Getting into your Python 3 virtual environment with your terminal window<\/h2>\n<p>In order to get into your Python 3 virtual environment with your terminal window, you need to run the <code>activate<\/code> script. Given that, run the following command to get into your Python 3 virtual environment:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nsource ~\/my_py3_venv\/bin\/activate\r\n<\/pre>\n<p>After the command complete, you will find that your terminal prompt is appended with <strong>(my_py3_venv)<\/strong>. While inside this virtual environment, you can install any Python dependencies with the <a href=\"https:\/\/pypi.org\/project\/pip\/\" rel=\"noopener\" target=\"_blank\">pip<\/a> command. For example, if you wish to install <a href=\"http:\/\/flask.pocoo.org\/\" rel=\"noopener\" target=\"_blank\">Flask<\/a>, you can run the following command:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\npip install flask\r\n<\/pre>\n<p>When you had installed the necessary dependencies for your Python 3 application, you can then run your application with the <code>python<\/code> binary:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\npython run_your_app.py\r\n<\/pre>\n<h2>Getting out of your Python 3 virtual environment<\/h2>\n<p>In order to get out of your Python 3 virtual environment, you need to run the following command:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\ndeactivate\r\n<\/pre>\n<p>After the command completes, you will find that your terminal prompt is not appended with <strong>(my_py3_venv)<\/strong>. At this point in time, whatever dependencies that you had added while inside the virtual environment will not be available.<\/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-mI\" 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-mI&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-mI&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%2F1408&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>The <a href=\"https:\/\/docs.python.org\/3\/library\/venv.html\" rel=\"noopener\" target=\"_blank\">python3-venv<\/a> module allows us to create lightweight \u201cvirtual environments\u201d with their own site directories, optionally isolated from system site directories. Given that, we will be able to run multiple Python 3 environments with varying dependencies on the same computer. <\/p>\n<p>This post shows how to create a virtual environment for your Python 3 application with python3-venv in Linux or Unix.<\/p>\n","protected":false},"author":1,"featured_media":1244,"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":[598,226,233,586,195,438],"jetpack_featured_media_url":"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/Python-Logo.gif","jetpack_shortlink":"https:\/\/wp.me\/p245TQ-mI","jetpack-related-posts":[],"jetpack_likes_enabled":true,"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts\/1408"}],"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=1408"}],"version-history":[{"count":0,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts\/1408\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/media\/1244"}],"wp:attachment":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/media?parent=1408"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/categories?post=1408"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/tags?post=1408"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}