<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Chris</title>
    <description>The latest articles on DEV Community by Chris (@treehouse).</description>
    <link>https://dev.to/treehouse</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F19009%2F6bc4385c-567b-4566-ad3b-992a7f783f95.jpg</url>
      <title>DEV Community: Chris</title>
      <link>https://dev.to/treehouse</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/treehouse"/>
    <language>en</language>
    <item>
      <title>Portable Python Bundles on Windows</title>
      <dc:creator>Chris</dc:creator>
      <pubDate>Sun, 26 Jan 2025 12:06:44 +0000</pubDate>
      <link>https://dev.to/treehouse/portable-python-bundles-on-windows-41ac</link>
      <guid>https://dev.to/treehouse/portable-python-bundles-on-windows-41ac</guid>
      <description>&lt;p&gt;Packaging Python applications and environments on MS Windows for other users so that they're "ready to run" on any machine can be a tricky task. This blog post describes my personal solution to the problem: Something that I like to call Python &lt;strong&gt;&lt;em&gt;Bundles&lt;/em&gt;&lt;/strong&gt; for Windows, a construct similar to virtual environments, but portable between machines.&lt;/p&gt;

&lt;p&gt;Python Bundles stand somewhat at the intersection of the values and tradeoffs provided by &lt;a href="https://docs.python.org/3/library/venv.html" rel="noopener noreferrer"&gt;virtual envs&lt;/a&gt;, regular Python installations and &lt;a href="https://pyinstaller.org/en/stable/" rel="noopener noreferrer"&gt;standalone executables created by tools like &lt;code&gt;pyinstaller&lt;/code&gt;&lt;/a&gt; or &lt;a href="https://www.py2exe.org/" rel="noopener noreferrer"&gt;&lt;code&gt;py2exe&lt;/code&gt;&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;No new tool is required to create such a bundle. It's just a loose and lightweight convention for a folder structure and some wrapper scripts that you can easily create manually. Or automate their creation in scripts or CI jobs.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftm8duxdmzr9ql62jutjn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftm8duxdmzr9ql62jutjn.png" alt="Python Bundles" width="800" height="852"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  1. The problem
&lt;/h2&gt;

&lt;p&gt;Let's assume we want to package and ship a Python application or environment to our users in a self-contained and "ready to run" way.&lt;/p&gt;

&lt;p&gt;We probably don't know what version of Python our users have installed, if any at all. And we definitely don't want to tamper with their potentially existing Python installations, which includes not having them to ask to install an additional version of Python. In other words: Our package should be all our users need to run our application or work with our Python environment.&lt;/p&gt;

&lt;h3&gt;
  
  
  1.1 The problem with venvs
&lt;/h3&gt;

&lt;p&gt;After some brainstorming we might come up with the idea to simply create a virtual env (&lt;code&gt;python -m venv venv_dir&lt;/code&gt;), install everything into the venv and then zip and distribute the venv folder to our users. But then we realize that venvs are based on hardcoded paths and a venv folder can't get easily relocated to a different path than the one it was created in. And that our venv in addition depends on the base Python installation that was used to create it (with exactly that Python version under exactly that path). So we'd need to tell our users exactly where to place their copy of our venv. And that they have to install a specific version of Python under a specific path. Not what we want.&lt;/p&gt;

&lt;h3&gt;
  
  
  1.2 The problem with packaging up regular Python installations
&lt;/h3&gt;

&lt;p&gt;Instead of a venv we could just install all of our requirements into a regular Python installation and zip and distribute its folder (i.e. &lt;code&gt;c:\Program Files\Python 3.13.1&lt;/code&gt;). That would work &lt;em&gt;mostly&lt;/em&gt;. The Python installation dir on Windows can get generally relocated to a different path (not so on Unix due to the static &lt;a href="https://docs.python.org/3/using/configure.html#cmdoption-prefix" rel="noopener noreferrer"&gt;prefix path&lt;/a&gt; - but that's a different topic).&lt;/p&gt;

&lt;p&gt;However, there's one big flaw: There are script executables (&lt;code&gt;.exe&lt;/code&gt; files in the &lt;code&gt;.\Scripts\&lt;/code&gt; dir, usually created by &lt;code&gt;pip&lt;/code&gt; when a package isn't just a library but also provides scripts as entrypoints. One such executable is &lt;code&gt;pip.exe&lt;/code&gt; itself). And these script executables depend on the path to the Python installation that is hardcoded "in themselves". Trying to relocate the Python installation dir results in these &lt;code&gt;.exe&lt;/code&gt; files to no longer be working.&lt;/p&gt;

