【问题标题】:How to include .exe, apt-get and brew install files on a PyPi setup.py file如何在 PyPi setup.py 文件中包含 .exe、apt-get 和 brew 安装文件
【发布时间】:2019-01-24 22:23:01
【问题描述】:

我正在编写一个 setup.py 文件来使用 PyPi 包安装一个开源项目。问题是,这个项目还需要安装外部应用程序(ghostscriptimagemagicktesseract)。这些应用有不同的安装方式,具体取决于平台(win、linux 或 mac)。

我写了一个文件,当我执行python setup.py install 时安装它。问题是当我在PyPi 上加载tarwhl 文件并执行pip install Gap-ML 时,它只是在安装模块,而不是安装这些应用程序。

这里是 setup.py 的代码:

""" setup Gap-ML
Copyright, 2018(c), Andrew Ferlitsch
Autor: David Molina @virtualdvid
"""

from setuptools import setup, find_packages
from distutils.command.install import install
import os, sys, platform
import requests

##Install custom apps Ghostscript, Imagemagick, and Tesseract
def install_apps(app_name, app_path, url):
    """
      Install custom apps
      :param app_name: name of the app to install
      :param app_path: path on windows where the app should be installed
      :param url: url where the .exe file is located
    """
    #extract the app_name.exe from the url
    app = url.split('/')[-1]

    #verify if the software is already installed on windows
    if os.path.exists(app_path):
        print('{} already installed'.format(app_name))
    else:
        print('Download has started')

        #warning message to specify the correct path where to install the app
        if platform.architecture()[0] == '64bit':
            print('Please verify C:\\Program Files\\ is part of the path to install the app')
        else:
            print('Please verify C:\\Program Files (x86)\\ is part of the path to install the app')

        #download, install, and delete the app_name.exe
        r = requests.get(url, allow_redirects=True)
        open(app, 'wb').write(r.content)
        os.system(os.path.join(os.getcwd(),app))
        os.remove(app)

        #verify pythonpath to execute the apps from terminal
        if app_path not in sys.path:
            sys.path.append(app_path)
        print('{} has been installed successful'.format(app_name))

def main():
    #verify Operative System
    if sys.platform.startswith('win'):
        windows={'64bit':{1:{'app_name':'Ghostscript',
                             'app_path':'C:\\Program Files\\gs\\gs9.23\\bin\\',
                             'url':'https://github.com/ArtifexSoftware/ghostpdl-downloads/releases/download/gs923/gs923w64.exe'},
                          2:{'app_name':'Imagemagick',
                             'app_path':'C:\\Program Files\\ImageMagick-7.0.8-Q8',
                             'url':'https://www.imagemagick.org/download/binaries/ImageMagick-7.0.8-9-Q8-x64-static.exe'},
                          3:{'app_name':'Tesseract',
                             'app_path':'C:\\Program Files\\Tesseract-OCR',
                             'url':'https://digi.bib.uni-mannheim.de/tesseract/tesseract-ocr-w64-setup-v4.0.0-beta.1.20180608.exe'}
                         },
                 '32bit':{1:{'app_name':'Ghostscript',
                             'app_path':'C:\\Program Files (x86)\\gs\\gs9.23\\bin\\',
                             'url':'https://github.com/ArtifexSoftware/ghostpdl-downloads/releases/download/gs923/gs923w32.exe'},
                          2:{'app_name':'Imagemagick',
                             'app_path':'C:\\Program Files (x86)\\ImageMagick-7.0.8-Q8',
                             'url':'https://www.imagemagick.org/download/binaries/ImageMagick-7.0.8-9-Q8-x86-static.exe'},
                          3:{'app_name':'Tesseract',
                             'app_path':'C:\\Program Files (x86)\\Tesseract-OCR',
                             'url':'https://digi.bib.uni-mannheim.de/tesseract/tesseract-ocr-w32-setup-v4.0.0-beta.1.20180608.exe'}
                         },
                 'common':{1:{'app_name':'Ghostscript',
                              'url':'https://www.ghostscript.com/download/gsdnld.html'},
                           2:{'app_name':'Imagemagick',
                              'url':'https://www.imagemagick.org/script/download.php'},
                           3:{'app_name':'Tesseract',
                              'url':'https://github.com/UB-Mannheim/tesseract/wiki'}
                          }
                }

        OS=platform.architecture()[0]
        for i in range(1,4):
            try:
                app_name = windows[OS][i]['app_name']
                app_path = windows[OS][i]['app_path']
                url = windows[OS][i]['url']
                install_apps(app_name, app_path, url)
            except:
                app_name = windows['common'][i]['app_name']
                url = windows['common'][i]['url']
                print('{} Download files on {}'.format(app_name, url))

    elif sys.platform.startswith('linux'):
        #install Ghostscript:
        os.system('sudo apt-get update && sudo apt-get install ghostscript')
        #install ImageMagick:
        os.system('sudo apt-get install imagemagick')
        #install Tesseract:
        os.system('sudo apt-get install tesseract-ocr && sudo apt-get install tesseract-ocr-eng')

    elif sys.platform.startswith('darwin'):
        os.system('/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"')
        os.system('brew update')
        os.system('brew install ghostscript imagemagick tesseract')

