Reader small image

You're reading from  NumPy Essentials

Product typeBook
Published inApr 2016
Reading LevelIntermediate
Publisher
ISBN-139781784393670
Edition1st Edition
Languages
Tools
Right arrow
Authors (3):
Leo (Liang-Huan) Chin
Leo (Liang-Huan) Chin
author image
Leo (Liang-Huan) Chin

Leo (Liang-Huan) Chin is a data engineer with more than 5 years of experience in the field of Python. He works for Gogoro smart scooter, Taiwan, where his job entails discovering new and interesting biking patterns . His previous work experience includes ESRI, California, USA, which focused on spatial-temporal data mining. He loves data, analytics, and the stories behind data and analytics. He received an MA degree of GIS in geography from State University of New York, Buffalo. When Leo isn't glued to a computer screen, he spends time on photography, traveling, and exploring some awesome restaurants across the world. You can reach Leo at http://chinleock.github.io/portfolio/.
Read more about Leo (Liang-Huan) Chin

Tanmay Dutta
Tanmay Dutta
author image
Tanmay Dutta

Tanmay Dutta is a seasoned programmer with expertise in programming languages such as Python, Erlang, C++, Haskell, and F#. He has extensive experience in developing numerical libraries and frameworks for investment banking businesses. He was also instrumental in the design and development of a risk framework in Python (pandas, NumPy, and Django) for a wealth fund in Singapore. Tanmay has a master's degree in financial engineering from Nanyang Technological University, Singapore, and a certification in computational finance from Tepper Business School, Carnegie Mellon University.
Read more about Tanmay Dutta

Shane Holloway
Shane Holloway
author image
Shane Holloway

http://shaneholloway.com/resume/
Read more about Shane Holloway

View More author details
Right arrow

Chapter 7. Building and Distributing NumPy Code

In a real-world scenario, you will be writing an application with the intentions of distributing it to the World or reusing it on various other computers. For this purpose, you would like your application to be packed in a standard way so that everyone in the community understands and follows. As you will have noticed by now, Python users mainly use a package manager called pip to automate the installation of modules created by other programmers. Python has a packaging platform called PyPI (Python Package Index), which is an official central repository for more than 50,000 Python packages. Once the package is registered in PyPi aka Cheese Shop, other users across the world can install it after configuring it with package management systems such as pip. Python comes with a number of solutions to help you to build your code ready for distribution to the Cheese Shop and, in this chapter, we will focus on two such tools, setuptools and Distutils...

Introducing Distutils and setuptools


Before we begin, first let us understand what these tools are and why we prefer one over another. Distutils is a framework that comes by default with Python, and setuptools builds over the standard Distutils to provide enhanced functionalities and features. In a real-world scenario, you will never use Distutils. The only case where you might want to use Distutils alone is where setuptools is unavailable. (A good setup script should check for the availability of setuptools before proceeding.) In most cases, users will be better off installing setuptools as most packages nowadays are built over them. We will be using setuptools for building Cython code in the following chapters; hence, for our purpose, we will be installing setuptools now and using it extensively from now on.

Next let us start by installing the required tools to build our first dummy (but working) installer. After we have got our installer working, we will dive into more functionalities...

Preparing the tools


To install setuptools on your system, you need to first download ez_setup.py on your system from https://pypi.python.org/pypi/setuptools and then execute this from your command prompt as follows:

    $ python ez_setup.py

To test the installation of setuptools, open the Python shell and type the following:

> import setuptools 

If the preceding import does not give any error, then we have successfully installed setuptools.

Building the first working distribution


All of the tools we mentioned previously (setuptoolsDistutils and numpy.distutils) are centered around the function setup. To get an understanding of most packaging requirements, we will look into a simple setup function and then study a full-fledged installer. To create a basic installer, we need to call the setup function with metadata about the package. Let's call our first package py_hello, which has just one function greeter, and just prints a message when called. The package can be downloaded from the Bitbucket repository at https://bitbucket.org/tdatta/books/src/af376df081ef/python/simple_setup/?at=master The project directory structure for the project looks like the following:

py_hello 
├── README 
├── MANIFEST.in 
├── setup.py 
├── bin 
│   └── greeter.bat 
└── greeter 
    ├── __init__.py 
    ├── greeter.py 

Let's look at some standard files here:

  • README-This file is used to store information...

Testing your package


It is very important that the package you are building should work/install without any problem on a user's computer. Hence, you should spend time testing the package. The general idea behind testing installation is to create a virtualenv and try to install the package or use another system altogether. Any error encountered at this stage should be removed and the author should try to make sure that the exceptions are easier to follow. Exceptions should also try to provide the solution. Common mistakes at this stage would be:

  • Assumptions about preinstalled modules and libraries.
  • Developers may forget to include dependencies in the setup file. This mistake will be caught if you use a new virtualenv to test the installer.
  • Permissions and elevated rights requirements.
  • Some users may have read-only access to the computer. This could be easily overlooked as most developers do not have this case in their own machines. This problem should not occur if the provider of the package...

Distributing your application


Once all the development for your module/application is complete and you are ready with your complete working application and setup file, the next task will be to share your hardwork with the World to benefit others. The steps to release it to the world by using PyPI are quite straightforward. The first thing you need to do as an author of a package is to register yourself. You can do it directly from the command line as follows:

    $ python setup.py register
    running register
    running egg_info
    ....
    ....
    We need to know who you are, so please choose either:
     1. use your existing login,
     2. register as a new user,
     3. have the server generate a new password for you (and email it to you), or
     4. quit
    Your selection [default 1]:

Tip

This process will fail if you do not have proper metadata information of any file missing in your setup.py. Make sure you have setup.py working...

Summary


In this chapter, we introduced the tools used for packaging and distributing applications. We first looked at a simpler setup.py file. You looked into the attributes of the function setup and how these arguments link up to the final installer. Next we added-NumPy related code and added some exception handling code. Finally, we built the installer and learnt how to upload it on the Cheese Shop (the PyPI website). In the next chapter, you will be looking at ways to further speed up your Python code by converting parts of it into Cython.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
NumPy Essentials
Published in: Apr 2016Publisher: ISBN-13: 9781784393670
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.
undefined
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 €14.99/month. Cancel anytime

Authors (3)

author image
Leo (Liang-Huan) Chin

Leo (Liang-Huan) Chin is a data engineer with more than 5 years of experience in the field of Python. He works for Gogoro smart scooter, Taiwan, where his job entails discovering new and interesting biking patterns . His previous work experience includes ESRI, California, USA, which focused on spatial-temporal data mining. He loves data, analytics, and the stories behind data and analytics. He received an MA degree of GIS in geography from State University of New York, Buffalo. When Leo isn't glued to a computer screen, he spends time on photography, traveling, and exploring some awesome restaurants across the world. You can reach Leo at http://chinleock.github.io/portfolio/.
Read more about Leo (Liang-Huan) Chin

author image
Tanmay Dutta

Tanmay Dutta is a seasoned programmer with expertise in programming languages such as Python, Erlang, C++, Haskell, and F#. He has extensive experience in developing numerical libraries and frameworks for investment banking businesses. He was also instrumental in the design and development of a risk framework in Python (pandas, NumPy, and Django) for a wealth fund in Singapore. Tanmay has a master's degree in financial engineering from Nanyang Technological University, Singapore, and a certification in computational finance from Tepper Business School, Carnegie Mellon University.
Read more about Tanmay Dutta

author image
Shane Holloway

http://shaneholloway.com/resume/
Read more about Shane Holloway