开发者

How do I require Tkinter with distutils?

开发者 https://www.devze.com 2023-01-26 14:39 出处:网络
I\'m trying to compile a program using distutils but I want to make sure that the user has Tkinter installed before installi开发者_如何转开发ng my package.

I'm trying to compile a program using distutils but I want to make sure that the user has Tkinter installed before installi开发者_如何转开发ng my package.

My Google searches have failed to turn up any useful info, any clue how I'd do this?

Thanks, Wayne


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


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")


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号