Vinny
Vinny

Reputation: 63

Is it possible to append Class objects to "__all__"?

Current Project Structure

---src
 |__controllers
 |  |__ prediction.py
 |   
 |__regression
    |__ __init__.py
    |__ deep_learning.py

The deep_learning.py contains a class where I am initializing a model.

Goal

My goal is to use the class object from deep_learning in prediction.py in the controller directory.

Current Code for __init__.py

I have tried adding the following code inside the __init__.py inside the regression directory:
from deep_learning import predict
future_predict = predict()
__all__.append('future_predict')

Here the predict is the name of the class that is contained inside the deep_learning.py.


Current Code for prediction.py

from flask import Flask, render_template, request, send_from_directory

@app.route('/<filename>', methods=["POST"])
def predict(filename):
    print("something: ", future_predict)
    return send_from_directory(app.config['IMAGE_UPLOAD_PATH'], filename)
    

Error

However, I have not been successful with using future_predict object inside prediction.py. The error: NameError: name 'future_predict' is not defined

Upvotes: 0

Views: 70

Answers (1)

J&#252;rgen Gmach
J&#252;rgen Gmach

Reputation: 6131

There are several misunderstandings:

  • __all__ is a way to define what is importable
  • you still need to import those symbols!
  • you need an __init__.py also in your controllers package

Usually, Python projects, which make use of a src top level directory, have a structure like this:

❯ tree src/
src/
└── project
    ├── __init__.py
    ├── package_1
    │   └── __init__.py
    ├── package_2
    │   └── __init__.py
    └── package_3
        └── __init__.py

4 directories, 4 files

Upvotes: 2

Related Questions