带有Python Setuptools的后安装脚本
是否可以作为setUptools setup.py文件的一部分指定后安装python脚本文件,以便用户可以运行命令:
python setup.py install
在本地项目文件存档或
pip install <name>
PYPI项目上,并且该脚本将在完成标准SetUptools安装的完成?我希望执行可以在单个Python脚本文件中编码的后安装任务(例如,向用户提供自定义的后安装消息,从其他远程源存储库中摘取其他数据文件)。
我遇到几年前解决这个主题,听起来好像当时的共识是您需要创建一个安装子命令。如果仍然如此,是否可以提供一个如何执行此操作的示例,以便用户不必输入第二个命令来运行脚本?
Is it possible to specify a post-install Python script file as part of the setuptools setup.py file so that a user can run the command:
python setup.py install
on a local project file archive, or
pip install <name>
for a PyPI project and the script will be run at the completion of the standard setuptools install? I am looking to perform post-install tasks that can be coded in a single Python script file (e.g. deliver a custom post-install message to the user, pull additional data files from a different remote source repository).
I came across this SO answer from several years ago that addresses the topic and it sounds as though the consensus at that time was that you need to create an install subcommand. If that is still the case, would it be possible for someone to provide an example of how to do this so that it is not necessary for the user to enter a second command to run the script?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
注意:下面的解决方案仅在安装源分布zip或tarball或从源树中安装可编辑模式时起作用。从二进制轮安装(
.whl
)时,它将无法使用 ,此解决方案更透明:
您将为
setup.py 并且不需要额外的文件。
另外,您需要考虑两个不同的后安装;一个用于开发/可编辑模式,另一种用于安装模式。
添加这两个类,其中包括您的后安装脚本 setup.py :
并插入
cmdclass
参数tosetup()
setup() setup.py 中的代码>函数:您甚至可以在安装过程中调用shell命令,就像在此示例中进行预安装准备:
ps: ps没有任何可用的预启动输入点setuptools。阅读为什么没有。
Note: The solution below only works when installing a source distribution zip or tarball, or installing in editable mode from a source tree. It will not work when installing from a binary wheel (
.whl
)This solution is more transparent:
You will make a few additions to
setup.py
and there is no need for an extra file.Also you need to consider two different post-installations; one for development/editable mode and the other one for install mode.
Add these two classes that includes your post-install script to
setup.py
:and insert
cmdclass
argument tosetup()
function insetup.py
:You can even call shell commands during installation, like in this example which does pre-installation preparation:
P.S. there are no any pre-install entry points available on setuptools. Read this discussion if you are wondering why there is none.
注意:下面的解决方案仅在安装源分布zip或tarball或从源树中安装可编辑模式时起作用。 它将无法工作
从二进制轮安装(
.whl
)时, 已经安装了:Note: The solution below only works when installing a source distribution zip or tarball, or installing in editable mode from a source tree. It will not work when installing from a binary wheel (
.whl
)This is the only strategy that has worked for me when the post-install script requires that the package dependencies have already been installed:
注意:下面的解决方案仅在安装源分布zip或tarball或从源树中安装可编辑模式时起作用。从二进制轮安装(
.whl
)时,它将无法使用可以在
post_setup.py
in设置中包含一个解决方案.py
的目录。post_setup.py
将包含一个函数,该函数执行后安装,setup.py
仅在适当的时间导入并启动它。在
setup.py
中:在
post_setup.py
中:具有从其目录中启动
setup.py
的常见想法,您将能够导入post_setup.py
否则它将启动一个空功能。在
post_setup.py
中,如果__name __ =='__-main __':
语句允许您从命令行手动启动后安装。Note: The solution below only works when installing a source distribution zip or tarball, or installing in editable mode from a source tree. It will not work when installing from a binary wheel (
.whl
)A solution could be to include a
post_setup.py
insetup.py
's directory.post_setup.py
will contain a function which does the post-install andsetup.py
will only import and launch it at the appropriate time.In
setup.py
:In
post_setup.py
:With the common idea of launching
setup.py
from its directory, you will be able to importpost_setup.py
else it will launch an empty function.In
post_setup.py
, theif __name__ == '__main__':
statement allows you to manually launch post-install from command line.结合@apalala,@zulu和@mertyildiran的答案;这在Python 3.5环境中对我有用:
这也使您可以访问
install_path
中的软件包的安装路径,以进行一些外壳工作。Combining the answers from @Apalala, @Zulu and @mertyildiran; this worked for me in a Python 3.5 environment:
This also gives you access the to the installation path of the package in
install_path
, to do some shell work on.我认为执行后安装并保留要求的最简单方法是装饰呼叫
setup(...)
:这将运行
setup()
声明设置时
。完成要求安装后,它将运行_POST_INSTALL()
函数,该功能将运行内部函数_post_actions()
。I think the easiest way to perform the post-install, and keep the requirements, is to decorate the call to
setup(...)
:This will run
setup()
when declaringsetup
. Once done with the requirements installation, it will run the_post_install()
function, which will run the inner function_post_actions()
.如果使用Atexit,则无需创建新的CMDClass。您只需在设置()调用之前立即创建ATEXIT注册即可。它做同样的事情。
另外,如果您需要首先安装依赖关系,则可以使用PIP安装 ,因为在PIP将软件包移动到位之前,请调用您的Anexit处理程序。
If using atexit, there is no need to create a new cmdclass. You can simply create your atexit register right before the setup() call. It does the same thing.
Also, if you need dependencies to be installed first, this does not work with pip install since your atexit handler will be called before pip moves the packages into place.
我无法通过任何提出的建议解决问题,所以这对我有帮助。
您可以在
setup()
insetup.py.py
之类的setup()
之后拨打函数,就像:I wasn't able to solve a problem with any presented recommendations, so here is what helped me.
You can call function, that you want to run after installation just after
setup()
insetup.py
, like that: