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
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
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
3. Integrating with setup.py:
Add the following lines to your setup() function in setup.py:
setup( ... cmdclass={ 'develop': PostDevelopCommand, 'install': PostInstallCommand, }, ... )
This will enable the execution of your post-installation script or function automatically upon installation from source or in editable mode.
以上是我們可以在 Python setuptools 中執行安裝後腳本嗎?的詳細內容。更多資訊請關注PHP中文網其他相關文章!