Create Packages Smoothly With Python Setuptools

The Python community has contributed vastly to the versatility and popularity of the language. The open-source approach to Python has allowed programmers worldwide to share their developments. Specific projects that help out all Python developers have become immensely popular in the community. This ability to share your projects easily as packages and help out Python’s user base has contributed to the immense growth in the language over the decades.

Python setuptools is a module that deals with the Python package development process. It is a library that is designed to allow packing Python projects in a simplified manner.

About the Module

This module is external. Therefore it is NOT a part of the Python standard library. You can install it manually by using PIP or its setup.py file.

Installing via PIP

Ensure that you have the latest version of PIP and Python installed through these commands –

python -version
pip -version

To prevent any issues with the module, upgrade to the latest version of PIP.

pip install –upgrade pip

Finally, you can install Python setuptools.

pip install setuptools

Installing via Setup.py

Download the latest setup file for Python setuptools here.

Furthermore, running the following command will initiate the extraction process.

tar -xzvf setuptools-60.2.0.tar.gz

After extraction, access the package file and start the installation process.

Accessing the package
cd setuptools-60.2.0

Installing the package
python setup.py install

Creating a PIP Installable Module Using Setuptools

Configuring setuptools.setup()

As far as we can tell, this is an essential function of the module.

Let’s make our own package. For this example, I have taken a function that determines whether the provided number is odd or even.

def oddEven(number):
    if number == 0:
        print("neither")
    else:
        if number%2 == 0 :
            print("even")
        else:
            print("odd")

We will be packaging this module and converting it into an installable module.

First off, we will be invoking .setup() for packaging our module.

Setuptools.setup() install_requires Argument

While working on a project in Python, you are bound to use the help of some external modules that are not available in the standard library. In packaging that library, you want to ensure that the installer also has those external dependencies installed. Therefore, when the user installs your project package, the argument will automatically call in a “PIP install” command for the modules specified in the argument.

Setuptools.setup() console_scripts Argument

Modules can register entry points through setuptools that can be hooked into other packages to provide certain functionality. console_scripts provided by the module allows such entry points. This argument allows Python functions to be accessed as tools in the command line. Refer to the following syntax:

setup(
    ...
    entry_points = {
        'console_scripts': ['myCommand=myCommand.command_line:main'],
    }
    ...
)

setup.py

import setuptools
setuptools.setup(
    # Includes all other files that are within your project folder 
    include_package_data=True,
 
    # Name of your Package
    name='oddeven',

    # Project Version
    version='1.0',
    
    # Description of your Package
    description='Check if your number is odd or even',
    
    # Website for your Project or Github repo
    url="https://github.com/myGitHubRepo",

    # Name of the Creator
    author='Author Name',

    # Creator's mail address
    author_email='[email protected]',
    
    # Projects you want to include in your Package
    packages=setuptools.find_packages (),
   
    # Dependencies/Other modules required for your package to work
    install_requires=['pandas', 'numpy'],

    # Detailed description of your package
    long_description='This module provides one function that determines whether the provided number is either odd, even, or neither (in the cases of 0)',

    # Format of your Detailed Description
    long_description_content_type="text/markdown",
     
    # Classifiers allow your Package to be categorized based on functionality
    classifiers = [
        "Programming Language :: Python :: 3",
         "Operating System :: OS Independent",
    ],
)

We must also provide a command for our module to be installed via PIP. Hence, this command file:

commands.txt

pip install .

Make sure setup.py and commands.txt are located parallel to your project. It is essential that they’re not present together in the same folder. Your file hierarchy should look something like this:

file-hierarchy

Packaging and Installing our Module

After setting up your files, open up your terminal and run:

pip install .

or

pip3 install .

Output

Installing self package output

Observe the above output. The highlighted line indicates that our module has been built.

Common Errors on Setuptools

Sudo PIP Install/Upgrade Error

Let’s look at the following exception raised:

Installing collected packages: setuptools
  Found existing installation: setuptools 1.1.6
    Uninstalling setuptools-1.1.6:
Exception:
Traceback (most recent call last):
  File "/Library/Python/2.7/site-packages/pip-8.1.1-py2.7.egg/pip/basecommand.py", line 209, in main
    status = self.run(options, args)
.....,
......
......
Error: [('/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/_markerlib/__init__.py', '/tmp/pip-rV15My-uninstall/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/_markerlib/__init__.py', "[Errno 1] Operation not permitted: '/tmp/pip-rV15My-uninstall/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/_markerlib/__init__.py'"), ('/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/_markerlib/__init__.pyc', '/tmp/pip-rV15My-uninstall/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/_markerlib/__init__.pyc', "[Errno 1] Operation not permitted: '/tmp/pip-rV15My-uninsta...

This error is most likely due to the fact that PIP did not get certain privileges to upgrade the module. A simple solution is to manually install/upgrade the module. Try the following command.

pip install --upgrade setuptools --user python

Adding Data Files in setuptools using data_files

Additional files, such as configuration files, message catalogs, data files, and anything else that does not fit in one of the previous categories, can be specified with the data_files option.

data_files specifies a sequence of (directoryfiles) pairs in the following way:

setup(...,
      data_files=[('bitmaps', ['my.gif', 'alsomy.gif']),
                  ('config', ['config/data.cfg']),
     )

In the sequence, each pair (directory, files) specifies the installation directory and the files to be installed there.

In files, each file name is interpreted relative to the setup.py script at the top of the package source distribution. The data files can be installed in the directory you specify, but they cannot be renamed.

What’s the difference between Python setuptools Vs. distutils?

disutilssetuptools
distutils is a very old packaging library provided since Python 2. It is included In the Python Standard Library under the Software Packaging and Distribution category.setuptools is a more recent module that provides package creation functionalities. However, it is not included in the Python Standard Library.
It’s is a solid module for distributing simple Python Packages.It introduced a command-line utility called easy_install. It also introduced the setuptools Python package that can be imported into your setup.py script.
It lacks certain features that increase the necessity for a more updated packaging module.Due to the lack of features in Python distutils, setuptools was developed.

What is the difference between easy_install and setup.py (pip package creation) ?

easy_installsetup.py
It does not provide the ability to uninstall the packageProvides commands to uninstall specific packages.
Does not support PEP 438Supports PEP 438 for file hosting on PyPI
Does not install packages from wheel distributionAllows installation from Wheel distribution
Installs in Encapsulated Egg formatInstalls ‘Flat’ packages with egg-info metadata.
Allows you to install various versions of the packageOnly the latest version is available for installation.

FAQs on Python Setuptools

How can I make Jenkins run “pip install”?

A child bash process sets environment variables that are not propagated up to the build script due to the activate script. You should source the activate script instead of running a separate shell. Check out the following implementation:

virtualenv venv --distribute
. venv/bin/activate
pip install -r requirements.txt
python tests.py

The following implementation will only work if you run it as a single build step in a virtual environment.

How to use requirements.txt while adding dependencies for setuptools?

It is possible to reference “requirements.txt” in the install_requires argument. You can parse requirements.txt using the pip.req module and use the pip parser generator function:

from pip.req import parse_requirements
myReqs = parse_requirements()
reqs = [str(ir.req) for ir in myReqs]
setup(

myReqs=reqs
)

Conclusion

We have demonstrated how we can create our own Projects into Python Packages. The ability to openly share your work and how it helps out the development of Python has been discussed. We have also discussed the similarity between setuptools and Python’s distutils on how the former has proved to be of greater use in the current Python environment.

Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments