Book Image

Flask Blueprints

By : Joel Perras
Book Image

Flask Blueprints

By: Joel Perras

Overview of this book

Table of Contents (14 chapters)

Setuptools and pip


When a developer wants to make their code more widely available, one of the first steps will be to create a setuptools-compatible package.

Most of the distributions of a modern Python version will come with setuptools already installed. If it is not present on your system of choice, then obtaining it is relatively simple, with additional instructions available on the official documentation:

wget https://bootstrap.pypa.io/ez_setup.py -O - | python

After setuptools is installed, the basic requirement to create a compatible package is the creation of a setup.py file at the root of your project. The primary content of this file should be the invocation of a setup() function with a few mandatory (and many optional) arguments, as follows:

from setuptools import setup

setup(
    name="My Great Project",
    version="0.0.1",
    author="Jane Doe",
    author_email="[email protected]",
    description= "A brief summary of the project.",
    license="BSD",
    keywords="example tutorial flask",
    url="http://example.com/my-great-project",
    packages=['foobar','tests'],
    long_description="A much longer project description.",
    classifiers=[
        "Development Status :: 3 - Alpha",
        "Topic :: Utilities",
        "License :: OSI Approved :: BSD License",
    ],
)

Tip

Downloading the example code

You can download the example code files from your account at http://www.packtpub.com for all the Packt Publishing books you have purchased. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

Once the package has been created, most developers will choose to upload their newly minted package to PyPI—the official source of nearly all Python packages—using the built-in tools that are provided by setuptools itself. While the use of this particular public PyPI repository is not a requirement (it's even possible to set up your own personal package index), most Python developers will expect to find their packages here.

This brings us to one more essential piece of the puzzle—the pip Python package installer. If you have Python 2.7.9 or greater installed, then pip will already be present. Some distributions might have it preinstalled for you or it might be present in a system-level package. For a Debian-like distribution of Linux, it may be installed via the following command:

apt-get install python-pip

Similarly, other Linux-based distributions will have their own recommended package managers. If you'd rather obtain the source and install it manually, it is a simple matter of fetching a file and running it using the Python interpreter:

$ curl -o get-pip.py https://bootstrap.pypa.io/get-pip.py
$ python get-pip.py

Pip is a tool for installing Python packages (and is itself a Python package). While it is not the only player in the game, pip is by far the most widely used.

Note

The predecessor to pip is easy_install, which has largely been replaced in the Python community by the former. The easy_install module suffered some relatively major problems, such as allowing partially completed installations, the inability to uninstall a package without requiring the user to manually delete the related .egg files, and console output that contained the useful success and error messages that allowed the developer to determine the best course of action in case something went wrong.

One can invoke pip in the command line to install, say, a scientific computing package on the local filesystem:

$ pip install numpy

The preceding command will query the default PyPI index for a package named numpy and download the latest version to a special place in your system, usually /usr/local/lib/pythonX.Y/site-packages (X and Y are the major/minor versions of the Python version that pip points to). This operation may require root privileges and would thus require sudo or similar actions to allow it to be completed.

One of the many benefits of virtual environments, which we will explore shortly, is that they generally avoid the privilege escalation requirement that can plague system-level changes to installed packages.

Once this operation is completed successfully, you now have the ability to import the numpy package into new modules and use any and all of the functionalities that it exposes:

import numpy

x = numpy.array([1, 2, 3])
sum = numpy.sum(x)
print sum  # prints 6

Once we have this package (or any other, for that matter) installed, there's nothing stopping us from fetching additional packages in the usual way. Moreover, we can install multiple packages at the same time by providing their names as additional arguments to the install command:

$ pip install scipy pandas # etc.