Setting up Python on Windows

There are various methods to arrange Python on Home windows—too many methods. In keeping with the Zen of Python:

There must be one—and ideally just one—apparent solution to do it.

On this spirit, I describe right here what I feel is the best solution to arrange Python on Home windows, the place “easiest” means:

  1. utilizing pure Python, with no additional packages;
  2. enabling a number of variations of Python to exist side-by-side, with out clobbering one another; and
  3. utilizing no magic, that means the consumer is in management and might perceive all the pieces (that is associated to 1 above);

Assuming you may have by no means put in Python, the fundamental steps are:

  1. disable the Home windows Python installer;
  2. obtain Python—as many variations as you need—from;
  3. set up every model in your consumer listing, with the Python launcher, however with out including something to your path; and
  4. utilizing py.exe, the Python launcher built-in to Python on Windows, inside every of your initiatives, create a digital surroundings as a subfolder within the venture folder, specifying the Python model wanted.

I conclude with some recommendations on utilizing VS Code and a dialogue of other strategies of organising Python on Home windows.

Step 1: Disable the Home windows Python installer

Should you kind python or python3 at a command immediate in a brand new Home windows 10 set up, you land at a hyperlink on the Microsoft Retailer for putting in Python. That is presupposed to make issues straightforward for newbies. It type of does, however on the expense of doubtless making it more durable to handle a number of Python variations. Set up by way of the Microsoft Retailer smells like magic to me, so I do not suggest it.

To disable this habits, go to Begin→Handle App Execution Aliases and flip off the toggles for App Installer – python.exe and App Installer – python3.exe, like so:

Step 2: Obtain Python from

To keep away from magic, download Python directly. You’ll be able to set up as many variations as you want. The order of set up would not actually matter, although (as mentioned under) it could have an effect on the habits of py.exe, the Python launcher.

Step 3: Set up Python in consumer listing, with launcher

By default, once you set up Python with an installer from

  1. Python shall be put in in a user-specific listing fairly than a system listing,
  2. Python will set up the Home windows launcher, py.exe, for all customers (in C:Home windows), and
  3. Python is not going to change your system path.

Settle for these defaults. The set up display ought to appear like this:

screenshot of Python 3.10 installation

After you choose Set up now, the subsequent display will ask if you wish to Disable path size restrict in order that your path can exceed the system’s 260-character restrict. I think that is pointless, since we’re intentionally not including Python to the trail. However I am undecided there is a draw back. Use your judgment.

Be aware: The subsequent time you put in some model of Python, the Set up launcher for all customers choice will in all probability be grayed out, as a result of the launcher will already be put in. If it is grayed out, that is wonderful. If it is not grayed out, go away it chosen, and the launcher shall be up to date.

Step 4: Use py.exe to create digital environments

The Python launcher py.exe lets you simply work with a number of totally different variations of Python. Primary instructions (executed at a command immediate):

  • py — launches the newest model of Python you may have put in.

    • There is a tiny little bit of magic right here. Whenever you set up a more recent model of Python, the launcher will automagically replace itself in order that when py is run with no arguments, it launches the most recent model of Python.
    • You’ll sometimes not use this by itself, as a result of you can be utilizing py to create digital environments, fairly than to run Python exterior of an surroundings.
  • py --list — reveals you all put in variations of Python
  • py --help (or py --help | extra to get data a web page at a time) — offers command-line switches for py and for python (which is known as by py)
  • py -X.X (as an example, py -3.10, or py -3.9) — launch model X.X of Python.

    • With no arguments, this launches the Python REPL. With arguments, that is how we execute modules or scripts. It is the important thing to creating digital environments.

Creating digital environments with py.exe and venv

  • At a command immediate, create a venture listing and navigate into to it. For instance (right here > is the immediate):
> cd C:my_projects
> mkdir project1
Enter fullscreen mode

Exit fullscreen mode

  • Inside your venture listing, use py to create a digital surroundings, specifying the python model and the listing title. For instance:
> cd C:my_projectsproject1
> py -3.9 -m venv p1_venv
Enter fullscreen mode

Exit fullscreen mode

  • Issues to notice:
    • You’ll be able to substitute any put in model for -3.9 on this assertion (you needn’t specify the minor model).
    • The surroundings title p1_venv is arbitrary.
    • This may create a subfolder p1_venv throughout the C:my_projectsproject1 father or mother folder.
    • This construction—a top-level venture folder with a digital surroundings in a subfolder—is the popular construction for doing Python growth with VS Code.
    • Lots of people use venv or .venv for the surroundings title. I dislike this conference as a result of the title venv or .venv is completely uninformative—it reveals up in your immediate to inform you that some digital surroundings is activated, however not which one.
    • There may be different good causes for at all times utilizing venv or .venv to call a digital surroundings. Maybe it makes different setup duties simpler. However I’ve by no means seen anybody clarify the advantages (in the event that they exist).

