<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Kamal Baiyewu on Medium]]></title>
        <description><![CDATA[Stories by Kamal Baiyewu on Medium]]></description>
        <link>https://medium.com/@kbaiyewu?source=rss-e615a2866324------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*44F0Wnf2EL--xFYO</url>
            <title>Stories by Kamal Baiyewu on Medium</title>
            <link>https://medium.com/@kbaiyewu?source=rss-e615a2866324------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 01 Dec 2026 08:58:58 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@kbaiyewu/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Deep Dive into Python Virtual Environments]]></title>
            <link>https://medium.com/@kbaiyewu/deep-dive-into-python-virtual-environments-33fc63a2f922?source=rss-e615a2866324------2</link>
            <guid isPermaLink="false">https://medium.com/p/33fc63a2f922</guid>
            <category><![CDATA[python-internals]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[python-virtualenv]]></category>
            <dc:creator><![CDATA[Kamal Baiyewu]]></dc:creator>
            <pubDate>Tue, 22 Sep 2026 17:07:38 GMT</pubDate>
            <atom:updated>2026-09-22T17:07:38.963Z</atom:updated>
            <content:encoded><![CDATA[<p>First article ever! Let’s get it started.</p><p>Everyone who uses Python has used virtual environments at least once. We open our project folder, run the commands below depending on our OS, install our project dependencies, and start building.</p><pre># UNIX Based systems<br>python3 -m venv .venv &amp;&amp; source .venv/bin/activate<br><br># Windows<br>python -m venv .venv &amp;&amp; .venv\Scripts\activate</pre><p>But what actually happens behind the scenes when we run the activation script? What do the generated files and folders do, and what do they contain?<br> This article answers all that, but first we need to understand what a virtual environment is.</p><h3>What are Virtual Environments?</h3><p>Imagine we have 2 babies in a house, baby A and baby B, and they both share several toys. One of those toys is the Omnitrix, but there’s only space for one of them in the toy pile.</p><ul><li><strong>Baby A </strong>only likes the Omnitrix from the original Ben 10.</li><li><strong>Baby B </strong>only likes the Omnitrix from Ben 10: Omniverse.</li></ul><p>This means that when either baby wants to play, the mother has to keep replacing the Omnitrix in the pile beforehand to avoid any of the babies crying, but this is tedious and unsustainable. So, is there any other solution?</p><p>Yes! The mother can have two separate toy piles for the two babies so one baby playing doesn’t affect the other, and they can both enjoy their own Omnitrix.</p><p>Python virtual environments work the same way. They isolate project dependencies to avoid conflicts when projects need different versions of the same package. This ensures that several projects can run on the same machine without any errors.<br>In the case above:<br>- <strong>Baby A</strong> and Baby B: 2 different projects.<br>- <strong>Toy pile</strong>: The global Python Installation.<br>- <strong>Separate toy piles</strong>: The 2 separate virtual environments.<br>- <strong>Omnitrix</strong>: A package like Django, FastAPI, requests and so on<br>- <strong>Mother</strong>: You, the developer.</p><h4>Unpacking the .venv Folder</h4><blockquote>I’ll focus mainly on UNIX-based systems, as the process in Windows is a lot more complex and I haven’t explored that route yet.</blockquote><p>If we run python3 -m venv .venv on our system (Ubuntu in my case), we can see that it generates a couple of files and folders here, namely the bin, include, lib, and lib64 folders and a pvenv.cfg file.</p><p>Here is what each component does:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/850/1*nfyBEaSDakfmw6dMBCDwzg.png" /><figcaption>Folder structure of the .venv file</figcaption></figure><ul><li>bin — This folder contains the executables and scripts that your project needs. It also contains the CLI programs like fastapi, pytest, and alembic you use in your projects. For example, let’s say I activate the environment and install the FastAPI package, which also installs a terminal application.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/850/1*CsKHwOXyXo2bPEKnjTv-nQ.png" /><figcaption>activating the virtual environment and installing fastapi</figcaption></figure><p>Let’s now see how the bin folder looks:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/797/1*6NUUxv-i76Vab7mrbuNSoQ.png" /><figcaption>bin folder after installing fastapi</figcaption></figure><p>As we can see, it added the fastapi executable to the bin folder after the installation.</p><p>You’ll notice the bin folder also contains:</p><ul><li><strong>Activation scripts:</strong> Shell scripts (activate, activate.csh, activate.fish) used to activate the environment depending on your current shell.</li><li><strong>Package installers:</strong> Tools like pip, uv which we use to install dependencies.</li><li><strong>Python interpreter:</strong> The Python binary used to run our programs.</li></ul><p>For ourpython executable, there’s an arrow (-&gt;) pointing to our global Python installation. This is called a <strong>symbolic link (aka symlink), w</strong>hich is basically like a shortcut, and what this does is that whenever we run the python3, python or python3.14, we execute the Python that is installed <strong>globally</strong> on our system.</p><p>But because the binary detects the adjacent pyvenv.cfg, it dynamically re-points its <a href="https://docs.python.org/3/library/sys.html#sys.prefix">sys.prefix</a> (base directory of lib/site-packages) and package search path (sys.path) to .venv .</p><ul><li>include — Contains C header files that some packages in your project might need.</li><li>lib — This contains the folder python{VERSION}/site-packages where packages are located when installed via a package manager like pip, uv, or poetry. In my case, with Python 3.14, packages are installed to lib/python3.14/site-packages/xxx .</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/796/1*OBYt2IB7kX52xhKcrhtsvw.png" /><figcaption>Before Installing fastapi</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/850/1*C8J8UltKXvUMjWtNhcVA4Q.png" /><figcaption>after installing fastapi</figcaption></figure><ul><li>lib64 — A symlink to the lib folder. It’s created when the 64-bit version of Python is installed</li><li>pyvenv.cfg-The configuration file for our virtual environment. It contains the following:</li><li>home — The base directory of the Python interpreter that the virtual environment is using.</li><li>include-system-site-packages — A flag that indicates whether the globally installed packages should be used by the project instead of the ones located in the ENV_NAME/lib/python{VERSION}/site-packages. By default, it is set to false.</li><li>version — The version of Python being used.</li><li>executable — The location of the Python executable used.</li><li>command — The command used to create the environment.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/850/1*tsiyZlV1zHtHXLFAliM9XQ.png" /><figcaption>This is what the file contains when we use the built-in <strong>venv</strong> module.</figcaption></figure><p>The built-in site module uses the settings above to set up our virtual environment.</p><h4>Activating the Virtual Environment</h4><p>Now that we’ve broken down the contents of the .venv folder, let’s take a look at the magic that happens when we run the command source .venv/bin/activate in our shell. In order:</p><ol><li>The activate script first <strong>creates</strong>deactivate , which <strong>undoes</strong> the actions listed below.</li><li>It sets the VIRTUAL_ENV environment variable to point to the absolute path of the .venv folder.</li><li>Adds VIRTUAL_ENV/bin to the beginning of the terminal’s PATH environment variable. By doing this, whenever we run python within our environment, it uses the Python located in the .venv folder.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/797/1*zECgBH5dzT4wd7fdiHx78g.png" /><figcaption>Before activating virtual environment</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/802/1*E9AzxBDClP5rQGXmJwR6Fw.png" /><figcaption>After. The highlighted section is the path to our bin. The Python we’re using also points to the one in the .venv folder as indicated by the which command.</figcaption></figure><p>4. Our shell prompt is also tweaked to show that we are currently in a virtual environment.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/797/1*r8KO4US4eyU2rlkusNWo5g.png" /><figcaption>Before activation</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/789/1*gpzjGrRnWKnEB_O1pkxjTA.png" /><figcaption>After activation (the directory where .venv folder resides is used as the name)</figcaption></figure><p>I’m committing to writing one technical breakdown per week to solidify what I learn and help others along the way.</p><p>Thanks for reading, and have a wonderful day.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=33fc63a2f922" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>