Registry / gcp / pydrive

pydrive

JSON →
library1.3.1pypypi✓ verified 87d ago

PyDrive is a Python wrapper library for the Google Drive API that simplifies common tasks like authentication, file upload, download, and management. The current version is 1.3.1. However, the original PyDrive project is deprecated and no longer maintained. Its last release was in 2016, and the GitHub repository was archived in July 2021. Users are strongly encouraged to consider `PyDrive2` (pypi.org/project/PyDrive2), an actively maintained fork, for ongoing development and support.

pip install pydrive
INSTALL
IMPORT
SIG · PYDRIVE
P
pydrive
gcppythonv1.3.1
Install
6.5s avg
Import
1286ms
Disk
150MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v1.3.1 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.920 runs
installs and imports cleanly · install 0.0s · import 1.327s · 150.2MB
glibc
py 3.103.920 runs
installs and imports cleanly · install 6.5s · import 1.244s · 152MB
150MB installed
● package 150MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

GoogleAuth
from pydrive.auth import GoogleAuth
GoogleDrive
from pydrive.drive import GoogleDrive

This quickstart demonstrates how to authenticate with Google Drive using PyDrive's `LocalWebserverAuth()` flow and then create and upload a simple text file. It also includes basic error handling for token expiration and shows how to list files. Ensure you have a `client_secrets.json` file from the Google API Console in your script's directory before running.

