Post-install script with Python setuptools
Data Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.
Python's `setuptools` is a dominant library for packaging Python projects. It extends the standard Python `distutils` with many powerful features, one of which is the ability to run post-install scripts. This functionality can be quite useful for performing additional setup steps after a package installation, such as installing external dependencies, setting environment variables, or configuring settings.
Understanding Post-install Scripts in Setuptools
In `setuptools`, a post-install script is typically executed via the `entry_points` in the `setup.py` file. Entry points are a way of specifying what a package can provide to another package or application. When an entry point is designated as a post-install command, it will automatically run once your package installation is complete.
Creating a Post-install Script
To create a post-install script using `setuptools`, follow these general steps:
- Define the Post-install Function: This function will encompass the actions you wish to carry out post-installation.
- Modify `setup.py`: Integrate your function with `setuptools` using `entry_points`.
- Test the Script: Verify that the script functions as expected after installation.
Step-by-Step Example
1. Define the Post-install Function
Start by defining a post-install function. This function will contain the logic you wish to execute post-installation. Create a Python module for your scripts if you don't have one already, e.g., `post_install.py`.
- Portability: Ensure scripts are platform-independent whenever possible.
- Dependencies: Avoid using post-install scripts to install dependencies not specified in `install_requires`, as this can lead to an incomplete installation if the script fails.
- Error Handling: Implement robust error handling to prevent the script from crashing on failure, ensuring logging or rollback if necessary.
- PEP 517/518: With the advancements in Python packaging standards like PEP 517 and PEP 518, you might consider moving towards using pyproject.toml for more modern builds. However, note that post-install scripts are still primarily handled at lower levels of packaging that can incorporate `setuptools`.
- Documentation: Ensure any important changes made during post-installation are well-documented, providing users with clear guidance on the actions taken by these scripts.
Related reading
- Practicing BDD with python
- Precomputed Kernels with LibSVM in Python
- Predicting new data using sklearn after standardizing the training data
- Preferred or most common file extension for a Python pickle
- Prepend a level to a pandas MultiIndex
- Pretty-print a NumPy array without scientific notation and with given precision
- Pretty-print an entire Pandas Series / DataFrame
- Pretty-print an entire Pandas Series / DataFrame
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseNo course covers this one yet
Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.
Build my courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.