【问题标题】:ImportError: No module named 'google-api-python-client' when using py2appImportError:使用 py2app 时没有名为“google-api-python-client”的模块
【发布时间】:2019-08-21 03:15:38
【问题描述】:

我一直在尝试使用 this tutorial 使用 py2app 制作一个独立的应用程序,但我没有使用分叉版本,而是使用了 official version.。使用python setup.py py2app -A 工作正常,但使用python setup.py py2app 时出现错误。最后一行是ImportError: No module named 'google-api-python-client'(完整输出可以在here找到。)。

我尝试过的事情

python3 setup.py py2app

我也看过 thisstackoverflow 文章,但它给出了同样的错误。

Setup.py

from setuptools import setup

APP = ['YoutubeSubCount.py']
DATA_FILES = ['key.txt', 'logo_sub.png', 'logo_sub2.icns']
APP_NAME = "Youtube Sub Count"
OPTIONS = {
    'argv_emulation': True,
    'iconfile': 'icon.icns',
    'plist': {
        'CFBundleName': APP_NAME,
        'CFBundleDisplayName': APP_NAME,
        'CFBundleVersion': "0.1.0",
        'CFBundleShortVersionString': "0.1.0",
        'LSUIElement': True,
    },
    'packages': ['rumps', 'sty', 'google-api-python-client'],
}

setup(
    app=APP,
    data_files=DATA_FILES,
    options={'py2app': OPTIONS},
    setup_requires=['py2app'],
)

YoutubeSubCount.py

import rumps
import time
import sys
import os
import linecache
from tkinter import *
from sty import fg
from googleapiclient.discovery import build

global update_timer, key, service, channel

key = open(os.path.join(sys.path[0], './key.txt')).read().strip()
service = build('youtube', 'v3', developerKey=key)
channel2 = 'UCERizKQbgpBXOck0R6t_--Q'

subs = service.channels().list(
    part='statistics',
    id= channel2
).execute()['items'][0]['statistics']['subscriberCount']

timers = ["1 secs","5 secs","10 secs","15 secs","20 secs","25 secs","30 secs","35 secs","45 secs","50 secs","1 Min"]

update_timer = 60

key_path = "key.txt"



class Sub_Counter(rumps.App):

    @rumps.timer(update_timer)   
    def pull_data(self, _):
        subs = service.channels().list(
            part='statistics',
            id=channel2
        ).execute()['items'][0]['statistics']['subscriberCount']

        a = (str(subs))
        self.icon = "logo_sub.png"
        self.title = "Subscribers: " + str(a)
        self.notification = str(a) + " Subscribers"

    @rumps.clicked('About')
    def about(self, _):
        rumps.notification("Youtube Subscriber Count", "Made by Roxiun using Python & rumps", "Shows Youtube Subscriber counts")

    @rumps.clicked('Preferences', 'API Key')
    def configuration_window_api(self, _):
        response = rumps.Window('Enter new API Key')
        response.dimensions=(320, 160)
        response.run()
        if response.clicked:
            key = response.text
            service = build('youtube', 'v3', developerKey=key)

    @rumps.clicked('Preferences', 'Channel')
    def configuration_window_channel(self, _):
        response = rumps.Window('Enter new Youtube Channel Link')
        response.dimensions=(320, 160)
        response.run()
        if response.clicked:
            channel = response.text
            if "https://www.youtube.com/channel/" in channel:
                channel2 = channel.replace("https://www.youtube.com/channel/", "")
            else:
                channel2 = channel.replace("https://www.youtube.com/user/", "")

            subs = service.channels().list(
                part='statistics',
                id=channel2
            ).execute()['items'][0]['statistics']['subscriberCount']

            a = (str(subs))
            self.icon = "logo_sub.png"
            self.title = "Subscribers: " + str(a)
            self.notification = str(a) + " Subscribers"


    @rumps.clicked('Icon', 'Normal')
    def icon_on(self, _):
        self.icon = 'logo_sub.png'

    @rumps.clicked('Icon', 'Coloured')
    def icon_col(self, _):
        self.icon = 'logo_sub2.icns'

    @rumps.clicked('Icon', 'Off')
    def icon_off(self, _):
        self.icon = None

    @rumps.clicked('Update Timer', 'Every Second')
    def timer_1(self, _):
        update_timer = 1

    @rumps.clicked('Update Timer', '5 Seconds')
    def timer_2(self, _):
        update_timer = 5

    @rumps.clicked('Update Timer', '10 Seconds')
    def timer_3(self, _):
        update_timer = 10   

    @rumps.clicked('Update Timer', '20 Seconds')
    def timer_4(self, _):
        update_timer = 20

    @rumps.clicked('Update Timer', '30 Seconds')
    def timer_5(self, _):
        update_timer = 30   

    @rumps.clicked('Update Timer', '40 Seconds')
    def timer_6(self, _):
        update_timer = 40   

    @rumps.clicked('Update Timer', '50 Seconds')
    def timer_7(self, _):
        update_timer = 50   

    @rumps.clicked('Update Timer', '1 Minute')
    def timer_8(self, _):
        update_timer = 60   

    @rumps.clicked("Detailed Statistics")
    def Detailed_Statistics(self, _):
        rumps.notification("You have:", self.notification , "Veiws Comming Soon")

