Preparing to package with setup.py

When packaging an application, a special file called setup.py is required. This file will be read by the packaging tool and used to determine things such as which libraries to include, and which files should be run when the user executes the application.

Create a file named setup.py in your main text editor folder (which I have named tkedit) and add the following content:

#!/usr/bin/env python3

from distutils.core import setup

setup(
name='tkedit',
version='0.1',
description='This is a python text editor with syntax highlighting',
author='David Love',
py_modules = [
"colorchooser",
"findwindow",
"fontchooser",
"highlighter",
"linenumbers",
"textarea",
"texteditor",
],
install_requires = [
"PyYAML",
],
entry_points = {
"console_scripts": ["tkedit = texteditor:main"]
}
)

To get our application building, we need to use a module designed to do that. Python comes with pip, which does a great job at handling this. Pip will use a module called distutils to prepare a package and we will need the setup function from its core module to do this.

After importing the setup function, we just need to call it, passing quite a few keyword arguments. These arguments represent the following:

  • name: The application's name
  • version: The application's version
  • description: A short description of the application
  • author: The author's name
  • py_modules: The modules (Python files) which will be extracted from the current project
  • install_requires: Any external libraries which need to be installed by pip to make the application usable
  • entry_points: The commands which can be run once the package is installed, and what Python function they correspond to

For this example, the entry_points argument defines a command tkedit which should run the main function from the texteditor module. We don't have this function yet, so we need to quickly go back to our texteditor.py file and add it. Note that this function will need to be defined outside of the TextEditor class, just before the if __name__ == "__main__" block:

def main():
mw = MainWindow()
mw.mainloop()

With the setup file created, we are now one step closer to distributing our application. Let's now have a look at exactly how this will be done.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset