Home > Backend Development > Python Tutorial > Can we execute a post-installation script in Python setuptools?

Can we execute a post-installation script in Python setuptools?

Mary-Kate Olsen
Release: 2024-11-15 05:06:02
Original
314 people have browsed it

Can we execute a post-installation script in Python setuptools?

Custom Post-Installation Script in Python setuptools

Question:

Can we execute a post-installation script as part of the setuptools setup.py file? This script should run automatically after executing python setup.py install locally or pip install from PyPI.

Answer:

Requirement:

Note that this solution is only active during installations from source distributions (zip, tarball) or when installing in editable mode. It will not execute when installing from binary wheels (.whl).

Transparent Solution:

To implement the desired behavior, we can modify the setup.py file without creating additional files. We need to consider separate scenarios for development/editable mode and installation mode:

1. Development Mode:

Create a PostDevelopCommand class that extends setuptools.command.develop and includes your post-installation script:

from setuptools import setup
from setuptools.command.develop import develop

class PostDevelopCommand(develop):
    def run(self):
        develop.run(self)
        # Your post-installation script or function can be called here
Copy after login

2. Installation Mode:

Create a PostInstallCommand class that extends setuptools.command.install and includes your post-installation script:

from setuptools import setup
from setuptools.command.install import install

class PostInstallCommand(install):
    def run(self):
        install.run(self)
        # Your post-installation script or function can be called here
Copy after login

3. Integrating with setup.py:

Add the following lines to your setup() function in setup.py:

setup(
    ...

    cmdclass={
        'develop': PostDevelopCommand,
        'install': PostInstallCommand,
    },

    ...
)
Copy after login

This will enable the execution of your post-installation script or function automatically upon installation from source or in editable mode.

The above is the detailed content of Can we execute a post-installation script in Python setuptools?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template