{"id":4704,"library":"pydrive2","title":"PyDrive2","description":"PyDrive2 is an actively maintained fork of PyDrive, a Python wrapper library for the Google Drive API. It simplifies common Google Drive API tasks like authentication and file management, providing an object-oriented interface. The library is currently at version 1.21.3 and has a regular release cadence, with recent updates addressing dependency compatibility and new features like fsspec integration.","status":"active","version":"1.21.3","language":"en","source_language":"en","source_url":"https://github.com/iterative/PyDrive2","tags":["google-drive","api","cloud-storage","oauth","file-management"],"install":[{"cmd":"pip install PyDrive2","lang":"bash","label":"Standard Install"},{"cmd":"pip install PyDrive2[fsspec]","lang":"bash","label":"Install with fsspec support"}],"dependencies":[{"reason":"Core library for interacting with Google APIs.","package":"google-api-python-client","optional":false},{"reason":"Handles OAuth2.0 authentication for Google services.","package":"oauth2client","optional":false},{"reason":"Used for configuration settings, especially for custom OAuth flows.","package":"PyYAML","optional":false},{"reason":"A critical dependency for secure communication, often subject to version pinning for stability.","package":"cryptography","optional":false},{"reason":"Provides SSL/TLS functionality, also frequently subject to version constraints.","package":"pyOpenSSL","optional":false},{"reason":"Enables a filesystem-like interface for Google Drive, allowing standard file operations.","package":"fsspec","optional":true}],"imports":[{"note":"PyDrive2 is a fork; ensure you import from `pydrive2` not the original `pydrive`.","wrong":"from pydrive.auth import GoogleAuth","symbol":"GoogleAuth","correct":"from pydrive2.auth import GoogleAuth"},{"note":"PyDrive2 is a fork; ensure you import from `pydrive2` not the original `pydrive`.","wrong":"from pydrive.drive import GoogleDrive","symbol":"GoogleDrive","correct":"from pydrive2.drive import GoogleDrive"}],"quickstart":{"code":"import os\nfrom pydrive2.auth import GoogleAuth\nfrom pydrive2.drive import GoogleDrive\n\n# IMPORTANT: Place your 'client_secrets.json' file in the same directory\n# as this script, or specify its path in gauth.settings['client_config_file'].\n# Instructions to get client_secrets.json: https://docs.iterative.ai/PyDrive2/quickstart/#authentication\n\ngauth = GoogleAuth()\n# Uncomment the following line to specify a custom path for client_secrets.json\n# gauth.settings['client_config_file'] = os.path.join(os.getcwd(), 'path_to_your', 'client_secrets.json')\n\n# Try to load saved client credentials\ntry:\n    gauth.LoadCredentialsFile(\"mycreds.txt\")\nexcept Exception:\n    pass # File might not exist yet\n\nif gauth.credentials is None:\n    # Authenticate if credentials are not found or invalid.\n    # For persistent access, ensure your OAuth client is configured for 'offline' access\n    # to receive a refresh token. This requires manual setup in Google Cloud Console.\n    gauth.LocalWebserverAuth() \nelif gauth.access_token_expired:\n    # Refresh credentials if the access token has expired\n    gauth.Refresh()\nelse:\n    # Authorize with the loaded credentials\n    gauth.Authorize()\n\n# Save the current credentials to a file for future use\ngauth.SaveCredentialsFile(\"mycreds.txt\")\n\ndrive = GoogleDrive(gauth)\n\n# Create a text file and upload it to Google Drive\nfile_title = \"MyTestFile_PyDrive2.txt\"\nfile_content = \"Hello, Google Drive from PyDrive2!\"\n\nfile = drive.CreateFile({'title': file_title})\nfile.SetContentString(file_content)\nfile.Upload()\nprint(f\"Uploaded file: {file['title']} (ID: {file['id']})\")\n\n# List files in the root folder of Google Drive\nprint(\"\\nFiles in Google Drive (first 10):\")\nfile_list = drive.ListFile({'q': \"'root' in parents and trashed=false\"}).GetList()\nfor f in file_list:\n    print(f\"Title: {f['title']}, ID: {f['id']}\")","lang":"python","description":"This quickstart demonstrates how to authenticate with Google Drive using `PyDrive2` and then upload a simple text file. It utilizes `LocalWebserverAuth` for initial authorization and saves credentials for subsequent runs. Ensure you have downloaded your `client_secrets.json` from the Google API Console and placed it in your working directory. You must enable the Google Drive API for your project in the Google Cloud Console."},"warnings":[{"fix":"Upgrade your Python environment to 3.8 or a later version.","message":"Python 3.7 support was dropped in PyDrive2 version 1.16.0. Users on Python 3.7 or older must upgrade their Python environment to 3.8 or newer.","severity":"breaking","affected_versions":">=1.16.0"},{"fix":"Migrate your code to use `pydrive2`. Update `pip install pydrive` to `pip install pydrive2` and change import statements from `pydrive` to `pydrive2`.","message":"The original PyDrive library is deprecated and unmaintained. Using `pydrive` instead of `pydrive2` will lead to unpatched bugs and compatibility issues, especially with recent Google API changes.","severity":"gotcha","affected_versions":"<1.x (original PyDrive)"},{"fix":"Ensure your OAuth client is configured as a 'Web application' in the Google Cloud Console, and use `LocalWebserverAuth()` or service account authentication. For `LocalWebserverAuth`, ensure `http://localhost:8080/` is an authorized redirect URI.","message":"Google's deprecation of Out-of-Band (OOB) OAuth flows may affect older authentication methods. `LocalWebserverAuth` is the recommended interactive flow.","severity":"gotcha","affected_versions":"All versions, depending on Google's API changes."},{"fix":"If encountering issues, ensure you are on the latest `pydrive2` version, which includes dependency pinning. If problems persist, manually pin `pyOpenSSL` to `<=24.2.1` and `cryptography` to `<44` or as specified in `pydrive2`'s `pyproject.toml` or `setup.cfg` for your installed version.","message":"Versions of `pyOpenSSL` and `cryptography` can cause import or runtime errors due to breaking changes in their APIs. Specific versions have been pinned in PyDrive2 to mitigate this.","severity":"gotcha","affected_versions":"1.21.2, 1.21.3 (and potentially older/newer versions depending on transitive dependencies)"},{"fix":"When setting up your OAuth client in Google Cloud Console, ensure it's configured to grant offline access. In your code, if using `LocalWebserverAuth`, it typically handles this, but always save and load credentials. For service accounts, ensure the service account has appropriate permissions.","message":"For persistent authentication that doesn't require re-authorization on every run, you need to ensure a refresh token is obtained. This typically requires setting `access_type='offline'` in your OAuth configuration and persisting credentials (e.g., using `gauth.SaveCredentialsFile()`). Without it, access tokens will expire, requiring re-authentication.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-12T00:00:00.000Z","next_check":"2026-07-11T00:00:00.000Z"}