Hello Dropbox Developer Community,
We're developing a Django application and are running into a very specific environment issue with the Python SDK when trying to access Team folders after an OAuth2 flow. We have code that works perfectly when run from a standalone script, but fails when executed within our Django/Gunicorn web application.
Our Goal:
Use an OAuth2 token from a team admin to create a client that can scan folders in the team's shared space.
The Problem:
Our application fails with the error: module 'dropbox.team' has no attribute 'DropboxTeam' when the web application tries to create a client.
However, when we run a simple test script from the server's command line, using the exact same logic and a manually generated token, it works perfectly. This proves the logic is correct, but the environment the Gunicorn service runs in is causing the issue.
Working Standalone Script & Output:
This script runs without errors from the command line:
# dropbox-test.py
import os
import time
from dotenv import load_dotenv
import dropbox
from dropbox import DropboxTeam
# Load environment variables from .env file
load_dotenv()
# Retrieve access token and admin member ID from environment
access_token = os.getenv("DROPBOX_ACCESS_TOKEN")
dbmid = os.getenv("DROPBOX_TEAM_ADMIN_MEMBER_ID")
# Initialize Dropbox Team client
dbx_team = DropboxTeam(access_token)
# Fetch and print team info — this works
team_info = dbx_team.team_get_info()
print("Team Name:", team_info.name)
# List team namespaces
namespaces = dbx_team.team_namespaces_list().namespaces
# Select target namespace
target_namespace = next(
ns for ns in namespaces if ns.name == "001 HAA Clients RETAINER"
)
# Create scoped client for the selected namespace — this works
dbx_target = dbx_team.as_user(dbmid).with_path_root(
dropbox.common.PathRoot.namespace_id(target_namespace.namespace_id)
)
print("Client for namespace created successfully.")
Output from shell:
Team info retrieved successfully. Team name: TEAM Client created successfully for target namespace
Failing Web Application Code:
This is the utility function in our Django app that fails. It is called by our views after the OAuth2 flow completes.
# from our docufy_app/dropbox_utils.py
import dropbox
import dropbox.team # This import fails under Gunicorn
from .models import DropboxCredential
def get_dbx_client():
credential = DropboxCredential.objects.first()
# ... token refresh logic ...
try:
# This line fails with the error, because for Gunicorn,
# dropbox.team has no attribute 'DropboxTeam'
team_client = dropbox.team.DropboxTeam(credential.access_token)
# ... rest of logic ...
except Exception as e:
# e is "AttributeError: module 'dropbox.team' has no attribute 'DropboxTeam'"
return None, f"An unexpected error occurred: {e}"
Our Environment:
Platform: Django / Gunicorn on a Linux server.
Python Version: Python 3.11.2
Dropbox SDK Version: Version: 12.0.2
Our Question:
We understand this is likely an environment issue, not a strict API one. Given that dropbox.team.DropboxTeam exists when we run a script from the command line but not when the same code is executed by our Gunicorn service, what could cause this discrepancy?
Could Gunicorn be picking up a different, older, system-wide installation of the dropbox library instead of the one in our project's virtual environment? Has anyone seen this behavior before and can suggest how to configure Gunicorn or the service file to ensure it uses the correct virtual environment and Python packages? Is this something to do with our Oath2 Flow that wasn't implemented correctly?
Thank you for any insight you can provide.