{"id":82,"date":"2025-10-21T04:37:27","date_gmt":"2025-10-21T04:37:27","guid":{"rendered":"https:\/\/codetypingpro.com\/?p=82"},"modified":"2025-12-17T07:48:31","modified_gmt":"2025-12-17T07:48:31","slug":"lesson-19-virtual-environments-in-python","status":"publish","type":"post","link":"https:\/\/codetypingpro.com\/?p=82","title":{"rendered":"Lesson 19: Virtual Environments in Python"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\">\ud83c\udfaf <strong>Lesson Objective<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">To understand how to create and manage <strong>isolated Python environments<\/strong> using virtual environments, keeping project dependencies organized and avoiding conflicts.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83e\udde9 <strong>1. What Is a Virtual Environment?<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A <strong>virtual environment<\/strong> is an isolated workspace that allows you to:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Install project-specific Python packages<\/li>\n\n\n\n<li>Avoid conflicts with other projects<\/li>\n\n\n\n<li>Work on multiple Python versions simultaneously<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">It keeps dependencies <strong>separate from the system Python<\/strong>.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u2699\ufe0f <strong>2. Creating a Virtual Environment<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u2705 <strong>Using <code>venv<\/code> (built-in module)<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Create a virtual environment named 'myenv'\npython -m venv myenv\n<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>This creates a folder <code>myenv\/<\/code> with isolated Python binaries and libraries.<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udd04 <strong>3. Activating the Virtual Environment<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>OS<\/th><th>Command<\/th><\/tr><\/thead><tbody><tr><td>Windows<\/td><td><code>myenv\\Scripts\\activate<\/code><\/td><\/tr><tr><td>macOS\/Linux<\/td><td><code>source myenv\/bin\/activate<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u2705 <strong>Example:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Activate on Windows\nmyenv\\Scripts\\activate\n\n# Activate on Linux\/macOS\nsource myenv\/bin\/activate\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">After activation, your command prompt shows <code>(myenv)<\/code> indicating the environment is active.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83e\uddf0 <strong>4. Installing Packages in a Virtual Environment<\/strong><\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>pip install requests\npip install numpy\n<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Packages installed here <strong>do not affect system Python<\/strong>.<\/li>\n\n\n\n<li>Check installed packages:<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>pip list\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83e\uddf1 <strong>5. Deactivating the Virtual Environment<\/strong><\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>deactivate\n<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Returns to <strong>system Python<\/strong>.<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udd04 <strong>6. Removing a Virtual Environment<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Simply delete the environment folder:<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>rm -rf myenv   # macOS\/Linux\nrmdir \/s myenv # Windows\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udee0\ufe0f <strong>7. Requirements File<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Save installed packages for project sharing:<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>pip freeze &gt; requirements.txt\n<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Install packages from requirements:<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>pip install -r requirements.txt\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83c\udf0d <strong>8. Real-Life Example \u2013 Project Setup<\/strong><\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Create virtual environment for <code>web_scraper_project<\/code><\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>python -m venv web_scraper_env\n<\/code><\/pre>\n\n\n\n<ol start=\"2\" class=\"wp-block-list\">\n<li>Activate environment<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>source web_scraper_env\/bin\/activate\n<\/code><\/pre>\n\n\n\n<ol start=\"3\" class=\"wp-block-list\">\n<li>Install dependencies<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>pip install requests beautifulsoup4\n<\/code><\/pre>\n\n\n\n<ol start=\"4\" class=\"wp-block-list\">\n<li>Run your scraper script<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>python scraper.py\n<\/code><\/pre>\n\n\n\n<ol start=\"5\" class=\"wp-block-list\">\n<li>Deactivate environment after work<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>deactivate<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\ud83c\udfaf Lesson Objective To understand how to create and manage isolated Python environments using virtual environments, keeping project dependencies organized and avoiding conflicts. \ud83e\udde9 1. What Is a Virtual Environment? A virtual environment is an isolated workspace that allows you to: It keeps dependencies separate from the system Python. \u2699\ufe0f 2. Creating a Virtual Environment [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,1],"tags":[],"class_list":["post-82","post","type-post","status-publish","format-standard","hentry","category-python-easy-course-outline","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/codetypingpro.com\/index.php?rest_route=\/wp\/v2\/posts\/82","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codetypingpro.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codetypingpro.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codetypingpro.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codetypingpro.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=82"}],"version-history":[{"count":1,"href":"https:\/\/codetypingpro.com\/index.php?rest_route=\/wp\/v2\/posts\/82\/revisions"}],"predecessor-version":[{"id":83,"href":"https:\/\/codetypingpro.com\/index.php?rest_route=\/wp\/v2\/posts\/82\/revisions\/83"}],"wp:attachment":[{"href":"https:\/\/codetypingpro.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=82"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codetypingpro.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=82"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codetypingpro.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=82"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}