我这个报错误什么原因?

我这个报错误什么原因?

D:\PyCharm\untitled\venv\Scripts\python.exe D:\PyCharm2021.2.3\plugins\python\helpers\pydev\pydevconsole.py --mode=client --port=55715

import sys; print('Python %s on %s' % (sys.version, sys.platform))

sys.path.extend(['D:\\PyCharm\\untitled', 'D:\\PyCharm\\untitled\\imooc\\mark\\sqldb', 'D:\\PyCharm\\untitled\\imooc\\python_pachong', 'D:\\PyCharm\\untitled\\imooc\\mark', 'D:\\PyCharm\\untitled\\imooc\\vega', 'D:\\PyCharm\\untitled\\imooc\\vega\\service', 'D:\\PyCharm\\untitled\\imooc\\gift\\common', 'D:\\PyCharm\\untitled\\imooc\\gift\\common_copy', 'D:\\PyCharm\\untitled\\imooc', 'D:\\PyCharm\\untitled\\test_my\\.pytest_cache', 'D:\\PyCharm\\untitled\\imooc\\dome_2', 'D:\\PyCharm\\untitled\\imooc\\mark\\markdb', 'D:\\PyCharm\\untitled\\imooc\\vega\\db', 'D:\\PyCharm\\untitled\\imooc\\gift', 'D:\\PyCharm\\untitled\\imooc\\gift\\storage', 'D:\\PyCharm\\untitled\\test_my\\testcase\\.pytest_cache', 'D:\\PyCharm\\untitled\\dome_sa\\GJ', 'D:\\PyCharm\\untitled\\test_my\\testcase', 'D:/PyCharm/untitled'])

Python 3.8.5 (tags/v3.8.5:580fbb0, Jul 20 2020, 15:43:08) [MSC v.1926 32 bit (Intel)]

Type 'copyright', 'credits' or 'license' for more information

IPython 7.28.0 -- An enhanced Interactive Python. Type '?' for help.

PyDev console: using IPython 7.28.0

Python 3.8.5 (tags/v3.8.5:580fbb0, Jul 20 2020, 15:43:08) [MSC v.1926 32 bit (Intel)] on win32

from app import db

Traceback (most recent call last):

  File "D:\python3\lib\site-packages\IPython\core\interactiveshell.py", line 3444, in run_code

    exec(code_obj, self.user_global_ns, self.user_ns)

  File "<ipython-input-2-db75c8fb0e4e>", line 1, in <module>

    from app import db

  File "D:\PyCharm2021.2.3\plugins\python\helpers\pydev\_pydev_bundle\pydev_import_hook.py", line 21, in do_import

    module = self._system_import(name, *args, **kwargs)

  File "D:\PyCharm\untitled\imooc\vega\app.py", line 5, in <module>

    from service.user_service import UserService

  File "D:\PyCharm2021.2.3\plugins\python\helpers\pydev\_pydev_bundle\pydev_import_hook.py", line 21, in do_import

    module = self._system_import(name, *args, **kwargs)

  File "D:\python3\lib\site-packages\service\__init__.py", line 34, in <module>

    from daemon import DaemonContext

  File "D:\PyCharm2021.2.3\plugins\python\helpers\pydev\_pydev_bundle\pydev_import_hook.py", line 21, in do_import

    module = self._system_import(name, *args, **kwargs)

  File "D:\python3\lib\site-packages\daemon\__init__.py", line 38, in <module>

    from .daemon import DaemonContext

  File "D:\PyCharm2021.2.3\plugins\python\helpers\pydev\_pydev_bundle\pydev_import_hook.py", line 21, in do_import

    module = self._system_import(name, *args, **kwargs)

  File "D:\python3\lib\site-packages\daemon\daemon.py", line 18, in <module>

    import pwd

  File "D:\PyCharm2021.2.3\plugins\python\helpers\pydev\_pydev_bundle\pydev_import_hook.py", line 21, in do_import

    module = self._system_import(name, *args, **kwargs)

ModuleNotFoundError: No module named 'pwd'

db.create_all()

Traceback (most recent call last):

  File "D:\python3\lib\site-packages\IPython\core\interactiveshell.py", line 3444, in run_code

    exec(code_obj, self.user_global_ns, self.user_ns)

  File "<ipython-input-3-653042798025>", line 1, in <module>

    db.create_all()

NameError: name 'db' is not defined

from flask import Flask
from flask_sqlalchemy import SQLAlchemy

app=Flask(__name__)
#配置参数
app.config['SQLALCHEMY_DATABASE_URL']='mysql://root:123456@127.0.0.1/test_flask'
db =SQLAlchemy(app)

class User(db.Model):
    id = db.Column(db.integer,primary_key=True)
    username=db.Column(db.String(64),nullable=False)


# if __name__=="__main__":
#     U=User


正在回答

登陆购买课程后可参与讨论,去登陆

1回答

