## TL;DR
attributeerror: module 'jwt' has no attribute 'encode' means Python imported the wrong package: the abandoned `jwt` distribution on PyPI shadows the real one, PyJWT, and it has no encode function. Fix it by running `pip uninstall -y jwt PyJWT` and then `pip install PyJWT`. Also check you do not have a local file named jwt.py shadowing the installed package, then verify with a one-line jwt.encode call.

```text
AttributeError: module 'jwt' has no attribute 'encode'
```

## Use this when
- `import jwt` succeeds but `jwt.encode` raises AttributeError: module 'jwt' has no attribute 'encode'
- `pip show jwt` reports an installed distribution (that is the wrong one)
- `jwt.__file__` points at a small package with no encode or decode in it
- The code worked on another machine or in another virtualenv with the same requirements file

## Not for this skill when
- The error is ModuleNotFoundError: No module named 'jwt'. Nothing is installed at all; see the related skill below
- The error names a different module or a different missing attribute (for example module 'jwt' has no attribute 'decode' after a PyJWT 1.x to 2.x upgrade is a different, API-change problem)
- jwt.encode exists but raises a type or algorithm error. Import is fine then; the call arguments are the problem

## Steps

1. See which distribution you actually have. Run:

```text
pip show jwt
pip show PyJWT
python -c "import jwt; print(jwt.__file__)"
```

Expected: if `pip show jwt` finds a package, that is the wrong one. PyJWT alone should be the only jwt provider.

2. Remove both distributions and install the right one:

```text
pip uninstall -y jwt PyJWT
pip install PyJWT
```

Expected: pip reports PyJWT installed and no jwt distribution remaining.

3. Rule out a shadowing local file:

```text
ls jwt.py
```

If a jwt.py exists in your working directory, rename it and delete the __pycache__ folder next to it, because Python imports the local file before site-packages.

Expected: no jwt.py in the working directory, or the renamed file no longer shadows the import.

4. Verify the fix in the same environment that raised the error:

```text
python -c "import jwt; print(jwt.encode({'a': 1}, 'secret', algorithm='HS256'))"
```

Expected: a three-part token string prints, with no AttributeError.

## Variant phrasings

### module 'jwt' has no attribute 'encode'
The short form of the same error, often pasted without the AttributeError prefix. Same wrong-package cause and the same uninstall-then-PyJWT fix in steps 1 to 2.

### pyjwt encode attributeerror
Usually the same collision seen from the other side: the code expects PyJWT (import jwt, jwt.encode) but the environment has the jwt distribution installed. Work steps 1 to 2.

### jwt has no attribute encode after pip install jwt
Installing with `pip install jwt` is exactly how the wrong package gets in. The package you want is named PyJWT even though it imports as jwt.

### attributeerror module jwt has no attribute decode
Same collision, hit on the decode call instead of encode. The abandoned jwt package lacks both; steps 1 to 2 fix decode too.

## Root cause
PyPI hosts two different distributions that both import as `jwt`: the real PyJWT and an unrelated, long-abandoned `jwt` package. Installing the wrong one is common because `pip install jwt` looks right, and the symptom is always this AttributeError: import succeeds, encode is missing. A local file named jwt.py causes the identical symptom by shadowing the installed package.

## Edge cases
- Virtualenv mismatch: pip and python point at different environments, so the fix lands where the code does not run. Re-run step 1 with the exact python that raises the error.
- Both distributions installed at once: uninstall both (step 2) before reinstalling, or leftover files from the wrong one can survive a plain `pip install PyJWT`.
- Pinned requirements: if a requirements file lists `jwt`, change the line to `PyJWT` or the wrong package comes back on the next install.

## Tool compatibility
PyJWT 2.x on Python 3. The squatter `jwt` distribution is unmaintained and should never be installed. Steps shown for pip; with conda or uv, uninstall and install the same two distribution names.

## Related skills
- ModuleNotFoundError: No module named 'jwt' (nothing installed at all, the neighbouring failure): https://vectle.com/skills/skl_xlSmzgPDYh_ERSmqcvsU-Q
- ModuleNotFoundError: No module named 'Crypto' (the same wrong-or-missing package shape in pycryptodome): https://vectle.com/skills/skl_yYUlQSrr4W6sVcUbM3piAQ
- importerror: `import openpyxl` failed (a missing companion package rather than a wrong one): https://vectle.com/skills/skl_tju-YKpJbVtEA6pRwyCBUw

## Provenance
Built from the itsourcecode fix guide and the project reports linked in the evidence.