from pydrive.auth import GoogleAuth from pydrive.drive import GoogleDrive import os # --- Pre-requisites for Quickstart --- # 1. Go to Google API Console (console.developers.google.com/apis/credentials) # 2. Create a new project or select an existing one. # 3. Enable the 'Google Drive API'. # 4. Create 'OAuth client ID' credentials: # - Application type: 'Web application' # - Authorized JavaScript origins: http://localhost:8080 # - Authorized redirect URIs: http://localhost:8080/ # 5. Download the client configuration JSON file and rename it to 'client_secrets.json'. # 6. Place 'client_secrets.json' in the same directory as this script. # ------------------------------------- gauth = GoogleAuth() # Try to load saved client credentials (e.g., from 'credentials.json' generated previously) gauth.LoadCredentials() if gauth.credentials is None: # Authenticate if credentials are not found print("Performing initial authentication via local webserver...") gauth.LocalWebserverAuth() # Creates local webserver and auto handles authentication. elif gauth.access_token_expired: # Refresh them if expired print("Refreshing expired access token...") gauth.Refresh() else: # Initialize the saved credentials print("Using existing credentials.") gauth.Authorize() # Save the current credentials to a file for future use gauth.SaveCredentials() drive = GoogleDrive(gauth) # --- Create and Upload a File --- file_title = "PyDrive_Registry_Test_File.txt" file_content = "This is a test file uploaded using PyDrive from the registry quickstart. Hello, Google Drive!" # Create GoogleDriveFile instance with metadata. file_metadata = {'title': file_title, 'mimeType': 'text/plain'} file1 = drive.CreateFile(file_metadata) file1.SetContentString(file_content) # Set content from a string file1.Upload() # Upload the file to Google Drive print(f"\nSuccessfully uploaded file: '{file1['title']}' (ID: {file1['id']})") # --- List Files (optional, for demonstration) --- print(f"\nSearching for file with title '{file_title}'...") file_list = drive.ListFile({'q': f"'me' in owners and title = '{file_title}' and trashed = false"}).GetList() if file_list: print(f"Found {len(file_list)} file(s) with title '{file_title}':") for file in file_list: print(f" - Title: {file['title']}, ID: {file['id']}, MimeType: {file['mimeType']}") else: print(f"No file found with title '{file_title}'.")
Debug
Known issues
breakingThe original PyDrive project is explicitly deprecated and no longer maintained. Its GitHub repository has been archived. No further changes or bug fixes will be made, leading to potential future incompatibilities and security vulnerabilities.
fix
Migrate to `PyDrive2` (`pip install pydrive2`), an actively maintained fork, for continued support, new features, and bug fixes.
affects: 1.3.1 and earlier
deprecatedPyDrive relies heavily on the `oauth2client` library for authentication, which is deprecated by Google and largely unmaintained. This can lead to security vulnerabilities, lack of thread-safety, and incompatibilities with newer Python versions or related libraries (e.g., `PyOpenSSL` or `httplib2`).
fix
The recommended solution is to migrate to `PyDrive2`, which aims to address these underlying dependency issues by migrating to `google-auth`.
affects: All versions of PyDrive
gotchaAuthentication refresh tokens obtained from Google Cloud projects with an 'external user type' and a 'Testing' publishing status will expire after 7 days, requiring manual re-authentication.
fix
To avoid frequent re-authentication in production or long-running scripts, ensure your OAuth consent screen's publishing status in the Google API Console is set to 'In production'.
affects: All versions
gotchaBy default, PyDrive's authentication might only grant access to root-level files and folders on your Google Drive. Accessing subfolders or files outside the default scope requires explicit configuration.
fix
To grant broader access, you must manually specify the `oauth_scope` in a `settings.yaml` file (e.g., `oauth_scope: ['https://www.googleapis.com/auth/drive']`) and then re-authenticate to apply the new scopes. Deleting existing `credentials.json` before re-authentication might be necessary.
affects: All versions
gotchaThe `client_secrets.json` file, required for authentication, must be named exactly `client_secrets.json` and placed in the working directory of your script for PyDrive to find it automatically. Incorrect naming or placement will result in authentication errors.
fix
Ensure the downloaded client configuration JSON file is renamed to `client_secrets.json` and resides in the same directory where your Python script is executed. Alternatively, you can explicitly specify the file path via `GoogleAuth.DEFAULT_SETTINGS['client_config_file']` or `gauth.LoadClientConfig(client_config_file='path/to/your/client_secrets.json')`.
affects: All versions
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'pydrive'
The 'pydrive' library is not installed in the active Python environment or there is a conflict in how Python environments are being used.
fix
Install the library using `pip install pydrive` or, preferably, `pip install pydrive2` as the original PyDrive is deprecated. If using virtual environments, ensure the correct environment is activated before installing and running.
InvalidConfigError: Invalid client secrets file File not found: "client_secrets.json"
The `client_secrets.json` file, containing Google API credentials, is either incorrectly named, missing from the script's working directory, or the specified path to it is wrong.
fix
Download the OAuth 2.0 Client ID JSON file from the Google Cloud Console (for a 'Desktop app' or 'Web application' with correct redirect URIs), rename it exactly to `client_secrets.json`, and place it in the same directory as your Python script. Alternatively, explicitly specify the file path using `gauth.LoadClientConfig(client_config_file='path/to/your/client_secrets.json')`.
pydrive.auth.AuthenticationError (often with 'Failed to start a local web server.')
The `LocalWebserverAuth()` method attempts to start a local HTTP server (default ports 8080/8090) to receive the authentication callback, but these ports are blocked by a firewall, already in use, or the execution environment prevents a local web server.
fix
Check for applications using ports 8080/8090. If in a restricted environment, use `gauth.CommandLineAuth()` to get a URL for manual browser authentication and then paste the code. Ensure `client_secrets.json` includes `http://localhost:8080` (and `8090` if needed) in `redirect_uris`.
googleapiclient.errors.HttpError: <HttpError 403 ...>
This HTTP 403 error indicates that the authenticated Google account lacks the necessary OAuth scopes or permissions to perform the requested operation (e.g., read, write, delete a specific file), or the file itself has restrictions (e.g., flagged as malware).
fix
Verify that your Google Cloud Project has the Google Drive API enabled and your OAuth consent screen is configured. Ensure your `GoogleAuth` object or `settings.yaml` specifies sufficient OAuth scopes (e.g., `https://www.googleapis.com/auth/drive`). Re-authenticate after changing scopes. If the error is 'cannot download abusive file', the file owner might need to address the issue in Google Drive.
Upgrade
Version history
1.3.1latest on PyPI · released Oct 24, 2016
Audit
Dependencies
google-api-python-clientrequiredPyDrive is a wrapper around this core Google API client library.
oauth2clientrequiredUsed for OAuth 2.0 authentication, but is deprecated and a source of compatibility and security concerns.
httplib2requiredA dependency of `oauth2client`, which is largely unmaintained and can lead to issues with newer Python environments.
Agent activity
31 hits · last 30 days
node
28
OpenAI (training)
1
Resources
pydrive — pip install pydrive · libregistry