<?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: bhagerty</title>
    <description>The latest articles on DEV Community by bhagerty (@bhagerty).</description>
    <link>https://dev.to/bhagerty</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%2F269172%2F97977be9-9b81-461b-b191-7b00fe102403.jpeg</url>
      <title>DEV Community: bhagerty</title>
      <link>https://dev.to/bhagerty</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/bhagerty"/>
    <language>en</language>
    <item>
      <title>Setting up Python on Windows</title>
      <dc:creator>bhagerty</dc:creator>
      <pubDate>Mon, 09 May 2022 02:08:43 +0000</pubDate>
      <link>https://dev.to/bhagerty/setting-up-python-on-windows-5216</link>
      <guid>https://dev.to/bhagerty/setting-up-python-on-windows-5216</guid>
      <description>&lt;p&gt;There are many ways to set up Python on Windows—&lt;strong&gt;&lt;em&gt;too many&lt;/em&gt;&lt;/strong&gt; ways. According to &lt;a href="https://peps.python.org/pep-0020/"&gt;the Zen of Python&lt;/a&gt;:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;There should be one—and preferably only one—obvious way to do it.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In this spirit, I describe here what I think is the simplest way to set up Python on Windows, where "simplest" means:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;using &lt;strong&gt;pure Python&lt;/strong&gt;, with no extra packages;&lt;/li&gt;
&lt;li&gt;enabling &lt;strong&gt;multiple versions&lt;/strong&gt; of Python to exist side-by-side, without clobbering each other; and&lt;/li&gt;
&lt;li&gt;using &lt;strong&gt;no magic&lt;/strong&gt;, meaning the user is in control and can understand everything (this is related to 1 above);&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Assuming you have never installed Python, the basic steps are:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;disable the Windows Python installer;&lt;/li&gt;
&lt;li&gt;download Python—as many versions as you want—from &lt;a href="https://www.python.org/downloads/"&gt;python.org&lt;/a&gt;;&lt;/li&gt;
&lt;li&gt;install each version in your user directory, &lt;em&gt;with&lt;/em&gt; the Python launcher, but &lt;em&gt;without&lt;/em&gt; adding anything to your path; and &lt;/li&gt;
&lt;li&gt;using &lt;code&gt;py.exe&lt;/code&gt;, the &lt;a href="https://peps.python.org/pep-0397/"&gt;Python launcher built-in to Python on Windows&lt;/a&gt;, within each of your projects, create a virtual environment as a subfolder in the project folder, specifying the Python version needed.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;I conclude with some tips on using VS Code and a discussion of alternative methods of setting up Python on Windows.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 1: Disable the Windows Python installer
&lt;/h2&gt;

&lt;p&gt;If you type &lt;code&gt;python&lt;/code&gt; or &lt;code&gt;python3&lt;/code&gt; at a command prompt in a new Windows 10 installation, you land at a link at the Microsoft Store for installing Python. This is supposed to make things easy for beginners. It sort of does, but at the expense of potentially making it harder to manage multiple Python versions. Installation via the Microsoft Store smells like magic to me, so I don't recommend it. &lt;/p&gt;

&lt;p&gt;To disable this behavior, go to &lt;em&gt;Start→Manage App Execution Aliases&lt;/em&gt; and &lt;strong&gt;turn off&lt;/strong&gt; the toggles for &lt;em&gt;App Installer - python.exe&lt;/em&gt; and &lt;em&gt;App Installer - python3.exe&lt;/em&gt;, like so:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--m9GdJt0A--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://bhagerty.github.io/assets/images/win10-app-aliases-python.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--m9GdJt0A--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://bhagerty.github.io/assets/images/win10-app-aliases-python.png" alt="screenshot of Manage App Execution Aliases window" width="320" height="414"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 2: Download Python from &lt;a href="https://www.python.org/downloads/"&gt;python.org&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;To avoid magic, &lt;a href="https://www.python.org/downloads/"&gt;download Python directly&lt;/a&gt;. You can install as many versions as you like. The order of installation doesn't really matter, though (as discussed below) it may affect the behavior of &lt;code&gt;py.exe&lt;/code&gt;, the Python launcher.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 3: Install Python in user directory, with launcher
&lt;/h2&gt;

