Search code examples
pythonmodulenotfounderror

ModuleNotFoundError: No module named 'tinkoff.invest'; 'tinkoff' is not a package


I have trouble with tinkoff API 2.0. Venv does not see tinkoff-investments, but it was installed successfully.

What did I try:

  • downgrade from 3.10 to 3.8 python version
  • update from 3.10 to 3.10.7
  • deleted and installed module tinkoff-investments

CODE: image

from tinkoff.invest import Client

TOKEN = 'my_token'

with Client(TOKEN) as client:

    print(client.users.get_accounts())

Solution

  • This error:

    ModuleNotFoundError: No module named 'tinkoff.invest'; 'tinkoff' is not a package
    

    may also occur if you have named the main program file you created as tinkoff.py and try to run it as python tinkoff.py or another file has the name tinkoff.py in the same folder from which you run your program. Python will consider your program file as a module and try to find something in it that is naturally not in it. About where Python is looking for modules, see sys.path.

    In this case, rename your program file so that its name does not equal with the name of the imported module.


    In your case, according to the screenshot, you run the file tinkoff.py, which has this line about import:

    from tinkoff.invest import Client
    

    Python sees import and first thing (as described above in documentation at link) Python does is look invest at this file — tinkoff.py, not in module tinkoff you installed in venv or in Python310\Libs\... It looks like Python trying to import the same file into itself and finds only the TOKEN variable and not invest object.
    So simply rename your startup file tinkoff.py to something else, for example to tinkoffApp.py, to avoid the similarity of the name with the imported module.