&lt;h3&gt;
  
  
  1.3 The problem with tools like &lt;code&gt;PyInstaller&lt;/code&gt; and &lt;code&gt;py2exe&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;Tools like &lt;a href="https://pyinstaller.org/" rel="noopener noreferrer"&gt;PyInstaller&lt;/a&gt; or &lt;a href="https://www.py2exe.org/" rel="noopener noreferrer"&gt;py2exe&lt;/a&gt; can bundle a Python application and all its dependencies into a single package. The user can run the packaged app without installing a Python interpreter or any modules.&lt;/p&gt;

&lt;p&gt;This perfectly solves our distribution needs. However, the tradeoff is that we're not distributing a full Python environment, but a custom, minified bundle format. This might be the right tool for packaging an application. But it isn't suitable if we, for example, want to send our users a "starterkit" Python environment that can be used in an IDE and get extended with additional &lt;code&gt;pip install&lt;/code&gt;s, etc. We're looking for a more generic solution.&lt;/p&gt;




&lt;h2&gt;
  
  
  2. My recipe for a solution: Meet Python &lt;strong&gt;&lt;em&gt;Bundles&lt;/em&gt;&lt;/strong&gt;!
&lt;/h2&gt;

&lt;p&gt;Let's start with creating a bundle. It's quick and easy.&lt;/p&gt;

&lt;p&gt;(You can find the completed example bundle from this article &lt;a href="https://github.com/christian-korneck/python_winbundle_demo1" rel="noopener noreferrer"&gt;here on GitHub&lt;/a&gt;).&lt;/p&gt;

&lt;h3&gt;
  
  
  2.1 Creating a bundle from scratch
&lt;/h3&gt;

&lt;p&gt;We'll start off in Powershell, creating a folder for our bundle:&lt;br&gt;
(Feel free to name it however you like, here we'll just go with &lt;code&gt;bundle&lt;/code&gt; as the name).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="n"&gt;mkdir&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;bundle&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="n"&gt;cd&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;bundle&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then, we'll add a Python installation in &lt;code&gt;&amp;lt;bundle&amp;gt;\python3&lt;/code&gt;. In this example, I'll download and add Python 3.13.1 from its &lt;a href="https://dev.tourl"&gt;nuget package&lt;/a&gt; , which is officially maintained by the CPython project and can be used as a "portable" copy of Python. (In the nuget zip file, the Python installation is in the &lt;code&gt;tools&lt;/code&gt; dir. That's all we need here).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="n"&gt;curl.exe&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nt"&gt;-L&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"https://www.nuget.org/api/v2/package/python/3.13.1"&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nt"&gt;-o&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;python3.zip&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="n"&gt;Expand-Archive&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;\python3.zip&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nt"&gt;-DestinationPath&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;extracted_nuget&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="n"&gt;move&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;\extracted_nuget\tools&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;python3&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="n"&gt;rm&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nt"&gt;-R&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;extracted_nuget&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="n"&gt;rm&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;\python3.zip&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now our bundle looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;bundle
└───python3
    ├───python3.exe
    ├───Lib/
    ├───...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's also add a &lt;code&gt;Scripts&lt;/code&gt; dir:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="n"&gt;mkdir&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;python3\Scripts&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Currently we don't have &lt;code&gt;pip&lt;/code&gt; enabled yet, so let's do that now.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="n"&gt;python3\python.exe&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nt"&gt;-m&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;ensurepip&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To work around the problem that &lt;code&gt;pip&lt;/code&gt; creates &lt;code&gt;.exe&lt;/code&gt; files in the &lt;code&gt;Scripts&lt;/code&gt; dir that depend on a hardcoded Python installation path, we are going to use a wrapper script for &lt;code&gt;pip&lt;/code&gt;, that monkey-patches the behaviour of &lt;code&gt;pip&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Let's create a file &lt;code&gt;&amp;lt;bundle_root&amp;gt;\python_wrapper\scripts\pip.py&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;#!/usr/bin/python
