I'm writing a Flask restful API, through which an application will get some predictions. To prepare data for predictions I need to import a pickle full of objects. To unpickle it I need to import necessary class, stored in some directory in the project but not related to API.
Starting in the easiest way I tried to unpickle it in files using it, which produces less errors and then move imports one by one. But it still produced errors like:
Can't get attribute 'Doctor' on main' from 'C:\\Users\\
After hours of searching I finally added reference of class in the init file, from where I run the API just for tests in Postman and it worked:
import os
from src.classes.doctor import Doctor
from flask import Flask
from flask_bcrypt import Bcrypt
from flask_cors import CORS
from src.API.server.config import ProductionConfig
app = Flask(__name__)
CORS(app)
app.config.from_object(ProductionConfig)
bcrypt = Bcrypt(app)
from src.API.server.auth.views import auth_blueprint
app.register_blueprint(auth_blueprint)
if __name__ == "__main__":
app.run(host='127.0.0.1', port=5002, debug=False)
Though I don't know why. Also, the import is shown as unused by Pycharm. Then I tried to create exactly the same test to it, through Unittest package, on the base that worked previously on other methods. It again produces the same error:
Can't get attribute 'Doctor' on main' from 'C:\\Users\\
I tried to import it in the BaseTestCase, but it didn't work.
This will also occur more times in the future. I would also want to import all of these references only once on API startup. Where should they be referenced, so it is always loaded properly and does not create circular imports? Why this fix works for Postman? Why it didn't work for tests?