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 pydriveVerified import paths — ran on the pinned version, not inferred.
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.
Migrate to `PyDrive2` (`pip install pydrive2`), an actively maintained fork, for continued support, new features, and bug fixes.
The recommended solution is to migrate to `PyDrive2`, which aims to address these underlying dependency issues by migrating to `google-auth`.
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'.
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.
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')`.
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.
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')`.
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`.
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.