[PYTHON] Cooperation between py2exe and setuptools


When creating an exe file on windows using py2exe, there are many sites that are unexpectedly written in combination with distutils. However, if you are using python on Linux etc., you will definitely want to use it in combination with distribute (setuptools) when packaging. The setuptools package enhances distutils, and if you include setuptools, the parameters given to the setup () function of distutils will be enhanced, the package can be distributed in egg format, and a command line tool called easy_install can be used. Or become.

This setuptools has stopped updating recently, but it is distribute that takes over the development and provides a fully compatible API. If you are going to use it now, you should definitely use distribute. I'd like to explain py2exe immediately, but for the sake of review, I will explain from distribute. (By the way, each concept is separate, so it may not be necessary to write it. For those people, this page is useless) </ del>

A kind comment turned out that the distribute became obsolete. It is better to go around and use setuptools.

Since the Setuptools 0.7 release, Setuptools and Distribute have merged and Distribute is no longer being maintained.

Installation of setuptools

First, I will introduce how to install general setuptools.

$ curl -O https://bitbucket.org/pypa/setuptools/downloads/ez_setup.py
$ sudo python ./dez_setup.py

Just download and run the setup script. Very easy. After installing this package, you will be able to use a tool called easy_install, which will allow you to install common packages on site_packages (see link at the end).

By the way, when distributing the source, you cannot assume that setuptools is installed. You can say "install" in the docs, but it's a bit ugly to do that, even though you're using python, which can also resolve dependencies. In such a case, include distribute_setup.py in the distribution and write as follows at the beginning of setup.py.


#!/usr/bin/env python
# -*- coding: utf-8 -*-

import ez_setup

If you put the above description at the beginning of setup.py, the distribute package will be installed when the setup.py script is installed.

How to write setup.py that combines setuptools + py2exe

With the introduction so far, setuptools can be imported. Here, I will introduce how to write setup.py that combines setuptools and py2exe. First of all, the template is as follows.


#!/usr/bin/env python
# -*- coding: utf-8 -*-

__author__  = "<author name>"
__author_email__ = "<author's email>"
__date__    = "<date>"
__version__ = "<version number>"

import os
from os import listdir
from os.path import join
from os.path import dirname
from os.path import splitext

import ez_setup

from setuptools import setup
import py2exe

def find_py_modules(dir):
    py_modules = []
    for f in listdir(dir):
        base, ext = splitext(f)
        if ext == '.py' and base != '<AppName>': py_modules.append(base)
    return py_modules

def read(fname):
    return open(join(dirname(__file__), fname)).read()

      url='<project homepage>',
            'script': '<main python module>',
            'icon_resources': [(1, '<icon path>'),],
      package_dir = {'': 'src'},
      package_data={'':['*.ini', '*.gif', '*.ico']},
      data_files=[("", [<necessary dlls>]),],

In the template, the part enclosed in <> is the part to be changed for each creator. The list is given below.

variable Overview
author Creator name
author's email The author's email address
date date
version number Version number
app name Paths of major python modules
description Overview
project homepage Project home page
icon path Icon path
necessary dlls Depending on the required file array and the package to be used, it may be necessary to include dlls and fonts.

The above variables need to be adjusted for the project. The method imported from distribute is setup. The parameters of setup.py are as follows.

Parameters Overview
version Software version number
description Software overview
author Creator name
author_email Author's contact
url Project url
license License information (not shown in the template)
keywords Keyword information (not described in the template)
long_description A more detailed description of the software
console/windows If you want to output an error message, declare it as a console parameter if you want to run a console program, as a pure UI app, or as a windows parameter if you want it to run.
options Compressed as a dictionary key, optimize, bundle_Specify files. (Not described in the template)
include_package_data CVS, Subversion, MANIFEST.A bool value that indicates whether to include the specified file in the in file
package_dir Directory information that is the basis of the package
package_data A dictionary that associates a list of package names with file name patterns
py_modules Specifying unpackaged python modules
data_files Specifying files to be included in the distribution other than the genuine python package
zipfile Specify whether to convert python modules to zipfile

The meanings of other methods are explained below. The <> variables in these functions and templates are not essential, so you can change them according to your project.

def find_py_modules(dir):
    py_modules = []
    for f in listdir(dir):
        base, ext = splitext(f)
        if ext == '.py' and base != '<AppName>': py_modules.append(base)
    return py_modules

find_py_modules is a modular version (also a simple implementation) of find_packages. In the case of an application that combines external packages, the core source may be a unit that does not need to be a package. In such a case, it is better to combine the py_modules parameter and the find_py_modules method (the contents should be adjusted individually) instead of the packages parameter and the find_packages method.

def read(fname):
    return open(join(dirname(__file__), fname)).read()

The read method is a method for reading the contents of long_description from a file. This is a convenient method when creating an application of a scale that description is not enough to explain.


As for setuptools itself, there is a blog with accurate and comprehensive information, so please refer to this.

I also referred to this site when creating this site. I think this is the starting point for handling setuptools. Other links about easy_install and distribute are below. Both are English sites, so if you want a simpler explanation, you can search on Google with the following keywords (+ python).

Recommended Posts

Cooperation between py2exe and setuptools
Cooperation between python module and API
(Bottom) Cooperation between Jupyter Notebook and running Excel
Between parametric and nonparametric
Difference between process and job
Correspondence between pandas and SQL
Conversion between unixtime and datetime
Difference between "categorical_crossentropy" and "sparse_categorical_crossentropy"
Collaboration between PTVS and Anaconda
Connection between flask and sqlite3
Difference between np.array and np.arange
Difference between MicroPython and CPython
Boundary between C and Golang
Difference between ps a and ps -a
Difference between return and print-Python
Difference between Ruby and Python split
Differences between Windows and Linux directories
Django's MVT-Relationship between Models and Modules-
Difference between java and python (memo)
Difference between list () and [] in Python
Difference between SQLAlchemy filter () and filter_by ()
Difference between == and is in python
[Note] Conflict between matplotlib and zstd
Differences between symbolic links and hard links
Memorandum (difference between csv.reader and csv.dictreader)
Correspondence between RecyclerView and Marker (Kotlin)
(Note) Difference between gateway and default gateway
Difference between Numpy randint and Random randint
Differences between Python, stftime and strptime
Difference between python2 series and python3 series dict.keys ()
Speed comparison between CPython and PyPy
[Python] Difference between function and method
Difference between SQLAlchemy flush () and commit ()
Python --Difference between exec and eval
[Python] Difference between randrange () and randint ()
[Python] Difference between sorted and sorted (Colaboratory)