How to get started with Python

By Serdar Yegulalp

Python is often described as an easy language—both easy to learn and easy to use. Python makes it easy to develop powerful software for an enormous variety of applications. But it can be as difficult to get started with Python as with any other programming language. Every choice you make will matter. The Python operating system and runtime you use will matter, as will the IDE or editor you use to write your code.

In this article, we’ll walk through the steps needed to get your “sea legs” with Python. We’ll discuss the different Python distributions, the leading development environments, and supporting tools you might want for your Python workspace, such as code formatters, project templates, and virtual environments. Being informed about your options will help you avoid some of the pitfalls that come with putting together a Python programming environment.

Choose a Python version

For many years, Python existed in two parallel incarnations—the older Python 2.x branch, kept alive to support legacy Python applications, and the newer Python 3.x branch. At this point, the only version you should be using as a new Python user is the 3.x series.

New releases for Python come out roughly once per year, typically in October. As of January 2024, for instance, the newest release version is Python 3.12. However, as a general rule, it's often best to go with a version that's one revision behind the current one, the better to ensure maximum compatibility. In this case, consider using version 3.11 instead of 3.12. Some Python libraries lag in compatibility, and won't work with the most recent releases.

Choose your Python platform

How you get started with Python will depend mainly on the operating system you’re planning to use as your development environment. Fortunately, Python is available for all the major operating systems, and so are good Python editors and development environments. Whatever OS you're comfortable with will be a decent choice.

Before you install anything: The instructions in this section are for installing the stock, standard version of Python produced by the Python Software Foundation. Python is available in other editions as well. The section “Pick a distro” goes into detail about this, but the standard version of Python is suitable for most basic use cases.

Microsoft Windows

Python doesn’t ship by default with Windows, but adding Python to Windows is generally no more complicated than downloading a runtime and clicking a few buttons. You can download installers from the Python for Windows release page.

If you use the winget package manager for Windows (which is a good idea), you can obtain editions of Python by typing winget search "Python 3" to find the IDs for specific versions to install. Do not install the versions that list msstore as the source, however—only install the ones that use winget as the source. For instance, to install Python 3.11, you'd use winget install Python.Python.3.11. (See my intro to Winget for more about the Windows package manager.)

Linux

Python is a standard-issue package on just about every major Linux distribution. The question is whether or not you will have the most recent version of Python installed by default. Some Linux distros bundle an older version of Python on purpose. For instance, Python scripts written for that particular distribution may rely on a given version of Python.

If that’s the case, you may need to use the package manager for your distribution to install another version of Python. If you want to avoid clashes with the version already installed, the pyenv project allows you to designate which of multiple installed versions of Python you want to use as the default. It also allows you to set a Python version for a specific project.

macOS

Once upon a time, macOS shipped with the 2.x incarnation of Python. This actually made it more complicated to use Python on macOS, since the system-installed Python was meant more for the OS itself and its tooling than for end users.

Today, macOS doesn't bundle Python by default, so one has to install Python in some form no matter what. The most common and battle-tested way to do this is through pyenv. A common way to install pyenv on macOS is through the Homebrew package management system, using brew install pyenv.

Cross-platform

If you have Docker installed, you might simply obtain a Docker container with a Python runtime and use that as the basis for a project. This is probably the best way to go if you eventually intend to deploy the app in question by way of Docker; you might as well get started on the right foot.

Choose a Python distribution

Python is available by way of different distributions, or repackagings of the language and runtime. Just as different Linux distributions are meant to satisfy different use cases, the different Python distributions appeal to distinct audiences and usage scenarios.

Note that what follows isn’t a definitive list of Python distributions. Many others are available that cover similar use cases, but these are the best-known and most widely used.

CPython

This is the default Python runtime, created by the Python Software Foundation, and the most general-purpose version of Python. In addition to the interpreter and the standard library, it includes a passel of third-party components commonly shipped with a Python interpreter (one example is the SQLite database).

Note that CPython does not come with technical support directly, since it is not provided by a vendor. It does have a built-in mechanism for adding third-party packages, called pip, but larger and more complex packages—especially those for Windows with binary dependencies—are sometimes challenging to get running.