Utilizing the digital surroundings

In case your digital surroundings is at C:my_projectsproject1p1_venv, activate it as follows:

> cd C:my_projectsproject1
> .p1_venvScriptsactivate
Enter fullscreen mode

Exit fullscreen mode

You must now see the digital surroundings title earlier than the immediate, like so (assuming your working listing can also be proven on the immediate):

(p1_venv) C:my_projectsproject1>
Enter fullscreen mode

Exit fullscreen mode

To deactivate, simply enter deactivate on the immediate.

Utilizing Powershell aliases to simplify utilizing digital environments

Creating Powershell aliases could make utilizing digital environments simpler. Constructing on the instance above, you may put one thing like this in your Powershell profile (a user- and host-specific profile is at $profile.CurrentUserCurrentHost and is known as Microsoft.PowerShell_profile.ps1 wherever it is discovered1):

# create perform and alias that calls perform
# p1 will change to venture listing and activate surroundings

Operate Use-project1 {
  cd "C:my_projectsproject1" 

Set-Alias p1 Use-project1
Enter fullscreen mode

Exit fullscreen mode

Utilizing VS Code with Python digital environments

The VS Code Python extension is designed to work nicely with digital environments, however I discovered the documentation complicated. Whereas answering my own question on StackOverflow, I found out what I feel is an efficient workflow. This assumes you may have already created your digital surroundings in a subfolder inside your venture listing:

  1. On the command immediate, navigate to your venture listing (the listing that incorporates your digital surroundings). (You may create an alias for this navigation in the event you like.)
  2. From there, execute code .

This may open VS Code, utilizing your venture listing as a workspace. Should you create or open a Python file within the workspace, VS Code will robotically detect and activate the digital surroundings within the subfolder within the workspace (i.e., venture listing).

You may be tempted to make use of a digital surroundings as a venture listing, that’s, as the foundation of your workspace. Do not do it. The VS Code documentation ought to say2):

Be aware: Your Python digital surroundings ought to at all times be a subdirectory inside a VS Code workspace. Opening the virtual-environment folder immediately, as the foundation of the workspace, may trigger issues.

You can create a Python digital surroundings inside a workspace through the use of VS Code’s built-in terminal. However I desire to create the surroundings first, exterior of VS Code, as a result of that approach, I do know precisely what’s taking place.

If for some motive VS Code does not detect your digital surroundings, you possibly can manually direct VS Code to it by opening the command palette with Ctrl-Shift-P, getting into Python: Choose Interpreter, and navigating to python.exe discovered within the Scripts subfolder of the digital surroundings. You must solely want to do that as soon as.

Various strategies for organising Python on Home windows

The pyenv venture

There’s a entire venture, pyenv for Windows, particularly designed to will let you handle a number of variations of Python on Home windows. It is a formidable piece of labor, and if it pursuits you, take a look at a very detailed Real Python tutorial on utilizing it.

I desire to not use it for 2 causes:

  1. I do not see the necessity, given the existence of py.exe. And
  2. It solely works nicely in the event you use it solely. As an example, when you’ve got beforehand put in a Python model immediately, with out utilizing pyenv, then pyenv will not detect the installed version.

Utilizing venv or virtualenv (or one thing else)

Since Python 3.3, venv has been the device within the Python customary libary for creating digital environments. For simplicity’s sake, just like the creator of the second answer to this StackOverflow question, I (and apparently Guido van Rossum) desire utilizing it.

You’ll, nonetheless, see a number of references to utilizing virtualenv for creating digital environments. It existed earlier than venv and is seemingly your solely choice in the event you use Python 2.x. And pyenv contains much more associated instruments. These varied instruments are summarized within the first answer to the StackOverflow question cited above. I disagree with the reply, nevertheless it’s filled with helpful info.

1For extra on Powershell profiles, see footnote 1 in my post about setting up a Windows dev environment.

2As an alternative of this very clear warning, the VS Code documentation says:

Be aware: Whereas it is potential to open a digital surroundings folder as a workspace, doing so isn’t beneficial and may trigger points with utilizing the Python extension.

Add a Comment

Your email address will not be published. Required fields are marked *