I have a basic Heroku app that runs just fine if I don't include from src.RtcTokenBuilder2 import RtcTokenBuilder, Role_Publisher, time in my main app file. I want to add a folder, src, that contains code for token generation for Agora RTC. To add the code I simply download the code from the Github in the link below then I copy and paste the src folder into my python project. The src folder that I need is found "DynamicKey/AgoraDynamicKey/python3/src". Once I have the src folder in the same directory I import the token generation using the import statement above.
Now I do git add AccessToken.py AccessToken2.py ChatTokenBuilder2.py DynamicKey.py DynamicKey2.py DynamicKey3.py DynamicKey4.py DynamicKey5.py Packer.py RtcTokenBuilder.py RtcTokenBuilder2.py RtmTokenBuilder.py RtmTokenBuilder2.py SignalingToken.py education_token_builder.py fpa_token_builder.py utils.py
The I commit the additions and run git push heroku master
By doing this the heroku git updates without errors and it says the site has been updated. But when I try to go see the example token that has been generated the site shows an error and says use the heroku logs to track the bug. The output of the logs is long but includes from src.RtcTokenBuilder2 import RtcTokenBuilder, Role_Publisher, time. ModuleNotFoundError: No module named 'src'
What am I doing wrong when I add the src folder and try to use it? There is nothing else wrong with my setup because everything works fine without the import above.
https://github.com/AgoraIO/Tools
hustlerserver.py
from flask import Flask, request
from src.RtcTokenBuilder2 import RtcTokenBuilder, Role_Publisher, time
app = Flask(__name__)
@app.route('/process_data', methods=['GET'])
def process_data():
x = int(request.args.get('x', 0))
y = request.args.get('y', 'default')
appId = "test"
appCertificate = "test"
expirationTimeInSeconds = 3600
channelName = y
uid = x
token = "random tok"
current_timestamp = int(time.time())
expired_ts = current_timestamp + expirationTimeInSeconds
token = RtcTokenBuilder.build_token_with_uid(appId, appCertificate, channelName, uid, Role_Publisher, token_expire=expired_ts, privilege_expire=expired_ts)
result = f'token is: {token}'
return f'<h1>{result}</h1>'
Procfile
web: gunicorn hustlerserver:app
Requirments.txt
Flask==3.0.1
gunicorn==21.2.0
Jinja2==3.1.3

Make sure there is an
__init__.pyfile in thesrcdirectory. That file can be empty, but its presence is what makes a directory a Python package.In your
hustlerserver.py, you are importing fromsrc.RtcTokenBuilder2. Make sure this path is correct and theRtcTokenBuilder2.pyfile exists inside thesrcdirectory.After making these changes, use
git add .to add all the changes, commit them, and then push to Heroku.