app = Sub_Counter("Loading...")#debug=True
app.menu = [
    ('About'),
    ('Preferences',('API Key', 'Channel')),
    None,
    ('Icon', ('Normal', 'Coloured', 'Off')),
    ('Update Timer', ('Every Second', '5 Seconds', '10 Seconds', '20 Seconds', '30 Seconds', '40 Seconds', '50 Seconds', '1 Minute')),
    None,
    ("Detailed Statistics")
]
app.run()

提前致谢!

更新

按照 Sergio Pulgarin 的回答后

New Setup.py

from setuptools import setup

APP = ['YoutubeSubCount.py']
DATA_FILES = ['key.txt', 'logo_sub.png', 'logo_sub2.icns']
APP_NAME = "Youtube Sub Count"
OPTIONS = {
    'argv_emulation': True,
    'iconfile': 'icon.icns',
    'plist': {
        'CFBundleName': APP_NAME,
        'CFBundleDisplayName': APP_NAME,
        'CFBundleVersion': "0.1.0",
        'CFBundleShortVersionString': "0.1.0",
        'LSUIElement': True,
    },
    'packages': ['rumps', 'sty'],
}

setup(
    app=APP,
    data_files=DATA_FILES,
    options={'py2app': OPTIONS},
    setup_requires=['py2app'],
    install_requires=['google-api-python-client'],
)

运行python setup.py py2app -A 给出了预期的输出并且工作正常,但是在运行python setup.py py2app 之后没有给出终端输出并且创建了应用程序,但是在打开应用程序时它打开了一个窗口sayinf Youtube Sub Count Error 并有一个按钮说控制台和一个说终止

【问题讨论】:

    标签: python python-3.x google-api-python-client py2app


    【解决方案1】:

    我知道这是一个老问题,但一个简单的解决方法是将 google_api_python_client-*.dist-info 复制到您各自的 Library/Frameworks/Python.framework/Versions/your_version/lib/python_version/site-packages/ 文件夹中并将其粘贴到 app_name.app/contents/Resources/lib/your_python_version/ 文件夹中。

    完成此操作后,尝试从终端再次运行该应用程序或单击 app_name.app/contents/macOS/app_name 中的文件作为第一次运行它时,可能需要通过您的 gmail 对其进行身份验证。

    我希望这能像我一样帮助人们在未来解决这个问题。

    【讨论】:

      【解决方案2】:

      你的意思是使用

      install_requires=[
         . . .
      ]
      

      ?

      https://packaging.python.org/discussions/install-requires-vs-requirements/

      【讨论】:

      • ??我遵循rumps.readthedocs.io/en/latest/creating.html 的指南,并在rumps 旁边添加了更多包名称。我会试试这个并将结果添加到帖子中
      • 它停止了控制台错误,但是在尝试打开应用程序时它打开了一个窗口,上面写着Youtube Sub Count Error。然后我尝试运行python setup.py py2app -A,当我打开应用程序时它运行良好。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-07-11
      • 2023-04-01
      • 2013-01-06
      • 1970-01-01
      • 2015-12-09
      • 2012-01-26
      • 2020-12-15
      相关资源
      最近更新 更多