You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python setup.py报错:AttributeError: module 'app' has no attribute 'main'

问题:执行console脚本时提示AttributeError: module 'app' has no attribute 'main'

我用Python搭建了一个Space Invaders网页项目,项目结构如下:

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

通过以下setup.py生成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'
        ]
    }
)

在虚拟环境安装该包后,执行run_space_invaders_rewebed命令时出现错误:AttributeError: module 'app' has no attribute 'main',但app.py中已定义main()函数:

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()

原因分析

  • setup.py中entry_points指定的app:main里,app指的是app包(对应app/__init__.py所在的模块),而非app/app.py文件。
  • 当前app/__init__.py是空文件,没有将app.py中的main函数导入到包的命名空间中,因此导入app包时无法找到main属性。

修复方案

方案1:修改setup.py的entry_point路径

直接将console脚本的指向改为app.app:main,让它定位到app/app.py模块中的main函数:

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

修改完成后,重新执行打包、安装流程即可。

方案2:在app/__init__.py中导出main函数

在app/__init__.py文件中添加以下代码,将app.py中的main函数导入到包的顶层命名空间:

from .app import main

此方法无需修改原entry_points配置,重新打包安装后,app包就会拥有main属性,脚本可正常执行。


内容的提问来源于stack exchange,提问作者schwitzky

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.25 18:13:21