reCAPTCHA WAF Session Token
Information Technology

How to install Python the smart way


Python is easy to use, friendly to the beginner, and powerful enough to create robust software for nearly any application. But like any other piece of software, Python can be complex to set up and manage.

In this article, we’ll walk through how to set up Python the right way. You’ll learn how to pick the appropriate version, how to keep multiple versions from stepping on one another, and how to avoid sharp edges and potential pitfalls along the way.

Choose the right Python version and distribution

At any one point in time, a few different versions of Python are actively receiving updates or at least security fixes. You might think it makes sense to pick the most recent edition of the bunch. However, for the sake of compatibility with third-party modules, it is always safest to choose a Python version that is one major point revision behind the current one.

At the time of this writing, Python 3.12 is the most current release version. The safe bet, then, is to use the latest update of Python 3.11. You can always try out the newest release in a controlled way—more on this later—but going one version back guarantees the best compatibility with common third-party Python packages.

Python, like Linux, also comes in a variety of distributions. Unlike Linux, though, Python offers one, gold-standard, “official” edition you can always fall back on: CPython, the version provided by the Python Software Foundation at Python.org. This is the safest and most broadly compatible distribution, the one nobody gets fired for using. You might also want to investigate other Python distributions that address specific use cases, but we won’t consider them here.

One key choice you may be asked to make, especially on Windows, is whether to use the 32-bit or 64-bit version of Python. The most likely answer is 64-bit, for the following reasons:

  • Most modern operating systems use a 64-bit edition of Python by default. Windows users can run 32-bit editions of Python on 64-bit Windows, but there is a slight performance cost.
  • 32-bit Python, and 32-bit apps generally, can access only 4GB of memory at a time. 64-bit applications don’t have this limit, hence many data analysis and machine learning tools for Python work best in 64-bit incarnations. Some are available only in 64-bit versions.

The only time you should choose the 32-bit version of Python is if you’re stuck with a 32-bit version of Windows, or if you need to use a third-party module that is available only in a 32-bit edition. As this is happening less often with each passing day, 64-bit is the common choice.

How to install Python on Windows

Python installs on Windows in much the same way as any other application, by way of an installer that guides you through the setup process.

By default, the Python installer for Windows places its executables in the user’s AppData directory, so that it doesn’t require administrative permissions. If you’re the only user on the system, you might want to place Python in a higher-level directory (e.g. C:\Python312) to make it easier to find. The Windows installer lets you specify the target directory.

Choose the right Python installer for Windows

Python.org offers various incarnations of Python for Windows. In addition to the 32-bit (“x86”) and 64-bit (“x86-64”) versions already mentioned, you can choose from the embeddable zip file, the executable installer, and the web-based installer. Here’s what those are all about:

  • The executable installer is just an .exe file that runs the setup process for Python. This is the easy default choice, and the most commonly used.
  • The web-based installer is the same as the executable installer, except that it separately downloads the bits needed to perform the install. This dramatically reduces the size of the actual installer, but of course, it requires a network connection.
  • The embeddable zip file is a self-contained, minimal copy of the Python runtime that fits in a single folder with no dependencies. It’s useful to bundle in when you want to distribute a Python app manually, or when you need a quick, one-off Python install to test something on the fly. But the embeddable zip doesn’t include pip or any of the other useful tools that come with a full install, so it’s for expert use only. See my video, How to use the Python embeddable redistribution to make standalone apps, for an example of what you can do with the embeddable redistribution.

Install Python using a Windows package manager

Yet another option is to use one of the Windows package management systems to install Python.

Windows 11 now comes pre-loaded with Microsoft’s own package management solution, Winget. (If you have Windows 10, you can install it manually.) You can install official Python.org editions of Python from winget by specifying a version, e.g.: winget install Python. Python.3.11, or use winget search Python. Python to find out what versions are available. (See my video tutorial for an introduction to using Winget.)

Other Windows package management tools also offer Python. NuGet, the package manager for .NET, offers Python in its repository. However, Python is provided there mainly for the sake of using it as a component in a .NET application, not as a way to install a standalone instance of Python for general use. You will likely find your Python instance easier to manage if you install Python the regular way.

