How do I require Tkinter with distutils?

大兔子大兔子 提交于 2019-12-02 01:32:27

You can have a class that inherits from install and then do this:

from distutils.command.install import install

class Install(install):
    def run(self):
        if not check_dependencies():
             # Tkinter was not installed, handle this here
        install.run(self) # proceed with the installation

def check_dependencies():
    try:
        return __import__('Tkinter')
    except ImportError:
        return None

Tkinter is in the python standard library, it should always be there.

Unfortunately there is no standard cross-platform way to force Tkinter to be installed. Tkinter is part of the Python standard library so distributors who strip out Tkinter, or other standard library modules, and package them as optional entities are doing so using their own package management tools and, in general, you'd need to know the specific commands for each distribution. The best you can do in general is test for and fail gracefully if Tkinter (or tkinter in Python 3) is not importable, so something like:

import sys
try:
    import Tkinter
except ImportError:
    sys.exit("Tkinter not found")
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!