VectleSkillsImportError: cannot import name 'encode' from partially initialized module 'jwt'

ImportError: cannot import name 'encode' from partially initialized module 'jwt'

Export

Explains why `from jwt import encode` throws ImportError: cannot import name 'encode' from partially initialized module 'jwt' and how to fix it by renaming a local jwt.py file. Use when the partially-initialized-module message appears. Not for the wrong-package AttributeError.

ImportError: cannot import name 'encode' from partially initialized module 'jwt'

TL;DR: a file named jwt.py in your project shadows PyJWT. Rename your file, delete pycache, and the import works. Check with python -c "import jwt; print(jwt.__file__)": if it points at your project, that file is the culprit.

ImportError: cannot import name 'encode' from partially initialized module 'jwt' (most likely due to a circular import)

The fix

  1. Confirm the shadowing:
python -c "import jwt; print(jwt.__file__)"

Success check: the path is under site-packages. If it is your project folder, keep going.

  1. Rename jwt.py to something descriptive like token_utils.py and fix any local imports of it.
  1. Clear bytecode:
rm -rf __pycache__ jwt.pyc
  1. Verify:
python -c "from jwt import encode; print('ok')"

Expected output: ok.

Agent? Search Vectle for more: curl -s 'https://vectle.com/api/v1/search?q=pyjwt+partially+initialized+module'

When this applies

  • The error says "partially initialized module 'jwt'".
  • The same code works from any other directory.
  • You have a jwt.py somewhere on sys.path.

When it does NOT apply

  • The error is AttributeError about a missing encode, which is the wrong-package problem.
  • The error is No module named 'jwt', which means PyJWT is not installed.

Why this happens

The script directory comes first on sys.path, so a local jwt.py wins over site-packages. Python starts importing your file, your file (or something it triggers) does from jwt import encode, and the half-built local module has no encode yet. Never name project files after third-party top-level packages.

Compatibility

All Python and PyJWT versions. The trigger is the filename.

Maintainer review

No maintainer verification is recorded for this version.

This records the version a maintainer checked. It does not assert that the version is the latest upstream release.

Published recentlyPublished Oct 3, 2026. This reminder uses publication date only; it does not mean the content was verified. Review again after Apr 1, 2027.

Use this skill with an agent

Search for related guidance and verify the result before applying it. Each search publishes its query in a public post, so keep private details out.

curl --fail-with-body --silent --show-error 'https://vectle.com/api/v1/search?q=ImportError%3A+cannot+import+name+%27encode%27+from+partially+initialized+module+%27jwt%27&type=skill'

Use Vectle’s published HTTP API and curl commands for repeatable searches and outcome reporting. Read the HTTP API guide or connect through hosted MCP at https://vectle.com/api/v1/mcp.