Chocolatey, a more general Windows package management system, offers Python as well. Chocolatey is a convenient way to run the Python installer and track the presence of the Python language runtime in your system—and thus it’s a better choice than NuGet. However, it’s best to avoid mixing and matching Chocolatey installs, winget installs, and manual installs of Python on the same system. It’s best to settle on one package management solution overall and stick with that.

How to install Python on Linux

Because Linux distributions differ significantly, the typical way to install Python on Linux is to use the specific distro’s package manager. Ubuntu and Fedora, for instance, have entirely different procedures for installing Python. On Linux (and macOS), the target directory for the installation is usually predetermined and based on the Python version number, e.g., /usr/bin/python3.X on Linux, or /usr/local/opt/ python/ on the Mac.

Another Linux tool for managing multiple Python installations is pyenv. It lets you install multiple, side-by-side editions of Python and switch between them freely, either systemwide or on a per-project basis. Note that pyenv builds each version of Python you install for your system, so you’ll need Python build dependencies installed beforehand.

One way to avoid dealing with the intricacies of Linux package managers is to use a containerized Python runtime. Containers run isolated from the rest of the system, so you need not worry about different Python runtimes stepping on each others’ toes. However, if your workflow doesn’t already include containers, you’ll need to devote time and energy to getting up to speed with Docker. (Note that you can use containerized Python on Windows as well.)

How to install Python on macOS

Traditionally, macOS has shipped with a version of Python installed, but never more recent than Python 2.7. This created problems when Python 3 arrived, as the two versions often conflicted. The official Python documentation has some notes to this effect, but it doesn’t provide any more detailed recommendations than to make sure you use the right path for the Python instance you want.

A common way to manage Python runtimes on macOS is through the Homebrew package manager, an unofficial (that is, not Apple-created) project that has become something of a de facto standard for package management on the Mac. Homebrew provides a consistent interface for downloading, installing, managing, and removing Python and other third-party command-line apps.

Python installation tips and traps to avoid

We’ve discussed the basics of installing Python on Windows, Linux, and macOS. Let’s conclude with three common installation scenarios that require a little more finesse. 

How not to install Python packages (and what to do instead)

Once you have a base install of a Python version set up, don’t start installing packages directly into it with pip—no, not even if you plan on using Python for just one project. Set up your project directories, install Python virtual environments into them, and then install packages into those virtual environments. This way, the base installation stays clean.

For a high-level way to manage multiple projects with virtual environments and dependencies, look into the Poetry project. Poetry provides a command-line tool for managing virtual environments and dependencies at a high level.

How to install multiple Python versions side-by-side

The single hardest issue when dealing with Python installations is how to handle different versions of Python installed side-by-side. Two universal rules of thumb apply here:

  • Always install each version in a different directory.
  • Ensure that any system paths are configured to point first to the version you want to run by default.

Running multiple Python versions argues strongly in favor of per-project virtual environments. When the virtual environment is activated, all Python activity within the context of the project is automatically directed towards the right version of Python.

Another option for Windows users is the py launcher app, which lets you control which Python version to use when multiples are installed. During Python setup, you’re offered the option to install the py launcher, a small executable that lets you select (via command-line flags) which version of Python to use for a given script. For instance, to run pip for Python 3.11, you would enter py -3.11 -m pip.

How to upgrade your Python version in a virtual environment

Minor revision upgrades for Python—e.g., Python 3.11.2 to Python 3.11.3—are generally easy enough. On Windows, the installer detects the presence of the existing version and upgrades it. On Linux and macOS, the installer or package manager typically does the same thing.

However, any virtual environments you have created will also need upgrading; they don’t upgrade automatically. To upgrade Python in a virtual environment, simply navigate to the virtual environment directory and enter venv --upgrade. Again, note that this works best only for minor point revision upgrades.

If you’re performing a major point revision upgrade, such as Python 3.11 to Python 3.12, your best bet is to use venv to create a new, separate virtual environment subdirectory in the project directory, reinstall any dependencies into it, and switch to using the new virtual environment. Most IDEs with Python support (e.g., Microsoft Visual Studio Code) will detect multiple virtual environments in a project and allow you to switch between them.

Copyright © 2024 IDG Communications, Inc.

Leave a Reply

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

Back to top button
WP Twitter Auto Publish Powered By : XYZScripts.com
SiteLock