&lt;p&gt;By default, when you install Python with an installer from &lt;a href="https://www.python.org/downloads/"&gt;python.org&lt;/a&gt;:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Python will be installed in a user-specific directory rather than a system directory, &lt;/li&gt;
&lt;li&gt;Python will install the Windows launcher, &lt;code&gt;py.exe&lt;/code&gt;, for all users (in &lt;code&gt;C:\Windows&lt;/code&gt;), and &lt;/li&gt;
&lt;li&gt;Python will not change your system path. &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Accept these defaults.&lt;/strong&gt; The installation screen should look like this: &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--_rqoOndb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://bhagerty.github.io/assets/images/python-310-installation.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--_rqoOndb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://bhagerty.github.io/assets/images/python-310-installation.png" alt="screenshot of Python 3.10 installation" width="521" height="320"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After you select &lt;em&gt;Install now&lt;/em&gt;, the next screen will ask if you want to &lt;em&gt;Disable path length limit&lt;/em&gt; so that your path can exceed the system's 260-character limit. I suspect this is &lt;strong&gt;unnecessary&lt;/strong&gt;, since we are deliberately not adding Python to the path. But I'm not sure there's a downside. Use your judgment. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: The next time you install some version of Python, the &lt;em&gt;Install launcher for all users&lt;/em&gt; option will probably be grayed out, because the launcher will already be installed. If it's grayed out, that's fine. If it's not grayed out, leave it selected, and the launcher will be updated.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 4: Use &lt;code&gt;py.exe&lt;/code&gt; to create virtual environments
&lt;/h2&gt;

&lt;p&gt;The &lt;a href="https://peps.python.org/pep-0397/"&gt;Python launcher &lt;code&gt;py.exe&lt;/code&gt;&lt;/a&gt; allows you to easily work with multiple different versions of Python. Basic commands (executed at a command prompt):&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;py&lt;/code&gt; — launches the &lt;strong&gt;latest version&lt;/strong&gt; of Python you have installed. 

