Get startedGet started for free

Installing your own package

1. Installing your own package

By this stage, you have completed the source code for your package. But there's one more step before your local copy of the package is ready for use. You need to install it.

2. Why should you install your own package?

Previously, when you imported your package in the example script, the script and the package directory were both in the same folder. Like this example.

3. Why should you install your own package?

However, if you move the script and the package apart, you will no longer be able to import your package. This is because the script can search for packages in its parent directory, but it won't search outside that. But if you install it, you can import the package no matter where the script is located. Just like you can always import NumPy, or any other package, when it is installed.

4. setup.py

To make the package installable, you are going to add a new file to the package - the setup-dot-py script. This script also contains metadata for the package, and will be important later on if you want to publish your package.

5. Package directory structure

To add the setup script, we need to restructure our directory slightly. The setup script is part of the package, but not part of the source code.

6. Package directory structure

Therefore we create a new folder inside the package directory to keep the source code. It's very common to name the inner and outer folders the same thing. Here the outer package directory is called my-sklearn, and inside this directory there is the directory of source code, also called my-sklearn.

7. Package directory structure

The outer directory also contains the setup script. Doing this keeps the source code separate from extra files the package needs. We'll be adding more of these files later in this chapter.

8. Inside setup.py

Inside the setup-dot-py script, we import the setup function from the setuptools package. The only thing we need to do in this script is call this setup function. Here we call the function and pass in metadata for the package, including author name, a short description for the package, the package name and the package version number. The version number has thee parts, the major number, minor number, and patch number. As you develop the package you will increment these numbers. You increase the patch number for bug fixes and improvements to the functions that already exist, increase the minor number for new features, and increase the major number for really big changes.

9. Inside setup.py

From setup-tools you also need to import the find-packages function. This is used to find the paths of your packages and subpackages. Here we use the find-packages function and pass the result into the setup function. We tell the find-packages function to include the my-sklearn package. my-sklearn-dot-star tells the function to include all the subpackages inside my-sklearn as well.

10. Editable installation

Once you have written the setup script you can install the package. You should navigate to the directory containing the setup file, and then you can use pip to install the package like this. Pip runs the setup-dot-py script for you. The dot at the end of this command means install the package in the current directory. The dash-e means to install this package in an editable mode. This means when you make changes to the source code, like fixing a bug or adding new features, these changes are included when you import the package. If you didn't do this, you would need to reinstall the package each time you make a change.

11. Let's practice!

Okay, time to make your package installable. Let's practice!

Create Your Free Account

or

By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.