Search code examples
pythontypesdropbox-apimypydropbox-sdk

mypy cannot find module 'dropbox'


I have a small codebase to back up Dropbox Business files, and am trying to use mypy to help me use the rather unpythonic Dropbox Python SDK.

I have installed mypy, and it is working.

However, mypy raises a warning for the following line:

import dropbox

The warning is "Cannot find module named 'dropbox'".

It seems that Dropbox's SDK generator, called Stone, should generate compatible stub files (which in this case would be called dropbox.pyi).

But there is no dropbox.pyi in site-packages/dropbox, where mypy would look for it.

How can I get type checking for the dropbox package working?

Thanks in advance.

Versions:

  • Python 3.6 64-bit (Anaconda)
  • OS: Windows 10 Pro 64-bit
  • mypy 0.521
  • dropbox SDK 8.0.0

Solution

  • The Dropbox Python SDK doesn't include the .pyi files as part of the Dropbox Python SDK, so you'd have to build them, and set MYPYPATH.

    To do so from the Dropbox API spec:

    # We first need the Dropbox stone and public stone specs:
    git clone git@github.com:dropbox/stone.git
    git clone git@github.com:dropbox/dropbox-api-spec.git
    
    # Next we need to install ply (used for running stone)
    pip install ply
    
    # Use stone to build the type stubs
    PYTHONPATH=stone python -m stone.cli python_type_stubs mypy_stubs/dropbox dropbox-api-spec/*.stone
    
    # include __init__.py files
    touch mypy_stubs/__init__.py mypy_stubs/dropbox/__init__.py
    
    # now mypy succeeds when using MYPYPATH to reference new .pyi files
    MYPYPATH=mypy_stubs mypy project.py
    

    Alternatively, you can build the stubs from the Dropbox Python SDK:

    # This assumes the following python modules are already installed: six, ply
    git clone git@github.com:dropbox/dropbox-sdk-python.git
    cd dropbox-sdk-python/
    git submodule init
    git submodule update
    PYTHONPATH=./stone python -m stone.cli python_type_stubs mypy_stubs/dropbox spec/*.stone