同学,你好!在导入db模块时就报错了,因此在调用create_all()时也会报错。 

同学的环境中有python-daemon模块,该模块用于在UNIX系统中实现守护进程,但在windows上是行不通的。需要卸载掉python-daemon模块。使用pip uninstall卸载即可

祝学习愉快!

  • 热爱编程学习 提问者 #1

    D:\PyCharm\untitled\venv\Scripts\python.exe D:\PyCharm2021.2.3\plugins\python\helpers\pydev\pydevconsole.py --mode=client --port=56648

    import sys; print('Python %s on %s' % (sys.version, sys.platform))

    sys.path.extend(['D:\\PyCharm\\untitled', 'D:\\PyCharm\\untitled\\imooc\\mark\\sqldb', 'D:\\PyCharm\\untitled\\imooc\\python_pachong', 'D:\\PyCharm\\untitled\\imooc\\mark', 'D:\\PyCharm\\untitled\\imooc\\vega', 'D:\\PyCharm\\untitled\\imooc\\vega\\service', 'D:\\PyCharm\\untitled\\imooc\\gift\\common', 'D:\\PyCharm\\untitled\\imooc\\gift\\common_copy', 'D:\\PyCharm\\untitled\\test_my\\.pytest_cache', 'D:\\PyCharm\\untitled\\imooc\\dome_2', 'D:\\PyCharm\\untitled\\imooc\\mark\\markdb', 'D:\\PyCharm\\untitled\\imooc\\vega\\db', 'D:\\PyCharm\\untitled\\imooc\\gift', 'D:\\PyCharm\\untitled\\imooc\\gift\\storage', 'D:\\PyCharm\\untitled\\test_my\\testcase\\.pytest_cache', 'D:\\PyCharm\\untitled\\dome_sa\\GJ', 'D:\\PyCharm\\untitled\\test_my\\testcase', 'D:/PyCharm/untitled'])

    Python 3.8.5 (tags/v3.8.5:580fbb0, Jul 20 2020, 15:43:08) [MSC v.1926 32 bit (Intel)]

    Type 'copyright', 'credits' or 'license' for more information

    IPython 7.28.0 -- An enhanced Interactive Python. Type '?' for help.

    PyDev console: using IPython 7.28.0

    Python 3.8.5 (tags/v3.8.5:580fbb0, Jul 20 2020, 15:43:08) [MSC v.1926 32 bit (Intel)] on win32

    from app import db

    Traceback (most recent call last):

      File "D:\python3\lib\site-packages\IPython\core\interactiveshell.py", line 3444, in run_code

        exec(code_obj, self.user_global_ns, self.user_ns)

      File "<ipython-input-2-db75c8fb0e4e>", line 1, in <module>

        from app import db

      File "D:\PyCharm2021.2.3\plugins\python\helpers\pydev\_pydev_bundle\pydev_import_hook.py", line 21, in do_import

        module = self._system_import(name, *args, **kwargs)

      File "D:\PyCharm\untitled\imooc\vega\app.py", line 5, in <module>

        from service.user_service import UserService

      File "D:\PyCharm2021.2.3\plugins\python\helpers\pydev\_pydev_bundle\pydev_import_hook.py", line 21, in do_import

        module = self._system_import(name, *args, **kwargs)

      File "D:\python3\lib\site-packages\service\__init__.py", line 34, in <module>

        from daemon import DaemonContext

      File "D:\PyCharm2021.2.3\plugins\python\helpers\pydev\_pydev_bundle\pydev_import_hook.py", line 21, in do_import

        module = self._system_import(name, *args, **kwargs)

    ModuleNotFoundError: No module named 'daemon'

    from flask import Flask,render_template,flash
    from flask_sqlalchemy import SQLAlchemy
    
    
    app=Flask(__name__)
    #配置参数
    app.config['SQLALCHEMY_DATABASE_URL']='mysql://root:123456@127.0.0.1/test_flask'
    app.config['WTF_CSRF_SECRET_KEY']='abc1234abc'
    app.config['SECRET_KEY']='abc'
    db =SQLAlchemy(app)
    
    class User(db.Model):
        id = db.Column(db.integer,primary_key=True)
        username=db.Column(db.String(64),nullable=False)



    2021-10-24 14:18:08
  • 好帮手慕美 回复 提问者 热爱编程学习 #2

    同学,你好!同学的pycharm项目目录结构是有很多层级的文件夹吗?可以直接打开app.py所在的文件夹

    https://img1.sycdn.imooc.com//climg/6174fff609936d2e07530231.jpg

    https://img1.sycdn.imooc.com//climg/617500120935185502480248.jpg

    若还有问题,同学可以可以提供一下目录结构图

    祝学习愉快!

    2021-10-24 14:41:35
  • 热爱编程学习 提问者 回复 好帮手慕美 #3

    spacer.gif

    2021-10-24 14:58:56
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师