##Setup config
#additional class to execute main() for custom install apps
class CustomInstall(install):
    def run(self):
        install.run(self)
        main()

#setup components
with open('README.md') as f:
    long_description = f.read()

install_requires=[
    'bs4',
    'numpy',
    'h5py',
    'imutils',
    'unidecode',
    'nltk',
    'requests',
    'opencv-python',
    'pillow',
    'pyaspeller']

tests_require=[
    'pytest',
    'pytest-cov']

package_data={'gapml': ['org-os/*', 'plan/*', 'tools/*', 'train/*']}

project_urls={"Documentation": "https://andrewferlitsch.github.io/Gap/",
              "Source Code": "https://github.com/andrewferlitsch/Gap"}

#https://pypi.org/pypi?%3Aaction=list_classifiers
classifiers=[
    'Development Status :: 3 - Alpha',
    'Intended Audience :: Healthcare Industry',
    'Topic :: Text Processing',
    'License :: OSI Approved :: Apache Software License',
    'Operating System :: Microsoft :: Windows',
    'Operating System :: MacOS',
    'Operating System :: POSIX :: Linux',
    'Programming Language :: Python :: 3',
    'Programming Language :: Python :: 3.0',
    'Programming Language :: Python :: 3.1',
    'Programming Language :: Python :: 3.2',
    'Programming Language :: Python :: 3.3',
    'Programming Language :: Python :: 3.4',
    'Programming Language :: Python :: 3.5',
    'Programming Language :: Python :: 3.6',
    'Programming Language :: Python :: 3.7']

setup(
    name='Gap-ML',
    version='0.9.2',
    description='NLP and CV Data Engineering Framework',
    author='Andrew Ferlitsch',
    author_email='aferlitsch@gmail.com',
    license='Apache 2.0',
    url='https://github.com/andrewferlitsch/Gap',
    project_urls=project_urls,
    long_description=long_description,
    packages=find_packages(exclude=["*.tests", "*.tests.*", "tests.*", "tests"]),
    install_requires=install_requires,
    tests_require=tests_require,
    package_data=package_data,
    cmdclass={'install': CustomInstall},
    classifiers=classifiers
)

如何从PyPi 安装这些应用程序?我错过了什么? :/

我们是 Machine LearningNatural Language Processing 的一个年轻的开源项目。如果有人想贡献,欢迎您加入我们。 :)

Repo on GitHub
Documentation

任何帮助将不胜感激,谢谢!

【问题讨论】:

  • 这种方法有几个问题:apt 不是 Linux 发行版的默认包管理器 - 如果你的包安装在 CentOS (yum)、Fedora (dnf) 上会怎样? SUSE (zypper) 等?在 MacOS 上,您无需检查是否已安装 brew,也无需检查是否配置正确,如有必要,将库路径添加到 DYLD_LIBRARY_PATH。这很快就会导致一个复杂且难以维护的解决方案,不确定您是否真的想要这样。
  • 一般来说,setup.py 应该只声明 Python 依赖项。如果您需要非 Python 部门,请检查您的安装脚本是否可用。如果没有,中止安装,打印一条信息消息,描述如何在当前平台上安装所需的东西。另一种选择是将外部共享库捆绑到轮子本身中:查看this question 以获取有关如何使用auditwheel/delocate 分别为 Linux/MacOS 生成此类轮子的示例。
  • 大家好,非常感谢。正如你所看到的,我对所有这些东西都很陌生。我将与我的团队讨论并修复我们的 setup.py 和文档!

标签: python setuptools pypi


【解决方案1】:

因为您将轮子 (.whl) 文件上传到 PyPI,pip install ... 将从该轮子安装,并且轮子不支持您尝试执行的操作。从理论上讲,您可以通过仅将.tar.gz.zip 文件上传到 PyPI 来使其“工作”,以便 pip 从中安装,但这需要用户始终以管理员权限安装您的包,这是与推荐做法背道而驰并且并不总是有效的坏主意(例如,用户将无法在 virtualenv 中安装您的包)。正确的做法是不要在您的setup.py 中安装任何系统包;相反,在项目的 README 中记录所需的包以及如何安装它们。

【讨论】:

  • 我注意到之前它只适用于tar 文件,但它取决于操作系统。我们就像您所说的 README.md 文件中的所有说明一样。我的想法是避免这种情况并为我们未来的用户轻松安装。很好,我将与我的团队分享所有这些有用的信息并获得最佳方法。谢谢!
猜你喜欢
  • 2015-03-21
  • 1970-01-01
  • 2017-05-28
  • 1970-01-01
  • 2020-12-22
  • 1970-01-01
  • 1970-01-01
  • 2018-11-13
  • 2013-11-23
相关资源
最近更新 更多