Use case: It's hard to go wrong choosing the stock CPython distribution, although you will have to set up and manage things yourself. That's also a good way to learn how things work in Python from the inside out.

ActivePython

ActiveState markets a whole slew of commercial and enterprise-grade language runtimes and IDEs. ActivePython includes the core CPython runtime and pre-installs dozens of vetted editions of popular third-party libraries used across a range of disciplines. Because of this, it requires less thumb-wrestling than CPython to get things to work. ActivePython also adds performance improvements for many of Python’s math and science libraries by way of the Intel Math Kernel Library.

ActivePython used to be offered in a free community edition with no support channel, but that has been phased out in favor of a free tier on the ActiveState Platform service. Paid versions provide tech support, local installs of ActivePython, support for end-of-life Python editions, indemnification, and many other bonuses. Prospective enterprise users can start from the bottom of that pyramid and work their way up to see if ActivePython is a good fit for their projects.

Use case: This distro is ideal for enterprise users who want hands-free access to common third-party libraries and who may want paid support.

PyPy

A drop-in substitute for CPython, PyPy’s big distinction is that it accelerates Python applications by way of a JIT (just-in-time) compiler. Some of the speedups can be dramatic, up to multiple orders of magnitude. However, the performance improvements will manifest most prominently in long-running applications, rather than, say, automation-style scripts that execute and quit.

Use case: PyPy is a great choice for developers of long-running services where Python’s dynamism becomes useful, and where C extensions aren’t used as much.

Anaconda

One of Python’s big use cases is for math and statistics—think engineering, data analysis, and machine learning. Of the Python distributions devoted to those use cases, Continuum Analytics’s Anaconda is among the best-known and most widely used. Like ActivePython, it bundles many common Python libraries for math and statistics and uses the Intel-optimized versions of math libraries wherever possible. Anaconda also provides its own installer for managing third-party libraries, making it easier to keep those packages up to date by managing their binary dependencies.

Use case: Anaconda will be of interest to anyone using Python for data analysis or machine learning. It isn’t limited to those use cases but is heavily optimized for them.

Choose your Python IDE

In theory, you don’t need anything more than a text editor and a Python runtime to develop software in Python. In practice, an IDE with Python support comes in mighty handy when developing anything more than a trivial script.

If you are already developing software with an IDE, you can probably use it for your Python programming, too. Most popular IDEs have robust Python support:

Many dedicated IDEs for Python also exist, covering a variety of use cases:

Compare Python IDEs on InfoWorld

For reviews of the leading Python IDEs, see our Python IDE roundup, followed by seven more you might have missed.

A word about Python packages

If you’re using a distribution like ActivePython or Anaconda, many common third-party Python libraries are either preinstalled or made available by way of a tool for obtaining more packages from the IDE maker’s own repositories. If you’re using CPython or another distribution that doesn’t provide these kinds of conveniences, adding third-party libraries is a little trickier.

The Python Software Foundation maintains a massive repository of third-party libraries, known as the Python Package Index (PyPI). Any package in PyPI can be added to a Python installation by way of the pip command-line tool. However, some packages require binaries built for specific platforms, and not every package in PyPI has binaries for every platform. Overcoming this problem isn’t difficult in Linux, which generally allows building those binaries on the fly, but doing it in Windows is less straightforward.

Python for Windows users

For certain kinds of Python projects, Windows users will need to do a little more manual lifting than users of Linux, macOS, and other Unix-based systems. One commonly missing ingredient that must be supplied by the Windows developer is a C compiler. For some packages, Python will need a C compiler to build certain modules when those modules aren’t provided in binary format. Cython, which translates Python into C code, needs a C compiler to produce working binaries.

The good news is that Microsoft’s own C compiler can be obtained and installed for free by way of Microsoft Visual Studio Community Edition. The easiest way to get the needed pieces is to install the Visual Studio Build Tools, the command-line components used by Visual Studio to perform actual compilation. You can install those with the winget package manager: winget install Microsoft.VisualStudio.2019.BuildTools (for the 2019 edition), or winget install Microsoft.VisualStudio.2022.BuildTools (for the 2022 edition).