&lt;ul&gt;
&lt;li&gt;There's a tiny bit of magic here. When you install a newer version of Python, the launcher will automagically update itself so that when &lt;code&gt;py&lt;/code&gt; is run with no arguments, it launches the latest version of Python. &lt;/li&gt;
&lt;li&gt;You will typically &lt;strong&gt;not use this by itself&lt;/strong&gt;, because you will be using &lt;code&gt;py&lt;/code&gt; to create virtual environments, rather than to run Python outside of an environment.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;py --list&lt;/code&gt; — shows you all installed versions of Python&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;py --help&lt;/code&gt; (or &lt;code&gt;py --help | more&lt;/code&gt; to get info a page at a time) — provides command-line switches for &lt;code&gt;py&lt;/code&gt; and for &lt;code&gt;python&lt;/code&gt; (which is called by &lt;code&gt;py&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;py -X.X&lt;/code&gt; (for instance, &lt;code&gt;py -3.10&lt;/code&gt;, or &lt;code&gt;py -3.9&lt;/code&gt;) — launch version &lt;code&gt;X.X&lt;/code&gt; of Python.

&lt;ul&gt;
&lt;li&gt;With no arguments, this launches the Python REPL. With arguments, this is how we execute modules or scripts. It's the key to creating virtual environments. &lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Creating virtual environments with &lt;code&gt;py.exe&lt;/code&gt; and &lt;code&gt;venv&lt;/code&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;At a command prompt, create a &lt;strong&gt;project directory&lt;/strong&gt; and navigate into to it. For example (here &lt;code&gt;&amp;gt;&lt;/code&gt; is the prompt):
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt; cd C:\my_projects
&amp;gt; mkdir project1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Within your project directory, &lt;strong&gt;use &lt;code&gt;py&lt;/code&gt;&lt;/strong&gt; to create a virtual environment, specifying the python version and the directory name. For example:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt; cd C:\my_projects\project1
&amp;gt; py -3.9 -m venv p1_venv
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Things to note:

&lt;ul&gt;
&lt;li&gt;You can substitute &lt;strong&gt;any installed version&lt;/strong&gt; for &lt;code&gt;-3.9&lt;/code&gt; in this statement (you don't need to specify the minor version).&lt;/li&gt;
&lt;li&gt;The environment name &lt;code&gt;p1_venv&lt;/code&gt; is &lt;strong&gt;arbitrary&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;This will create a subfolder &lt;code&gt;\p1_venv&lt;/code&gt; within the &lt;code&gt;C:\my_projects\project1&lt;/code&gt; parent folder. &lt;/li&gt;
&lt;li&gt;This structure—a top-level project folder with a virtual environment in a subfolder—is the preferred structure for doing Python development with VS Code. &lt;/li&gt;
&lt;li&gt;A lot of people use &lt;code&gt;venv&lt;/code&gt; or &lt;code&gt;.venv&lt;/code&gt; for the environment name. I &lt;strong&gt;dislike this convention&lt;/strong&gt; because the name &lt;code&gt;venv&lt;/code&gt; or &lt;code&gt;.venv&lt;/code&gt; is totally uninformative—it shows up in your prompt to tell you that &lt;em&gt;some&lt;/em&gt; virtual environment is activated, but not &lt;em&gt;which one&lt;/em&gt;. &lt;/li&gt;
&lt;li&gt;There might be other good reasons for always using &lt;code&gt;venv&lt;/code&gt; or &lt;code&gt;.venv&lt;/code&gt; to name a virtual environment. Perhaps it makes other setup tasks easier. But I've never seen anyone explain the benefits (if they exist).&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Using the virtual environment
&lt;/h3&gt;

&lt;p&gt;If your virtual environment is at &lt;code&gt;C:\my_projects\project1\p1_venv&lt;/code&gt;, activate it as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt; cd C:\my_projects\project1\
&amp;gt; .\p1_venv\Scripts\activate
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You should now see the virtual environment name before the prompt, like so (assuming your working directory is also shown at the prompt):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;(p1_venv) C:\my_projects\project1&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To deactivate, just enter &lt;code&gt;deactivate&lt;/code&gt; at the prompt. &lt;/p&gt;

&lt;h3&gt;
  
  
  Using Powershell aliases to simplify using virtual environments
&lt;/h3&gt;

&lt;p&gt;Creating Powershell aliases can make using virtual environments easier. Building on the example above, you might put something like this in your Powershell profile (a user- and host-specific profile is at &lt;code&gt;$profile.CurrentUserCurrentHost&lt;/code&gt; and is called &lt;code&gt;Microsoft.PowerShell_profile.ps1&lt;/code&gt; wherever it's found&lt;a id="a1"&gt;&lt;/a&gt;&lt;sup&gt;1&lt;/sup&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="c"&gt;# create function and alias that calls function&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="c"&gt;# p1 will change to project directory and activate environment&lt;/span&gt;&lt;span class="w"&gt;

&lt;/span&gt;&lt;span class="kr"&gt;Function&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nf"&gt;Use-project1&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&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="s2"&gt;"C:\my_projects\project1"&lt;/span&gt;&lt;span class="w"&gt; 
  &lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;\p1_venv\Scripts\activate&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;

&lt;/span&gt;&lt;span class="n"&gt;Set-Alias&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;p1&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;Use-project1&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Using VS Code with Python virtual environments
&lt;/h2&gt;

&lt;p&gt;The &lt;a href="https://marketplace.visualstudio.com/items?itemName=ms-python.python"&gt;VS Code Python extension&lt;/a&gt; is designed to work well with virtual environments, but I found &lt;a href="https://code.visualstudio.com/docs/python/environments"&gt;the documentation&lt;/a&gt; confusing. While &lt;a href="https://stackoverflow.com/questions/72070092/does-the-vs-code-documentation-for-python-really-mean-you-shouldnt-open-a-virtu"&gt;answering my own question on StackOverflow&lt;/a&gt;, I figured out what I think is a good workflow. This assumes you have &lt;strong&gt;already created your virtual environment&lt;/strong&gt; in a subfolder within your project directory:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;At the command prompt, navigate to your project directory (the directory that contains your virtual environment). (You could create an alias for this navigation if you like.)&lt;/li&gt;
&lt;li&gt;From there, execute &lt;code&gt;code .&lt;/code&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This will open VS Code, using your project directory as a workspace. If you create or open a Python file in the workspace, VS Code will &lt;strong&gt;automatically detect and activate the virtual environment&lt;/strong&gt; in the subfolder in the workspace (i.e., project directory).&lt;/p&gt;

&lt;p&gt;You might be tempted to use a virtual environment &lt;strong&gt;as&lt;/strong&gt; a project directory, that is, as the root of your workspace. Don't do it. The VS Code documentation should say&lt;a id="a2"&gt;&lt;/a&gt;&lt;sup&gt;2&lt;/sup&gt;):&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Note: Your Python virtual environment should always be a &lt;strong&gt;subdirectory within a VS Code workspace.&lt;/strong&gt; Opening the virtual-environment folder directly, as the root of the workspace, might cause problems.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;You &lt;em&gt;can&lt;/em&gt; create a Python virtual environment within a workspace by using VS Code's built-in terminal. But I prefer to create the environment first, outside of VS Code, because that way, I know exactly what's happening. &lt;/p&gt;

&lt;p&gt;If for some reason VS Code does &lt;em&gt;not&lt;/em&gt; detect your virtual environment, you can manually direct VS Code to it by opening the command palette with &lt;code&gt;Ctrl-Shift-P&lt;/code&gt;, entering &lt;code&gt;Python: Select Interpreter&lt;/code&gt;, and navigating to &lt;code&gt;python.exe&lt;/code&gt; found in the &lt;code&gt;\Scripts&lt;/code&gt; subfolder of the virtual environment. You should only need to do this once.&lt;/p&gt;

&lt;h2&gt;
  
  
  Alternative methods for setting up Python on Windows
&lt;/h2&gt;

&lt;h3&gt;
  
  
  The &lt;code&gt;pyenv&lt;/code&gt; project
&lt;/h3&gt;

&lt;p&gt;There is a whole project, &lt;strong&gt;&lt;a href="https://pyenv-win.github.io/pyenv-win/"&gt;pyenv for Windows&lt;/a&gt;&lt;/strong&gt;, specifically designed to allow you to manage multiple versions of Python on Windows. It's an impressive piece of work, and if it interests you, check out a &lt;a href="https://realpython.com/intro-to-pyenv/"&gt;very detailed Real Python tutorial&lt;/a&gt; on using it.&lt;/p&gt;

&lt;p&gt;I prefer not to use it for two reasons:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;I don't see the need, given the existence of &lt;code&gt;py.exe&lt;/code&gt;. And &lt;/li&gt;
&lt;li&gt;It only works well if you use it exclusively. For instance, if you have previously installed a Python version directly, without using &lt;code&gt;pyenv&lt;/code&gt;, then &lt;a href="https://github.com/pyenv-win/pyenv-win/issues/42"&gt;&lt;code&gt;pyenv&lt;/code&gt; will not detect the installed version&lt;/a&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Using &lt;code&gt;venv&lt;/code&gt; or &lt;code&gt;virtualenv&lt;/code&gt; (or something else)
&lt;/h3&gt;

&lt;p&gt;Since Python 3.3, &lt;code&gt;venv&lt;/code&gt; has been the tool in the Python standard libary for creating virtual environments. For simplicity's sake, like the author of the &lt;a href="https://stackoverflow.com/questions/41573587/what-is-the-difference-between-venv-pyvenv-pyenv-virtualenv-virtualenvwrappe/47559925#47559925"&gt;second answer to this StackOverflow question&lt;/a&gt;, I (and apparently Guido van Rossum) prefer using it.&lt;/p&gt;

&lt;p&gt;You will, however, see lots of references to using &lt;code&gt;virtualenv&lt;/code&gt; for creating virtual environments. It existed before &lt;code&gt;venv&lt;/code&gt; and is apparently your only option if you use Python 2.x. And &lt;code&gt;pyenv&lt;/code&gt; includes even more related tools. These various tools are summarized in the &lt;a href="https://stackoverflow.com/questions/41573587/what-is-the-difference-between-venv-pyvenv-pyenv-virtualenv-virtualenvwrappe/41573588#41573588"&gt;first answer to the StackOverflow question cited above&lt;/a&gt;. I disagree with the answer, but it's full of useful information.&lt;/p&gt;




&lt;p&gt;&lt;a id="f1"&gt;&lt;/a&gt;&lt;sup&gt;1&lt;/sup&gt;For more on Powershell profiles, see &lt;a href="///2022/04/27/windows-dev-setup-part-1.html#f1"&gt;footnote 1 in my post about setting up a Windows dev environment&lt;/a&gt;. ↩&lt;/p&gt;

&lt;p&gt;&lt;a id="f2"&gt;&lt;/a&gt;&lt;sup&gt;2&lt;/sup&gt;Instead of this very clear warning, &lt;a href="https://code.visualstudio.com/docs/python/environments"&gt;the VS Code documentation&lt;/a&gt; says:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: While it's possible to open a virtual environment folder as a workspace, doing so is not recommended and might cause issues with using the Python extension. ↩&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>python</category>
      <category>windows</category>
    </item>
  </channel>
</rss>
