Skip to content

ModuleNotFoundError: No module named 'typer' after upgrading dependencies #1537

@tiangolo

Description

@tiangolo

Are you seeing an error like this after updating your dependencies?

python main.py
Traceback (most recent call last):
  File "/home/user/code/myproject/main.py", line 1, in <module>
    import typer
ModuleNotFoundError: No module named 'typer'

Just remove and install the dependencies again, that will solve it.

TL;DR:

This is related to dropping support for typer-slim, just by re-installing the dependencies (removing the Python environment and creating it from scratch) it will all work, and you won't see the error again in that environment.

Background

We just dropped support for typer-slim, a slimmed down version of typer that didn't include Rich.

Now typer-slim simply depends on typer, so when a package depends on typer-slim, it will get regular typer instead of a slimmed down version.

Technical Details

typer-slim provided the same files as typer, so, installing it, would createa directory typer/ in your Python environment.

Regular typer also creates the same package typer/.

So, if in your dependencies one package depends on typer and another one depends on typer-slim, one would end up ovewriting the other, but because they are the same files, in the end it would work, more or less.

But that would cause other issues (#1501), and is a very hacky and fragile workaround, so we needed to remove it.

The latest version of typer-slim no longer provides any files of its own, it only depends on typer, so typer will be installed normally, including its own files in typer/.

Why No Module

Now, the problem of ModuleNotFoundError: No module named 'typer' would only happen if you already had an environment installed with those old versions that provided the same files, and upgrade/install the new version.

The installer (uv, pip, poetry, etc.) doesn't have a predefined order in which to remove old files and copy new files.

If you got this error, what happened is that the installer:

  • Removed the old files for typer, the directory typer/
  • Installed the new files for typer, the directory typer/ again
  • Then it removed the old files for typer-slim, which also provided the same directory typer/, so it just removed that same typer/ directory copied from above 😱
  • Then it "copied" the new files for typer-slim, which doesn't provide any files anymore, so, nothing to copy 😬

The end result, after removing and copying the typer/ directory a couple of times, it ended up with no typer/ directory.

Strangely, it could happen that it did the process in the other order, starting with typer-slim and finishing with typer, and in that situation it would have worked.

And sadly, it would not be solvable by doing anything else that creates a new release, as the same would happen (only once).

It Will Happen Only Once

The next version installed (or installing from scratch) would work as normally, but the first time it is installed from the old version that provided files to any of the new ones that don't provide any files will have this (terrible) effect.

I'm sorry for that, there's not much that can be done really, other than re-installing, or doing a sequence of upgrading to one version and then upgrading to the next.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions