Python Hands-on

How to get started with Python

How to pick the right Python distribution, the right Python IDE, and the right supporting tools to jumpstart your Python programming

Python Hands-on

Show More
1 2 Page 2
Page 2 of 2

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.

Copyright © 2017 IDG Communications, Inc.

1 2 Page 2
Page 2 of 2