繁体   English   中英

python setup.py - 如何在安装后显示消息

[英]python setup.py - how to show message after install

我正在开发 PyQt5 应用程序并通过pip install使其可用,因为 python3 中的 pip 可以安装 pyqt5 作为依赖项。 我创建了一个入口点来启动我的包,并告诉 setup.py 它是一个 gui_scripts。

我现在想做的是,在用户输入pip install package并且安装完成后,向用户显示一条消息,告诉您现在可以在终端中输入package来加载应用程序。 这样做的正确方法是什么? 或者我不应该这样做?

如果你能确保

  • 该软件包总是从源代码分发版安装,而不是二进制轮,并且
  • 用户使用-v选项进行pip install

您可以在setup.py脚本中输出文本。

setup.py几乎是一个常规的 Python 脚本。 只需使用setup.py文件末尾的print()函数即可。 在这个例子中,文件结构是somedir/setup.pysomedir/test/test/__init__.py

简单的解决方案

from setuptools import setup

print("Started!")

setup(name='testing',
      version='0.1',
      description='The simplest setup in the world',
      classifiers=[
        'Development Status :: 3 - Alpha',
        'License :: OSI Approved :: MIT License',
        'Programming Language :: Python :: 3.0',
      ],
      keywords='setup',
      author='someone',
      author_email='someone@example.com',
      license='MIT',
      packages=['test'],
      entry_points={
      },
      zip_safe=False)

print("Finished!")

开始了!
运行安装
运行 bdist_egg
运行 egg_info
编写 testing.egg-info/PKG-INFO
...
...
...
处理测试依赖== 0.1
完成测试的处理依赖== 0.1
完成!

使用setuptools.command.install解决方案

此外,您可以子类化setuptools.command.install命令。 在干净的设置中更改install.run(self)os.system("cat testing.egg-info/PKG-INFO")的顺序时检查差异。

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


class PostInstallCommand(install):
    """Post-installation for installation mode."""
    def run(self):
        install.run(self)
        os.system("cat testing.egg-info/PKG-INFO")


setup(name='testing',
      version='0.1',
      description='The simplest setup in the world',
      classifiers=[
        'Development Status :: 3 - Alpha',
        'License :: OSI Approved :: MIT License',
        'Programming Language :: Python :: 3.0',
      ],
      keywords='setup',
      author='someone',
      author_email='someone@example.com',
      license='MIT',
      packages=['test'],
      entry_points={
      },
      cmdclass={
        'install': PostInstallCommand,
      },
      zip_safe=False)

另一种选择是日志记录模块。

例如rasterio使用这样的方法,您可以根据自己的需要进行调整:

import logging
import sys

logging.basicConfig(stream=sys.stderr, level=logging.INFO)
log = logging.getLogger()

...

log.info("Your message")

sys.stderr将由 pip 打印。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM