python无法连接到驱动器时转换为exe

p3rjfoxz  于 2023-06-04  发布在  Python
关注(0)|答案(1)|浏览(135)

``
import cv2 import pyautogui import numpy as np from datetime import datetime import os import shutil import time from google.oauth2 import service_account from googleapiclient. discovery import build from googleapiclient. http import MediaFileUpload import json import threading
设置屏幕录制参数SCREEN_SIZE =(1920,1080)FRAME_RATE = 15.0 output_directory ='.'
Google云端硬盘设置CREDENTIALS_FILE ='credentials.json'
创建凭证文件credentials ={"type ":"service_account","project_id":"screenrecords","private_key_id":"(我有一个,但为了安全起见,我不会把它放在这里)","private_key":"我有一个,但出于安全考虑,我不会将其放在这里","client_id":"我有一个,但出于安全考虑,我不会将其放在这里","auth_uri":"www.example.com","token_uri":https://accounts.google.com/o/oauth2/auth"www.example.com","client_x509_cert_url":"www.example.com","universe_domain":https://oauth2.googleapis.com/token", "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs ", "client_x509_cert_url": " https://www.googleapis.com/robot/v1/metadata/x509/mara2366%40screenrecords.iam.gserviceaccount.com ", "universe_domain": "googleapis.com" }
使用open(CREDENTIALS_FILE,'w')作为f:json. dump(credentials,f)
从凭证文件creds = service_account. Credentials. from_service_account_file(CREDENTIALS_FILE)读取凭证
从配置文件中读取或创建Google Drive文件夹ID config_file ='config.txt' default_google_drive_folder_id ='我有一个,但出于安全考虑,我不会将其放在这里'
如果不是os.path.isfile(config_file):使用open(config_file,'w')作为文件:file. write(default_google_drive_folder_id)
使用open(config_file,'r')作为文件:google_drive_folder_id = www.example.com(). strip()file.read ().strip()
如果os.path.isfile(CREDENTIALS_FILE):print("找到凭证文件")else:print("错误:找不到凭据文件。")
如果os. path. isfile(config_file):print("找到配置文件")else:print("错误:未找到配置文件。")
如果os.path.basename(CREDENTIALS_FILE),请验证文件名是否与实际文件名匹配!='credentials.json':print("错误:凭据文件名不正确。")
if os. path. basename(config_file)!='config.txt':print("错误:配置文件名不正确。")
使用open(CREDENTIALS_FILE,'r')作为f从凭证文件加载凭证:credentials = json. load(f)
print("Loaded credentials:",credentials)
如果不是os. path. isfile(config_file),则从配置文件加载或创建Google云端硬盘文件夹ID:使用open(config_file,'w')作为文件:file. write(default_google_drive_folder_id)
使用open(config_file,'r')作为文件:google_drive_folder_id = www.example.com(). strip()file.read ().strip()
print("Google云端硬盘文件夹ID:",google_drive_folder_id)
def record_screen():输出文件= os. path. join(输出目录,f "{datetime.now(). strftime('% Y- % m- % d_ %H- %M- % S')}. avi")
fourcc = cv2.VideoWriter_fourcc(*"XVID")out = cv2.VideoWriter(output_file,fourcc,FRAME_RATE,SCREEN_SIZE)
while True:img = pyautogui. screenshot()frame = cv2.cvtColor(np. array(img),cv2.COLOR_RGB2BGR)out. write(frame)
if cv2.waitKey(1)== order('q '):折断
out.release()cv2.destroyAllWindows()
def upload_video(video_file):try:service = build('drive ',' v3',credentials = creds)
检查文件是否已存在于Google云端硬盘file_list = service. files(). list(q = f "name ='{os. path. basename(video_file)}' and parents in '{google_drive_folder_id}'",fields ='files(id)'). execute()files = file_list. get('files ',[])
如果len(文件)> 0:如果文件存在,请更新它file_id = files [0]['id '] media = MediaFileUpload(video_file)service. files(). update(fileId = file_id,media_body = media). execute()print(f "Video'{os. path. basename(video_file)}'updated successfully.")else:如果文件不存在,则创建它media = MediaFileUpload(video_file)file_metadata ={'name ':os. path. basename(video_file),'parents':[google_drive_folder_id]} service. files(). create(body = file_metadata,media_body = media,fields ='id '). execute()print(f "视频'{os. path. basename(video_file)}'上传成功。")
return True except Exception as e:print(f "上传视频'{video_file}'到Google云端硬盘时出错:",str(e))return False
def main():
开始屏幕录制过程recording_thread = threading. Thread(target = record_screen)recording_thread. start()
等待几秒钟,让屏幕录制开始计时。sleep(5)
while True:video_files =[f for f in os. listdir('.')if os. path. isfile(f)and f. endswith('. avi')] video_files. sort(key = lambda x:os. path. getmtime(x))
如果len(video_files)> 1:video_files = video_files [:-1]
对于video_files中的video_file:try:upload = upload_video(video_file)
如果上传:os. remove(video_file)print(f "Video '{video_file}' uploaded successfully and deleted.")else:print(f "上载视频“{video_file}”时出错。5秒后重试...")time. sleep(5)except PermissionError:print(f "跳过视频'{video_file}',因为它正被另一个进程使用。")
if not recording_thread. is_alive():折断
时间.睡眠(5)
recording_thread. join()
ifname=='main':main()

我希望它能成功地上传视频到我的驱动器。在将其转换为exe之前,它运行良好

8zzbczxx

8zzbczxx1#

这是PyInstaller的已知问题。当您将Python程序转换为**.exe文件时,PyInstaller会将Python解释器和程序所需的所有依赖项捆绑到.exe文件中。但是,Google云端硬盘API不包含在默认的依赖项集中。要解决此问题,您需要将Google云端硬盘API添加到捆绑到.exe文件中的依赖项列表中。您可以通过编辑PyInstaller生成的setup.py文件来完成此操作。在setup.py文件中,将以下行添加到install_requires列表**中:

google-api-python-client

将Google云端硬盘API添加到依赖项列表后,您可以重建.exe文件。.exe文件重建后,它应该能够连接到Google云端硬盘。
以下是您可以采取的解决此问题的一些其他步骤:

  • 将Google云端硬盘API添加到依赖项列表后,您可以重建.exe文件。.exe文件重建后,它应该能够连接到Google云端硬盘。
  • 尝试使用-n选项重建.exe文件,这将禁用有时会导致Google Drive API出现问题的优化。
  • 如果您仍然有问题,您可以在PyInstaller论坛或Google Drive API论坛上发布问题。

也可以尝试使用相对路径:使用os.path.isfile(os.path.join(os.path.dirname(__file__)而不是os.path.isfile(CREDENTIALS_FILE)更新output_directory变量以使用绝对路径。使用output_directory = os.path.dirname(os.path.abspath(__file__))而不是output_directory = '.'将输出目录设置为脚本的目录。另外,请在提问时使用代码部分的代码格式

相关问题