&lt;/span&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;sys&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;__name__&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;__main__&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;pip._vendor.distlib.scripts&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;ScriptMaker&lt;/span&gt;
    &lt;span class="n"&gt;ScriptMaker&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;executable&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sa"&gt;r&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;python.exe&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;

    &lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;pip._internal.cli.main&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;
    &lt;span class="n"&gt;sys&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;exit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;How does it work? Whenever, we install a package via our &lt;code&gt;pip.py&lt;/code&gt; wrapper script (i.e. by running &lt;code&gt;python3\python.exe pip_wrapper\scripts\pip.py &amp;lt;some-package&amp;gt;&lt;/code&gt;), any new &lt;code&gt;.exe&lt;/code&gt; file generated in &lt;code&gt;python3\Scripts\&lt;/code&gt; will now just point to and use whatever &lt;code&gt;python.exe&lt;/code&gt; is found via the &lt;code&gt;PATH&lt;/code&gt; environment variable during runtime (instead of using a hardcoded, absolute path to an executable like &lt;code&gt;c:\program files\Python 3.13.1\python.exe&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;This is, of course risky and has implications. It is now our job to ensure that the &lt;code&gt;python.exe&lt;/code&gt; in the PATH variable is the correct one when someone executes such a "patched" &lt;code&gt;Scripts\*.exe&lt;/code&gt; file. That's why our bundle needs to get &lt;em&gt;activated&lt;/em&gt; by the user, similar to a virtual env. We'll get to that in a moment.&lt;/p&gt;

&lt;p&gt;(For more infos on the idea of such a pip wrapper, &lt;a href="https://github.com/christian-korneck/pip-wrapper/" rel="noopener noreferrer"&gt;see here&lt;/a&gt;)&lt;/p&gt;

&lt;p&gt;Now, wouldn't it be nice to also have a &lt;code&gt;pip.exe&lt;/code&gt; for our pip wrapper, so that we later can just use the &lt;code&gt;pip&lt;/code&gt; command (instead of having to do &lt;code&gt;python pip.py&lt;/code&gt;)? Let's create one. It needs to be portable too, of course, that's why we'll create it in a similar way.&lt;/p&gt;

&lt;p&gt;To do so, let's create a &lt;code&gt;&amp;lt;bundle&amp;gt;\pip_wrapper\bin&lt;/code&gt; folder and create the &lt;code&gt;.exe&lt;/code&gt; file there.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="n"&gt;mkdir&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;pip_wrapper\bin&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then let's start a Python shell (REPL) with &lt;code&gt;python3\python.exe&lt;/code&gt; and execute the following code to create the &lt;code&gt;pip.exe&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;pip._vendor.distlib.scripts&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;ScriptMaker&lt;/span&gt;
&lt;span class="n"&gt;maker&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;ScriptMaker&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;pip_wrapper/scripts&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;pip_wrapper/bin&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;maker&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;executable&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sa"&gt;r&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;python.exe&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
&lt;span class="n"&gt;maker&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;make&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;pip.py&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Our folder structure should now look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;bundle
└───python3
    ├───python3.exe
    ├───Scripts/
    ├───Lib/
    ├───...
└───pip_wrapper
    ├───scripts/
        ├───pip.py
    ├───bin/
        ├───pip.exe
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, we'll need a way to activate our Python bundle in a current &lt;code&gt;cmd.exe&lt;/code&gt; or &lt;code&gt;powershell&lt;/code&gt; shell session.&lt;/p&gt;

&lt;p&gt;For this we're going to create venv-style &lt;code&gt;activate&lt;/code&gt; scripts for both shell types in the root of our bundle:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&amp;lt;bundle&amp;gt;\activate.cmd&lt;/code&gt; :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight batchfile"&gt;&lt;code&gt;@echo &lt;span class="na"&gt;off&lt;/span&gt;
&lt;span class="kd"&gt;set&lt;/span&gt; &lt;span class="kd"&gt;PATH&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="vm"&gt;%~dp0&lt;/span&gt;&lt;span class="kd"&gt;pip_wrapper&lt;/span&gt;\bin\&lt;span class="o"&gt;;&lt;/span&gt;&lt;span class="vm"&gt;%~dp0&lt;/span&gt;&lt;span class="kd"&gt;python3&lt;/span&gt;\Scripts\&lt;span class="o"&gt;;&lt;/span&gt;&lt;span class="vm"&gt;%~dp0&lt;/span&gt;&lt;span class="kd"&gt;python3&lt;/span&gt;\&lt;span class="o"&gt;;&lt;/span&gt;&lt;span class="nv"&gt;%PATH%&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;&amp;lt;bundle&amp;gt;\activate.ps1&lt;/code&gt; :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ScriptDir&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Split-Path&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nt"&gt;-Parent&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="bp"&gt;$MyInvocation&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;MyCommand&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Definition&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="nv"&gt;$&lt;/span&gt;&lt;span class="nn"&gt;Env&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="nv"&gt;PATH&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$ScriptDir&lt;/span&gt;&lt;span class="s2"&gt;\pip_wrapper\bin;&lt;/span&gt;&lt;span class="nv"&gt;$ScriptDir&lt;/span&gt;&lt;span class="s2"&gt;\python3\Scripts;&lt;/span&gt;&lt;span class="nv"&gt;$ScriptDir&lt;/span&gt;&lt;span class="s2"&gt;\python3;&lt;/span&gt;&lt;span class="nv"&gt;$&lt;/span&gt;&lt;span class="nn"&gt;Env&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="nv"&gt;PATH&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="w"&gt;

&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now our basic bundle folder structure is complete.&lt;/p&gt;




&lt;h2&gt;
  
  
  3. Usage
&lt;/h2&gt;

&lt;p&gt;So how can we use our newly created Bundle?&lt;/p&gt;

&lt;p&gt;Let's open either a &lt;code&gt;cmd.exe&lt;/code&gt; shell or a &lt;code&gt;powershell&lt;/code&gt; session and activate the bundle.&lt;/p&gt;

&lt;h3&gt;
  
  
  3.1 Activate in a &lt;code&gt;cmd.exe&lt;/code&gt; shell:
&lt;/h3&gt;

&lt;p&gt;To activate the bundle:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;call &amp;lt;bundle&amp;gt;\activate.cmd
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;call c:\workspace\activate.cmd
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can now verify that our active python, pythonw and pip executables are those from the bundle:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ where python
c:\workspace\bundle\python3\python.exe
$ where pip
c:\workspace\bundle\pip_wrapper\bin\pip.exe
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  3.2 Activate in a Powershell instance:
&lt;/h3&gt;

&lt;p&gt;Open a Powershell instance and activate the bundle using the dot source method:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;bundle&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;\activate.ps1&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;c:\workspace\bundle\activate.ps1&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;(If you receive a Powershell error message that script execution is disallowed for your user, you may need to allow it once with &lt;code&gt;Set-ExecutionPolicy RemoteSigned -Scope CurrentUser&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;Afterwards you can verify that the currently active python.exe, pip.exe, etc are indeed the ones from our bundle:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;Get-Command&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;python&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;Format-List&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nt"&gt;-Property&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;Path&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="n"&gt;C:\workspace\bundle2\python3\python.exe&lt;/span&gt;&lt;span class="w"&gt;

&lt;/span&gt;&lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;Get-Command&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;pip&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;Format-List&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nt"&gt;-Property&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;Path&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="n"&gt;C:\workspace\bundle2\pip_wrapper\bin\pip.exe&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  3.3 How to use a bundle after its activation:
&lt;/h3&gt;

&lt;p&gt;We can now use the bundle's Python environment from the shell that we have activated it in, similar to an activated venv. (In the following I'm assuming we're in Powershell).&lt;/p&gt;

&lt;p&gt;For example we could use &lt;code&gt;pip&lt;/code&gt; to install Jupyter Notebook:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ pip install notebook
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This creates a &lt;code&gt;&amp;lt;bundle&amp;gt;\python3\Scripts\jupyter.exe&lt;/code&gt; that is already in the PATH of our activated environment:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;Get-Command&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;jupyter&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;Format-List&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nt"&gt;-Property&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;Path&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="n"&gt;C:\workspace\bundle2\python3\Scripts\jupyter.exe&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So, to start Jupyter Notebook, we could run this command directly from our Powershell instance:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="n"&gt;jupyter&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;notebook&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  4. You said our bundle is portable?
&lt;/h2&gt;

&lt;p&gt;It is! You can rename or copy the &lt;code&gt;&amp;lt;bundle&amp;gt;&lt;/code&gt; dir to any path you like - it will not break. Try it out and make a copy of it under a new name.&lt;/p&gt;

&lt;p&gt;This means you can zip the &lt;code&gt;&amp;lt;bundle&amp;gt;&lt;/code&gt; folder and send it to other users. All they need to do is to place it somewhere on their Windows filesystem, activate it in a shell and they're ready to go.&lt;/p&gt;

&lt;p&gt;It's like a venv - but self-contained and portable / path-independent. Mission accomplished. 🎉&lt;/p&gt;




&lt;h2&gt;
  
  
  5. FAQ &amp;amp; Bonus section
&lt;/h2&gt;

&lt;p&gt;We’ve reached the end of this blog post. If you’re still not tired to read on, here’s some bonus stuff.&lt;/p&gt;

&lt;h3&gt;
  
  
  5.1 Q: &lt;em&gt;Can I still start &lt;code&gt;Scripts\*.exe&lt;/code&gt; files directly (i.e. double-click on them)?&lt;/em&gt;
&lt;/h3&gt;

&lt;p&gt;A: No, other than with a regular Python installation or venv &lt;code&gt;.exe&lt;/code&gt; files from the Script dir can only get started from an activated shell. If you need a replacement for this limitation, I'd recommend to create wrapper scripts for them. Here's an example for a wrapper script for &lt;code&gt;jupyter.exe&lt;/code&gt;, that passes through all arguments to the jupyter processes and passes back its exit code to the shell.&lt;/p&gt;

&lt;p&gt;You can double click on it to launch it or use it in a cmd shell with or without arguments.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&amp;lt;bundle&amp;gt;\jupyter.cmd&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight batchfile"&gt;&lt;code&gt;@echo &lt;span class="na"&gt;off&lt;/span&gt;
&lt;span class="nb"&gt;setlocal&lt;/span&gt;
&lt;span class="k"&gt;call&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="vm"&gt;%~dp0&lt;/span&gt;&lt;span class="s2"&gt;activate.cmd"&lt;/span&gt;
&lt;span class="s2"&gt;"jupyter.exe"&lt;/span&gt; &lt;span class="err"&gt;%&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;
&lt;span class="k"&gt;exit&lt;/span&gt; &lt;span class="na"&gt;/B &lt;/span&gt;&lt;span class="nv"&gt;%errorlevel%&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  5.2 Q: What are some good example use cases for bundles?
&lt;/h3&gt;

&lt;h4&gt;
  
  
  5.2.1 Example1:
&lt;/h4&gt;

&lt;p&gt;I have created a Jupyter Notebook, that requires a specific set of libraries for its contents to work (like numpy, pandas, etc). I want to share this notebook with someone and make it low effort for them to open it on their Windows machine. Creating a bundle allows me to tell them: &lt;em&gt;"Here's a zip file, extract it anywhere you like and in the extracted folder you'll find a &lt;code&gt;notebook.cmd&lt;/code&gt; wrapper script. Just double-click it to start the Jupyter WebUI."&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;(I've put exactly this example as a &lt;a href="https://github.com/christian-korneck/python_winbundle_demo1" rel="noopener noreferrer"&gt;Demo on GitHub here&lt;/a&gt;).&lt;/p&gt;

&lt;h4&gt;
  
  
  5.2.2 Example2:
&lt;/h4&gt;

&lt;p&gt;I want to create a Windows installer for a Python application. The entire runtime and dependencies should be part of my application bundle to make things easy and safe for the user. The default installation path by the installer is &lt;code&gt;c:\program files\myapp&lt;/code&gt;, but the installer also lets the user optionally select a custom path. Packaging my app as a bundle gives me portability and path independence. I can automate the creation of the bundle as a build artifact in the CI pipeline of my application project.&lt;/p&gt;

&lt;h3&gt;
  
  
  5.3 Q: What about Unix?
&lt;/h3&gt;

&lt;p&gt;Linux, MacOS, etc are out of scope of this solution. Bundles only work on Windows. On Unix systems you usually need to build Python for a specific prefix path (i.e. &lt;code&gt;/opt/myapp/python&lt;/code&gt;) and the resulting Python binaries aren't moveable to other paths. You also need to build against various dependencies, which differ between distros. This all makes it more difficult.&lt;/p&gt;

&lt;p&gt;An easy way for application/environment bundles on Linux are Docker/OCI containers. The &lt;a href="https://hub.docker.com/_/python" rel="noopener noreferrer"&gt;official python:3 and python:3-slim base images&lt;/a&gt; are a great starting point.&lt;/p&gt;

&lt;p&gt;Alternatively you could create distro specific bundles that need to get placed under a specific path (i.e. &lt;code&gt;/opt/myapp&lt;/code&gt;). You can use most patterns of our Windows bundles, but you'd need to build your own copy of Python from source for your custom prefix path with something like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;./configure &lt;span class="nt"&gt;--prefix&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;/opt/myapp/python3
make &lt;span class="nt"&gt;-j&lt;/span&gt; &lt;span class="sb"&gt;`&lt;/span&gt;&lt;span class="nb"&gt;nproc&lt;/span&gt;&lt;span class="sb"&gt;`&lt;/span&gt;
make &lt;span class="nb"&gt;install&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  5.4 Can I use bundles with IDEs and editors?
&lt;/h3&gt;

&lt;p&gt;Many IDEs and editors have first class support for venvs , but are of course unaware of bundles. So some extra steps might be necessary.&lt;/p&gt;

&lt;p&gt;For some IDEs, it's sufficient to just start the IDE from an activated shell. For some other IDEs you might need to modify the IDE's Python runner/debug configuration profile, so that the correct Python executable is being used and the PATH environment variable gets prepended with the same three paths that our &lt;code&gt;activate&lt;/code&gt; scripts set:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;&amp;lt;bundle&amp;gt;\pip_wrapper\bin&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;&amp;lt;bundle&amp;gt;\python3\scripts\&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;&amp;lt;bundle&amp;gt;\python3\&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you'd like to see a more detailed follow up blog post how to do this in vscode, Pycharm, etc please leave a comment.&lt;/p&gt;

&lt;h3&gt;
  
  
  5.5 Q: Are bundles the right tool for every job? Should I stop using venvs?
&lt;/h3&gt;

&lt;p&gt;Certainly not. I for myself am using venvs as the default in my day to day work, for multiple reasons - the major one being to save disk space. Each bundle contains a full Python installation and is therefore much larger and slower to create than a venv.&lt;/p&gt;

&lt;p&gt;Here's a quick, incomplete comparison of some of the aspects of some of the many options, just to visualize the idea.&lt;/p&gt;

&lt;h4&gt;
  
  
  5.5.1 comparison
&lt;/h4&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;Bundle&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;Virtual Env&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;Python Installation&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;Pyinstaller&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Path independent and self-contained?&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;yes&lt;/td&gt;
&lt;td&gt;no&lt;br&gt;&lt;br&gt;(path to Python installation is hardcoded in venv)&lt;/td&gt;
&lt;td&gt;no&lt;br&gt;&lt;br&gt;(&lt;code&gt;.\scripts\*.exe&lt;/code&gt; files will break)&lt;/td&gt;
&lt;td&gt;yes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Can have multiple instances on the same system&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;yes&lt;/td&gt;
&lt;td&gt;yes&lt;/td&gt;
&lt;td&gt;not without issues&lt;br&gt;&lt;br&gt;(concept is one Python installation per Python version per user or system)&lt;/td&gt;
&lt;td&gt;yes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Disk Usage&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;large&lt;br&gt;&lt;br&gt;(contains a full Python installation)&lt;/td&gt;
&lt;td&gt;small&lt;br&gt;&lt;br&gt;(depends on a Python installation)&lt;/td&gt;
&lt;td&gt;large&lt;/td&gt;
&lt;td&gt;medium&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Needs to get activated&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;yes&lt;/td&gt;
&lt;td&gt;yes&lt;/td&gt;
&lt;td&gt;no&lt;/td&gt;
&lt;td&gt;no&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Single executable&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;no&lt;/td&gt;
&lt;td&gt;no&lt;/td&gt;
&lt;td&gt;no&lt;/td&gt;
&lt;td&gt;yes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Can be used like a regular Python installation (REPL, pip, scripts, etc)&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;yes&lt;/td&gt;
&lt;td&gt;yes&lt;/td&gt;
&lt;td&gt;yes&lt;/td&gt;
&lt;td&gt;no&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Can be used with IDEs?&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;yes, but you might need to configure environment variables in the IDE’s run/debug profile&lt;/td&gt;
&lt;td&gt;yes&lt;/td&gt;
&lt;td&gt;yes&lt;/td&gt;
&lt;td&gt;no&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

</description>
      <category>python</category>
      <category>microsoft</category>
      <category>virtualenv</category>
      <category>pip</category>
    </item>
    <item>
      <title>How to 100% CPU</title>
      <dc:creator>Chris</dc:creator>
      <pubDate>Wed, 08 Jan 2025 20:41:58 +0000</pubDate>
      <link>https://dev.to/treehouse/how-to-100-cpu-1h60</link>
      <guid>https://dev.to/treehouse/how-to-100-cpu-1h60</guid>
      <description>&lt;p&gt;I've been working with many sysadmins over the years and one question comes up from time to time: "I quickly need to create some dummy CPU load on this machine, what cpu stress tool should I install?"&lt;/p&gt;

&lt;p&gt;If our need is &lt;em&gt;very&lt;/em&gt; basic (i.e. we &lt;em&gt;just&lt;/em&gt; want to see 100% CPU load on one or multiple cores), maybe we should consider building our own.&lt;/p&gt;

&lt;h2&gt;
  
  
  The One-Liner
&lt;/h2&gt;

&lt;p&gt;All we need is to put this line of C code in a file, build it with &lt;code&gt;gcc -o stressme stressme.c&lt;/code&gt; (or on Windows &lt;code&gt;cl stressme.c&lt;/code&gt;) and run it with &lt;code&gt;./stressme&lt;/code&gt; (or &lt;code&gt;stressme.exe&lt;/code&gt;).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{}}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And while the program runs, we'll see 100% CPU load on one core. For multiple cores, we could start the program multiple times.&lt;/p&gt;

&lt;h2&gt;
  
  
  Multi-Threaded
&lt;/h2&gt;

&lt;p&gt;Or we could use threads, here's a variant that uses four POSIX threads:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;pthread.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;unistd.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="cp"&gt;#define NUM_THREADS 4
&lt;/span&gt;
&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="nf"&gt;loop&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;arg&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;pthread_t&lt;/span&gt; &lt;span class="n"&gt;threads&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;NUM_THREADS&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;NUM_THREADS&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;pthread_create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;threads&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;loop&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;pause&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;(To build it, add the &lt;code&gt;-pthread&lt;/code&gt; flag: &lt;code&gt;gcc -o multistress multistress.c -pthread&lt;/code&gt;)&lt;/p&gt;

&lt;h2&gt;
  
  
  Why does this work?
&lt;/h2&gt;

&lt;p&gt;We're running an infinite loop. When we look at the simplest infinite loop in assembly, it becomes clear the CPU is &lt;em&gt;busy&lt;/em&gt; doing &lt;em&gt;something&lt;/em&gt;: Executing a &lt;code&gt;jmp&lt;/code&gt; instruction that "jumps to itself", as fast as possible. So the CPU is busy running instructions. It's not doing nothing like with a pause/sleep.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight nasm"&gt;&lt;code&gt;&lt;span class="nf"&gt;global&lt;/span&gt; &lt;span class="nv"&gt;_start&lt;/span&gt;

&lt;span class="nl"&gt;_start:&lt;/span&gt;
    &lt;span class="nf"&gt;jmp&lt;/span&gt; &lt;span class="nv"&gt;_start&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If we were on an older operating system with a cooperative multitasking scheduler, such an infinite loop would probably make our system unresponsive. On today's preemptive multitasking systems, infinite loops cause the program to consume all available processor time, but can still be terminated.&lt;/p&gt;

</description>
      <category>c</category>
      <category>assembly</category>
      <category>linux</category>
      <category>benchmark</category>
    </item>
    <item>
      <title>Migrating a large Dropbox account to Onedrive</title>
      <dc:creator>Chris</dc:creator>
      <pubDate>Sat, 02 Sep 2023 12:12:36 +0000</pubDate>
      <link>https://dev.to/treehouse/migrating-a-large-dropbox-account-to-onedrive-4mia</link>
      <guid>https://dev.to/treehouse/migrating-a-large-dropbox-account-to-onedrive-4mia</guid>
      <description>&lt;p&gt;I recently migrated my ~ 1TB Dropbox account (single user paid plan) to Microsoft OneDrive (consumer version). Here's my "lessons learned".&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;I prepaid for Microsoft Office 365 Home for several years with discounted gift cards bought during a Black Friday sale. It turns out you can prepay Office 365 Home for a maximum of 4 years. When you try to redeem more gift cards you'll receive a cryptic error message ("MaxStackingDurationExceeded") - and you'll need to wait at least 1 year before you can redeem the next gift card. (I believe the gift cards have no specific expiration date, but am not entirely sure).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The easiest way to migrate data from Dropbox to OneDrive (consumer) is to use Microsoft's &lt;a href="https://mover.io" rel="noopener noreferrer"&gt;https://mover.io&lt;/a&gt; service. Mover was a data cloud migration startup that Microsoft bought a couple of years ago and integrated their tech into its Enterprise offerings. For consumers who want to migrate their data from another cloud storage to OneDrive (consumer) Microsoft still operates the original mover.io service - and it's free. (More info in the Microsoft docs: &lt;a href="https://support.microsoft.com/en-us/office/transfer-files-to-your-personal-onedrive-with-the-mover-transfer-wizard-7dbda93c-71e6-483f-8914-ad445554cd31" rel="noopener noreferrer"&gt;https://support.microsoft.com/en-us/office/transfer-files-to-your-personal-onedrive-with-the-mover-transfer-wizard-7dbda93c-71e6-483f-8914-ad445554cd31&lt;/a&gt;) &lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F86p37z8qrkoe1et4zrve.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F86p37z8qrkoe1et4zrve.png" alt="mover ui" width="800" height="546"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;you can login on mover.io with your Microsoft (consumer) login and connect your Dropbox account as source and your OneDrive account as destination, pick a source and target folder (i.e. the root folder on both sides) and start a data transfer job.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;the data transfer runs on the mover.io servers (not via your laptop and your own Internet connection). In my case it took around 14 hours to transfer ~ 1 TB data. In the end you get access to a detailed log that can be filtered for skipped and error'ed files.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;in my case I had a number of files that hadn't been copied due to these problems:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt; file too large (OneDrive only allows files up to 10 GB. I had some ISOs, VM images, etc that were larger)&lt;/li&gt;
&lt;li&gt;empty files. (unlike Dropbox, OneDrive doesn't support 0 byte files. In my case I had hundreds of &lt;code&gt;.gitkeep&lt;/code&gt;, &lt;code&gt;__init__.py&lt;/code&gt;, etc files. Fixing these was a manual process (i.e. I put in comments or dummy content).&lt;/li&gt;
&lt;li&gt;path too long (OneDrive doesn't support long paths)&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;&lt;p&gt;mover.io also seems to support other cloud storage providers as source like Google Drive, Box and other OneDrive accounts. (However, I haven't tried connecting them).&lt;/p&gt;&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxy34mulj00k91jdojkvb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxy34mulj00k91jdojkvb.png" alt="mover.io supported cloud storage providers" width="636" height="413"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;That's it. Happy migrating!&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
