{"id":1374,"date":"2018-11-15T18:18:39","date_gmt":"2018-11-15T10:18:39","guid":{"rendered":"https:\/\/www.techcoil.com\/blog\/?p=1374"},"modified":"2021-01-10T13:07:38","modified_gmt":"2021-01-10T05:07:38","slug":"how-to-use-a-python-3-virtual-environment-in-windows-10","status":"publish","type":"post","link":"https:\/\/www.techcoil.com\/blog\/how-to-use-a-python-3-virtual-environment-in-windows-10\/","title":{"rendered":"How to use a Python 3 virtual environment in Windows 10"},"content":{"rendered":"<p>When you need each Python 3 application that you are building to run in its own isolated environment, you can turn to <a href=\"https:\/\/docs.python.org\/3\/tutorial\/venv.html\" rel=\"noopener\" target=\"_blank\">virtual environments<\/a>.  <\/p>\n<p>Since Python is available on Windows 10, you can also use virtual environments on Windows 10.<\/p>\n<p>Typically, using a Python 3 virtual environment in Windows 10 involves the following steps:<\/p>\n<ol>\n<li>Installing Python 3 with pip and several features.<\/li>\n<li>Creating a Python 3 virtual environment with Python 3 venv module.<\/li>\n<li>Activating the Python 3 virtual environment.<\/li>\n<li>Installing Python 3 packages that your Python application is using into the virtual environment.<\/li>\n<li>Running your Python 3 application within the Python 3 virtual environment.<\/li>\n<li>Deactivating the Python 3 virtual environment.<\/li>\n<\/ol>\n<h2>1. Installing Python 3 with pip and several features on Windows 10<\/h2>\n<p>When you head over to the <a href=\"https:\/\/www.python.org\/downloads\/windows\/\" rel=\"noopener\" target=\"_blank\">Python 3 download page for windows<\/a>, you will find several options:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/Python-3-download-page-for-Windows-as-of-20181115.jpg\" alt=\"Python 3 download page for Windows as of 20181115\" class=\"aligncenter\"\/><\/p>\n<p>Next, <a href=\"https:\/\/www.techcoil.com\/blog\/how-to-determine-if-your-windows-10-iot-operating-system-is-32-bit-or-64-bit\/\" rel=\"noopener\" target=\"_blank\">determine whether your Windows 10 operating is 32 bit or 64 bit<\/a>. When you have a 32 bit operating system, download the latest executable installer with <strong>x86<\/strong>. However, if you have a 64 bit operating system, download the one with <strong>x86-64<\/strong>. <\/p>\n<p>For example, if I have Windows 10 64 bit, I can <a href=\"https:\/\/www.python.org\/ftp\/python\/3.7.1\/python-3.7.1-amd64.exe\" rel=\"noopener\" target=\"_blank\">download Windows x86-64 executable installer for Python 3.7.1 runtime<\/a>.<\/p>\n<p>After you had downloaded the Python 3 installer, double-click on it. Choose to Add Python 3.7 to PATH:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/Python-3.7.1-64-bit-Windows-Setup-first-page.jpg\" alt=\"Python 3.7.1 (64-bit) Windows Setup first page\" class=\"aligncenter\"\/> <\/p>\n<p>Left-click on <strong>Customize installation<\/strong>. After the next screen appears, check <strong>pip<\/strong>:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/Python-3.7.1-64-bit-Windows-setup-Optional-Features-screen.jpg\" alt=\"Python 3.7.1 (64-bit) Windows setup Optional Features screen\" class=\"aligncenter\"\/><\/p>\n<p>Left-click on <strong>Next<\/strong> and the Advanced Options screen appears:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/Python-3.7.1-64-bit-Windows-setup-Advanced-Options-screen.jpg\" alt=\"Python 3.7.1 (64-bit) Windows setup Advanced Options screen\" class=\"aligncenter\"><\/p>\n<p>Finally, left-click on <strong>Install<\/strong> to start the installation progress:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/Python-3.7.1-64-bit-Windows-setup-progress.jpg\" alt=\"Python 3.7.1 (64-bit) Windows setup progress\" class=\"aligncenter\"\/><\/p>\n<p>When the installation had completed, you will see the following screen:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/Python-3.7.1-64-bit-Windows-setup-successful-screen.jpg\" alt=\"Python 3.7.1 (64-bit) Windows setup successful screen\" class=\"aligncenter\"\/><\/p>\n<p>Left-click on <strong>Close<\/strong> to exit the installation wizard.<\/p>\n<h2>2. Creating a Python 3 virtual environment with Python 3 venv module on Windows 10<\/h2>\n<p>When you had installed Python 3 on Windows 10, you can then create the virtual environment for your Python 3 application. In order to do so, open up a command prompt window and type the following command:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\npython -m venv %systemdrive%%homepath%\\my-venv\r\n<\/pre>\n<p>After the command completes, you will find the <code>my-venv<\/code> directory inside your home directory. Inside the <code>my-venv<\/code>, you will find the Python artefacts to work with your virtual environment.<\/p>\n<h2>3. Activating your Python 3 virtual environment on Windows 10<\/h2>\n<p>Before you can run your Python 3 application inside of your Python 3 virtual environment, you will need to activate it. In order to activate your virtual environment, you will need to run the <code>activate.bat<\/code> script located inside your virtual environment directory. <\/p>\n<p>For example, to activate the virtual environment inside <code>my-venv<\/code>, you can run the following command in your command prompt window:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n%systemdrive%%homepath%\\my-venv\\Scripts\\activate.bat\r\n<\/pre>\n<p>After the <code>activate.bat<\/code> script had ran, you will see the prompt appended with <strong>(my-venv)<\/strong>:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/Windows-10-command-shell-with-a-Python-3.7.1-virtual-environment-activated.jpg\" alt=\"Windows 10 command shell with a Python 3.7.1 virtual environment activated\" class=\"aligncenter\"\/><\/p>\n<p>This tells us that the command prompt session is inside the Python 3 virtual environment.<\/p>\n<h2>4. Installing Python 3 packages that your Python application is using into the virtual environment.<\/h2>\n<p>When you had activated your virtual environment, you can then install your Python 3 dependencies into your Python 3 virtual environment on Windows 10. For example, you can install the <a href=\"http:\/\/docs.python-requests.org\/en\/master\/\" rel=\"noopener\" target=\"_blank\">requests library<\/a> for your Python 3 application to <a href=\"https:\/\/www.techcoil.com\/blog\/how-to-download-a-file-via-http-post-and-http-get-with-python-3-requests-library\/\" rel=\"noopener\" target=\"_blank\">download a file from a HTTP server<\/a> or <a href=\"https:\/\/www.techcoil.com\/blog\/how-to-upload-a-file-and-some-data-through-http-multipart-in-python-3-using-the-requests-library\/\" rel=\"noopener\" target=\"_blank\">upload a file to a HTTP server<\/a>:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\npip install requests\r\n<\/pre>\n<p>In order to use the pip command, you need to ensure that you had installed it during your Python 3 installation.<\/p>\n<h2>5. Running your Python 3 application within the Python 3 virtual environment<\/h2>\n<p>Subsequently, when you had installed all the needed dependencies, you can then run your Python 3 application with the <strong>python<\/strong> binary:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\npython a_python_application.py\r\n<\/pre>\n<h2>6. Deactivating the Python 3 virtual environment on Windows 10<\/h2>\n<p>When you want to get out of your Python 3 virtual environment on Windows 10, you can simply run the following command:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\ndeactivate\r\n<\/pre>\n<p>After the virtual environment is deactivated, your command prompt will switch to the global Python 3 environment. In addition, those Python 3 dependencies that you had installed in your 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-ma\" 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-ma&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-ma&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%2F1374&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 need each Python 3 application that you are building to run in its own isolated environment, you can turn to <a href=\"https:\/\/docs.python.org\/3\/tutorial\/venv.html\" rel=\"noopener\" target=\"_blank\">virtual environments<\/a>.  <\/p>\n<p>Since Python is available on Windows 10, you can also use virtual environments on Windows 10.<\/p>\n<p>Typically, using a Python 3 virtual environment in Windows 10 involves the following steps:<\/p>\n<ol>\n<li>Installing Python 3 with pip and several features.<\/li>\n<li>Creating a Python 3 virtual environment with Python 3 venv module.<\/li>\n<li>Activating the Python 3 virtual environment.<\/li>\n<li>Installing Python 3 packages that your Python application is using into the virtual environment.<\/li>\n<li>Running your Python 3 application within the Python 3 virtual environment.<\/li>\n<li>Deactivating the Python 3 virtual environment.<\/li>\n<\/ol>\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":[226,233,586,255,195,438,181,347],"jetpack_featured_media_url":"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/Python-Logo.gif","jetpack_shortlink":"https:\/\/wp.me\/p245TQ-ma","jetpack-related-posts":[],"jetpack_likes_enabled":true,"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts\/1374"}],"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=1374"}],"version-history":[{"count":0,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts\/1374\/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=1374"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/categories?post=1374"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/tags?post=1374"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}