Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Getting Started with Python and Raspberry Pi (Redirected from Learning Python By Developing Raspberry Pi Applications)

You're reading from  Getting Started with Python and Raspberry Pi (Redirected from Learning Python By Developing Raspberry Pi Applications)

Product type Book
Published in Sep 2015
Publisher
ISBN-13 9781783551590
Pages 200 pages
Edition 1st Edition
Languages
Author (1):
Dan Nixon Dan Nixon
Profile icon Dan Nixon

Chapter 5. Packaging Code with setuptools

In this chapter, we will look at the ways we can use third-party libraries in our Python code and how we can package our own Python modules ready for distribution. The distribution can be done in a variety of ways including via the popular pip repository, which we will take a quick look at.

The following topics will be covered in this chapter:

  • Using packages in your Python code

  • Packaging your own Python modules

Using packages in your Python code


We will first have a look at the ways in which third party code and libraries can be downloaded, installed, and included in the Python scripts and application we write.

Importing modules

As we have already seen in several of the examples used so far, Python libraries (modules) are used in code by importing them using the import statement. However, so far, we have only been importing the entire modules. For example, import threading imports the entire threading module and to use, say, the Thread class, you would have to specify it as threading.Thread.

Python also allows you to import single classes, and even functions, using a slightly modified syntax, as shown next:

from threading import Thread
from time import time

This preceding code imports just the Thread class and the time function from the threading and time modules respectively. Now when you want to use either within the rest of the file, you would only need to specify either Thread or time, rather than...

Packaging your own Python modules


Now that we have seen how the Python packages can be downloaded and installed, we will look at how they can be created from our own modules. For now, we will only look at how they are packaged and leave out the process of publishing it to a repository.

Packaging a library

We will first look at how to package a library that can be imported by other Python scripts and applications.

We will start with a copy of the calculator module that we created in Chapter 3, Working with Data Structures and I/O.

  1. First, create a new directory named calcpy and move the calculator directory inside it. This calcpy directory will be the packaged library.

  2. Now, create an empty Python file named __init__.py inside the calcpy directory using the following command. This file will tell Python that the calcpy directory should be treated as a module.

    touch __init__.py
    
  3. Next, create another directory called calcpy that contains the calcpy directory created in the previous step. This directory...

Summary


In this chapter, we first learnt about the most common ways to install third party libraries and how to then use them in our own Python code. Doing this greatly broadens the range of functionality that becomes available to your Python applications.

We then looked at ways that we can package our own Python libraries and application, using the setup tools module ready for distribution to other devices.

In the next chapter, we will start to focus on some of the Raspberry Pi specific side of Python as we start to use the gpio library to access and control the GPIO header on the Raspberry Pi.

lock icon The rest of the chapter is locked
You have been reading a chapter from
Getting Started with Python and Raspberry Pi (Redirected from Learning Python By Developing Raspberry Pi Applications)
Published in: Sep 2015 Publisher: ISBN-13: 9781783551590
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}