0

I made a project in Python with this project structure:

G:.
│   .gitignore
│   ReadMe.txt
│   requirements.txt
│   setup.py
│
├───.vscode
│       settings.json
│
├───app
│   │   app.py
│   │   models.py
│   │   __init__.py
│   │
│   ├───static
│   │   ├───css
│   │   │       login_styles.css
│   │   │
│   │   ├───fonts
│   │   │       NotoSansDisplay-Regular.ttf
│   │   │
│   │   ├───js
│   │   │       game_script.js
│   │   │       login_script.js
│   │   │
│   │   └───res
│   │       │   3201279.jpg
│   │       │   background_abstract.jpg
│   │       │
│   │       ├───fonts
│   │       │       ARCADECLASSIC.TTF
│   │       │
│   │       └───sprites
│   │           ├───invaders
│   │           │       space__0000_A1.png
│   │           │       space__0001_A2.png
│   │           │       
│   │           └───projectiles
│   │                   missile_1.png
│   │
│   ├───templates
│   │       game.html
│   │       login.html
│   │
│   └───__pycache__
│           app.cpython-310.pyc
│           __init__.cpython-310.pyc
│
├───instance
│       test.db
│
├───Pflichtenheft
│       Space-Invaders-ReWebed_Pflichtenheft.docx
│
└───tests
        test_01.py

I used the setup.py to create a tar.gz:

from setuptools import setup, find_packages

setup(
    name='space_invaders_rewebed',
    version='1.0',
    packages=find_packages(),
    include_package_data=True,
    install_requires=[
        'annotated-types==0.6.0',
        'anyio==4.3.0',
        'blinker==1.7.0',
        'click==8.1.7',
        'colorama==0.4.6',
        'exceptiongroup==1.2.0',
        'Flask==3.0.2',
        'Flask-SQLAlchemy==3.1.1',
        'greenlet==3.0.3',
        'idna==3.6',
        'itsdangerous==2.1.2',
        'Jinja2==3.1.3',
        'MarkupSafe==2.1.5',
        'pydantic==2.6.3',
        'pydantic_core==2.16.3',
        'sniffio==1.3.1',
        'SQLAlchemy==2.0.28',
        'starlette==0.36.3',
        'typing_extensions==4.10.0',
        'Werkzeug==3.0.1'
    ],
    entry_points={
        'console_scripts': [
            'run_space_invaders_rewebed = app:main'
        ]
    }
)

However, when I install my tar.gz in a venv and run my command run_space_invaders_rewebed, I get the following error:

AttributeError: module 'app' has no attribute 'main'

But there's a clearly defined main() in my app.py:

from flask import Flask, jsonify, render_template, request, redirect, url_for, session, make_response
from models import db, User, Score
from werkzeug.security import generate_password_hash, check_password_hash


def create_app():
    app = Flask(__name__)
    app.secret_key = "super secret key"

    app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///test.db'
    app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True

    db.init_app(app)

    with app.app_context():
        db.create_all()

    @app.route('/')
    def index():
        return render_template('login.html')

    
    return app


def main():
    app = create_app()
    app.run(debug=True)


if __name__ == '__main__':
    main()

I want my script to execute correctly, but the error doesn't allow me to. How exactly can I fix the error and why is my main() not found?

2 Answers 2

2

Your main function is defined in the app.py file, located in the app folder (seen as a module).

Two solutions :

Solution 1

Your entry point should then be defined this way :

entry_points={
    'console_scripts': [
        'run_space_invaders_rewebed = app.app:main'
    ]
}

Solution 2

Or you can keep your entry point definition, and add to app/__init__.py the following lines :

from .app import main

__all__ = [main]

This will add the main function to the app module.

Sign up to request clarification or add additional context in comments.

Comments

0

The entry-point configuration would follow <package>.<module>:<function> syntax, like:

entry_points={
    'console_scripts': [
        'run_space_invaders_rewebed = app.app:main'
    ]
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.