It is also possible to install GCC (the Gnu Compiler Collection) by way of a project like Cygwin or MSYS2. However, because the Visual Studio C compiler is used to build CPython on Windows, using Visual Studio guarantees more consistency between binaries.

Finally, Windows doesn’t include revision control software, such as Git. IDEs that integrate with Git will balk if they don’t find it. You can obtain Git for Windows manually or install it by way of Winget (winget install Git.Git).

Tools for your Python workspace

Once you have your basic Python workspace set up, there are a few additional, supporting tools you'll want to consider. We'll look at those next.

Keep your Python code clean

Most programming languages have some way to perform code linting and syntax standardization. In Python, a collection of packages handles this work. All of the major IDEs have some manner of integrated support for them, so they’re worth adding to your workspace.

The rules governing Python code formatting are codified in a document named PEP 8. If you want to apply those rules to a given codebase, the tools autopep8 or black can automate that job. Most IDEs with Python support can use either of those as default code formatter. black has become the default choice for many, as it has little to no configuration, so the results are highly consistent across codebases and between developers.

Dynamic languages like Python make it easier for developers to introduce subtle bugs, but Python also has support tooling to help defray those problems. Pylint, for instance, has long been one of the common code-linting tools for Python. Among other things, it can use the optional type-hinting functionality that was introduced in Python 3.5 to check for type mismatches. Another project, Mypy, focuses exclusively on type checking. In time, Mypy could become a way for Python to be more efficiently compiled to native code (but don’t hold your breath for that).

More recently, Microsoft has created its own type checker, Pyright, and linting tool and language server Pylance. Both of these tools integrates into the Visual Studio Code.

Create Python project templates

If you find yourself recreating the same types of projects over and over, save yourself some hassle and create a template for the project. One way to do this is to create a Git repository for a blank project, revise it incrementally over time (for instance, as newer versions of libraries become available), and create branches or tags for each revision. Then you can instantiate a new project simply by cloning the repository.

With Python, you can take this a step further by way of the Cookiecutter project. New Python projects can be bootstrapped with a Cookiecutter template, and those templates can be stored in Git and cloned on demand. There’s a good chance a Cookiecutter template is available to kickstart your next project, and you can always share out a created template of your own.

Virtual environments

The more Python projects you work on, the more likely you’ll have to use multiple versions of libraries. As an example, you might be trying to maintain a legacy project that depends on an older version of something, while at the same time building a replacement that uses newer versions of the same libraries.

Python provides a way to work around this: virtual environments. Python virtual environments allow a project folder to have its own local copies of libraries that supersede the versions installed in the Python interpreter.

Chances are good that Python virtual environments are supported directly in your IDE. PyCharm, for instance, has virtual environment support built in. The Python extension for Visual Studio Code is similarly equipped.

Another alternative to creating virtual environments is to use a standalone installation of the Python runtime. CPython for Windows, for instance, can be obtained in an “embeddable zip file” format, which when unpacked provides a minimal installation of Python in its own subdirectory. If you are testing something against multiple versions of the Python runtime and you don’t want to formally install each version and switch between them, this is one way to accomplish that.

Using a self-contained Python installation comes with a few downsides. For one, many of the features found in a full Python deployment aren’t available by default, such as the Tkinter UI toolkit. (SQLite, though, is included.) Also, tools like pip won’t be readily available; you’ll have to provide any third-party packages manually, or at least set the PYTHONPATH environment variable (which specifies where packages can be found) to include a path to those packages.

Share your Python app

If you create a Python project that you think will be useful to others, look into how to package the project so it can be redistributed on PyPI. The first time you do this, it may be a little awkward, as you may find the layout of your project needs to be reworked to conform to PyPI packaging standards. But it is well worth learning about, because making a project readily available through PyPI aids adoption and makes it easier to garner feedback from users.

Python doesn’t provide a native way to produce a stand-alone executable or self-contained copy of a Python program, but third-party libraries have stepped up to fill the gap. They do this by bundling the application with a copy of the Python runtime.

There are various projects for repackaging Python apps, but among the best-known and most aggressively developed is PyInstaller. PyInstaller stands out with the ability to package up apps that make use of many common third-party libraries for Python, even those with binary modules such as Pandas or NumPy.

© Info World