Python has been 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 it is with any other programming language. Every choice you make will matter—the OS, the Python runtime, and the editor or IDE you will 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 best choices among development environments, and supporting tools for your Python workspace, such as code formatters, project templates, and virtual environments. Hopefully, we’ll help you avoid some of the pitfalls that come with putting together a Python programming environment.
The first issue we need to address is whether to use Python 2 or Python 3. Short answer: Use Python 3.
Python 3 is considered the present and future edition of the language. Python 2 is now a legacy version supported mainly for the sake of backwards compatibility. The overwhelming majority of third-party Python libraries support Python 3 at this point, and Python 3 has concurrency features and performance benefits that aren’t available in Python 2. Plus, the differences in syntax between the two versions are fairly minimal. It won’t take long to learn Python 2 if the need ever arises.
Pick 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 of the major operating systems, and so are good Python editors and development environments, so you are free to start with the operating system of your 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, and one of those might be more suitable to your needs. Read the section “Pick a distro” before actually installing anything.
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. If you use the Chocolately package manager for Windows (and why not? it’s a great idea), you can obtain the latest edition of Python by typing
choco install python3 at the command prompt.
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 Linuxes bundle an older version of Python for a good reason. 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: The situation on MacOS is similar to Linux, with a couple of exceptions. First, the only pre-installed version of Python on the most recent version of MacOS (10.11) is Python 2.7.10. If you want to use Python 3, you will need to use Homebrew to install it. You might also want to use pyenv to manage it.
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.
Pick a Python distribution
Python is available by way of a number of different distributions, or repackagings of the language 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 this 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 habitually shipped with a Python interpreter (e.g., binaries for SQLite).
Note that CPython has no technical support available for it directly. 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 with it.
Use case: The absolute beginner who’s not venturing very far, or the true do-it-yourselfer who is not intimidated by having to pick up her own pieces.
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 popular third-party libraries used across a range of disciplines, so it requires far less thumb-wrestling than CPython to get those 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.
It isn’t necessary to shell out cash to use ActivePython. The distro comes in three versions: a free Community Edition (no support), a Business Edition (support, some perks), and an Enterprise Edition (support, legal indemnification, plenty of 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: 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, on the order of 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: 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 math and statistics—engineering, data analysis, machine learning. A few Python distributions have popped up that are 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: Anyone using Python for data analysis or machine learning. Anaconda isn’t limited to those use cases, but it is optimized heavily for them. The Enthought Python Distribution is built the same way, and meant for similar use cases.
Pick a 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:
- Both Microsoft IDEs—Visual Studio and Visual Studio Code—provide excellent Python support through extensions, so anyone with a workflow already built atop those IDEs simply needs to install the appropriate add-ons.
- Eclipse, widely used for Java but also other languages, also supports Python through add-ons. Alternatively, you could use LiClipse, an edition of the Eclipse IDE with various Python-supporting add-ons pre-installed and ready to go.
- Many MacOS users praise the Sublime Text editor for its speed, simplicity, and power, and it too has Python support. Sublime Text even has an integrated Python interpreter, and many of its plug-ins are written in Python, making it easy for Python developers to accentuate their workflow with Sublime Text.
- Vim and Emacs both have strong Python support that can be augmented further. Vim has the python-mode plug-in, and Emacs has basic Python support that can be enhanced with add-ons.
Many dedicated IDEs for Python also exist, covering a variety of use cases:
- IDLE,bundled with CPython, is suitable for tossing together quick scripts and helping those new to the language get their bearings.
- PyCharm, from JetBrains, comes widely recommended, and it’s not hard to see why. It supports a great many tools native to the Python world (e.g., Jupyter notebooks), but also strives to make those tools accessible and easy to work with.
- Komodo, the IDE line produced by ActiveState, can work as a close complement to ActiveState’s own ActivePython, but it works well with CPython, too.
- Spyder is aimed mainly at the science-and-stats crowd, rather than Python software developers generally, but it includes valuable aids like built-in support for Jupyter notebooks.
Pick your 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, 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 those binaries to be built on the fly, but it is less straightforward to do that in Windows.
One partial solution comes by way of Christoph Gohlke of the University of California, Irvine. He provides an unofficial repository of prebuilt Windows binaries for many popular Python packages. Most of the packages are for scientific computing applications or machine learning (e.g., NumPy, TensorFlow), but many cover more general-purpose use cases (e.g., Pillow for image processing, or the asynchronous HTTP server Aiohttp).
If you’re on Windows …
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. 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.
Another third-party addition you might need but is not typically installed by default is the LLVM compiler framework. Some Python projects use LLVM—e.g., Numba, which transforms Python functions into assembly. If you draw on one of these packages, you’ll need to install LLVM yourself or use Anaconda or another Python distribution that has LLVM pre-integrated.
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 Chocolatey.
Keep your Python code clean
Most languages have some way to perform code linting and syntax standardization. In Python, there is a collection of packages that handle 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 tool autopep8 can automate that job. It neatens up Python code to match PEP 8, and returns warnings about specific lines that need developer attention. Most IDEs with Python support can use autopep8 as the default code formatter.
A newer project to apply formatting rules to Python is yapf. Originally developed by Google, this tool reformats documents entirely, removing all existing formatting and reformatting according to PEP 8 rules. One potential pitfall with yapf is that it might remove formatting you want hand-specified—for instance, for data literals—but you can use inline comments to toggle yapf formatting off and on as needed.
Dynamic languages like Python allow developers to introduce subtle bugs, but Python 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 make use of 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 (although don’t hold your breath for that).
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.
Keep Python projects straight with virtual environments
The more Python projects you work on, the more likely you’ll have to use multiple versions of libraries. Example: You’re trying to maintain a legacy project that depends on an older version of something, while at the same time build a replacement that uses newer versions of the same libraries.
Python provides a way to work around this: virtual environments. Virtual environments allow a project folder to have its own local copies of libraries that supersede the versions installed in the Python interpreter. Most of this is managed by way of a command-line tool, virtualenv, that lets you toggle which virtual environment to use at a given moment. Another tool, virtualenvwrapper, further automates the process of creating virtual environments.
Chances are good that Python virtual environments are supported directly in your IDE. PyCharm, for instance, has virtual environment support built in.
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
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.
A number of different projects can help you do this, 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 that have binary modules (e.g. Pandas or NumPy).
Finally, 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.