Reputation: 538
I am trying to call jwt.encode
function of PyJWT
but it's probably conflicting with my package jwt
and thus giving the error AttributeError("module 'jwt' has no attribute 'encode'")
My application structure is as below
jwt
|-- __init__.py
|-- db.py
instance
|-- jwt.sqlite
tests
|-- __init__.py
|-- conftest.py
|-- test_encodetoken.py
I get the error when I run
(venv) ~$ pytest
I put an empty __init__.py
in tests
folder because otherwise it cannot find my jwt
package.
Below is the function calling jwt.encode
which is inside db.py
import jwt
def encode_auth_token(user_id,app):
"""
Generates the Auth Token
:return: string
"""
try:
payload = {
'exp': datetime.datetime.utcnow() + datetime.timedelta(days=0, seconds=5),
'iat': datetime.datetime.utcnow(),
'sub': user_id
}
return jwt.encode(
payload,
app.config.get('SECRET_KEY'),
algorithm='HS256'
)
except Exception as e:
return e
Upvotes: 1
Views: 1395
Reputation: 11403
Fix: Rename the directory jwt
Why: Since your code is before the library's code in the PYTHONPATH
you code is seen and not the jwt
from PyJWT
Example:
dbutils
|-- __init__.py
|-- db.py
instance
|-- jwt.sqlite
tests
|-- __init__.py
|-- conftest.py
|-- test_encodetoken.py
Upvotes: 1