网站LOGO
博客 | 棋の小站
页面加载中
12月6日
达尔达尼亚瀑布,博洛尼亚,意大利 ...
网站LOGO 博客 | 棋の小站
记录学习,心得,状态,生活。
菜单
  • 博客 | 棋の小站
    记录学习,心得,状态,生活。
    用户的头像
    首次访问
    上次留言
    累计留言
    我的等级
    我的角色
    打赏二维码
    打赏博主
    Python问题汇总
    点击复制本页地址
    微信扫一扫
    文章二维码
    文章图片 文章标题
    创建时间
  • 一 言
    确认删除此评论么? 确认
  • 本弹窗介绍内容来自,本网站不对其中内容负责。
    按住ctrl可打开默认菜单

    Python问题汇总

    · 原创 ·
    解决困难 · FlaskpipPythonPyQt
    共 6082 字 · 约 3 分钟 · 435

    此文章用于存储我在Python学习和实践时遇到的困难。

    统计表格中数据

    现有表如图一,要求读取表中第一列的时间,并筛选出月份,求出每个月及每个季度的销售总额。
    代码:图片2
    结果:图片3



    使用Flask-SQLAlchemy遇到请求上下文的问题

    示例代码:

    python 代码:
    from flask import Flask
    from flask_sqlalchemy import SQLAlchemy
    
    app = Flask(__name__)
    
    # 配置相关属性
    app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True
    app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://root:254456@localhost/db_flask'
    
    # 实例化Alchemy类
    db = SQLAlchemy(app)
    
    # 创建数据库模型类
    class User(db.Model):
        id = db.Column(db.Integer, autoincrement=True, primary_key=True)
    username = db.Column(db.String(80), unique=True, nullable=False)
    email = db.Column(db.String(120), unique=True, nullable=False)
    
    def __repr__(self):
        return '' % self.username
    
    if __name__ == '__main__':
        db.create_all() # 这里遇到了异常

    控制台输出如下:
    RuntimeError: Working outside of application context.

    This typically means that you attempted to use functionality that needed the current application.
    To solve this, set up an application context with app.app_context().
    See the documentation for more information.

    这个异常通常出现在 Flask 应用程序上下文之外的地方使用了某些 Flask 功能。这可能是因为你尝试在 Flask 应用程序上下文之外的地方调用了 Flask 扩展、路由函数或视图函数。为了解决这个问题,你需要在使用 Flask 功能之前创建一个应用程序上下文。可以使用 app.app_context() 方法创建应用程序上下文,然后在上下文中执行需要的操作。
    将main部分的代码做如下改动:

    python 代码:
    if __name__ == '__main__':
        with app.app_context():
        # 在应用程序上下文中执行需要的操作
            db.create_all()

    问题就可以解决。

    Python使用pip遇到403

    遵照文章《使用国内镜像源加速pip下载》后使用清华大学镜像源下载wordcloud模块时出现403的问题:换用其他下载源即可。
    由于主题对评论区的限制,无法输入空格及HTML代码,在使用pip安装wordcloud的命令最后加上-i再加上淘宝镜像源https://mirrors.aliyun.com/pypi/simple/即可。若不熟悉pip安装的-i参数可百度或参照我的《使用国内镜像源加速pip下载》教程学习-i参数。

    cryptography模块也遇到同样情况,也可使用此方法解决。

    PyCharm配置PyQt5设计器和转换工具

    安装正版PyCharm专业版、汉化PyCharm过程略。新建项目、创建venv虚拟环境过程略。pip配置国内镜像点过程参照【使用国内镜像源加速pip下载】

    在venv环境中输入下方命令安装相关依赖包。

    shell 代码:
    pip install pyqt5
    pip install pyqt5-tools
    pip install pyqt5designer

    打开PyCharm设置界面,依次点击【工具】 - 【外部工具】,点击加号,进入配置工具界面。

    配置工具界面配置工具界面

    首先配置将.ui文件转换成.py文件的转换器工具,在如下图所示的输入框中输入下表中的文本。

    输入框内容
    名称PyUIC
    描述(非必填)将.ui文件转换成.py文件
    程序venv/Scripts/pyuic5.exe
    实参-o $FileNameWithoutExtension$.py $FileName$
    工作目录$ProjectFileDir$
    配置PyUIC配置PyUIC

    其中程序也可以点击右侧文件夹图标找到venv虚拟环境下的Scripts/pyuics.exe。

    接着配置设计器,按照刚才的方式打开配置工具界面,输入如下表的内容。

    输入框内容
    名称PyQtDesigner
    描述(非必填)PyQt5设计器
    程序venv/Scripts/designer.exe
    实参不填
    工作目录$ProjectFileDir$
    配置设计器配置设计器

    程序同样,可以手动寻找,它和PyUIC都在Python.exe的同级目录里。

    这两个工具的打开方式参照下图。

    打开外部工具打开外部工具

    直接点击设计器就可以打开设计师工具,设计好窗体并保存后得到.ui文件,然后在左侧文件列表先选中此文件,然后点击PyUIC工具就可以将此文件转换为Python文件。

    将.ui转换成.py将.ui转换成.py

    然后在最下方添加入口代码即可。

    python 代码:
    import sys
    
    if __name__ == '__main__':
        app = QtWidgets.QApplication(sys.argv)
        MainWindow = QtWidgets.QMainWindow()  # 创建窗体对象
        ui = Ui_MainWindow()  # 创建PyQt设置的窗体对象
        ui.setupUi(MainWindow)  # 调用PyQt窗体的方法对窗体对象进行初始化设置
        MainWindow.show()  # 显示窗体
        sys.exit(app.exec_())  # 程序关闭时关闭线程

    PyQt6显示不能找到DLL文件

    报错信息如下:

    Traceback (most recent call last):
    File "A:\JetBrainsProjects\pyqt6study\demo.py", line 3, in

    from PyQt6.QtWidgets import QApplication, QMainWindow

    ImportError: DLL load failed while importing QtCore: 找不到指定的程序。

    原因一:查询StackOverflow得知缺少Python的DLL。下载地址:https://dll.website/python3-dll

    原因二:PyQt6和PyQt6-Qt6软件包版本不对应,可使用pip list查看。

    PyQt6和PyQt6-Qt6软件包版本不对应PyQt6和PyQt6-Qt6软件包版本不对应

    先使用pip uninstall PyQt6-Qt6卸载,再使用pip install PyQt6-Qt6==6.6.0安装对应版本即可。

    解决。

    备用解决方案——来自StackOverFlow:

    Here a complete guide to install pyqt6 on Python 3.11

    Download the file from pypi. This is the link: https://pypi.org/project/PyQt6/#files. Download the version for windows with the name "PyQt6-6.4.1-cp37-abi3-win_amd64.whl"
    Rename the file to "PyQt6-6.4.1-py3-none-win_amd64.whl"
    Download now the file of plugins, also from pypi. This is the link: https://pypi.org/project/pyqt6-plugins/#files. Download the file named "pyqt6_plugins-6.1.0.2.2-cp39-cp39-win_amd64.whl"
    Rename this last file to "pyqt6_plugins-6.1.0.2.2-py3-none-win_amd64.whl"
    Now, with WinRar (or similar) open this last file, that now is renamed as "pyqt6_plugins-6.1.0.2.2-py3-none-win_amd64.whl"
    Open the folder "pyqt6_plugins-6.1.0.2.2.dist-info"
    Open with a text editor the file "METADATA"
    Find the line: "Requires-Dist: pyqt6 (==6.1.0)" and change to "Requires-Dist: pyqt6". Save the file.
    Now your 2 files are ready, and you just need to manually pip install the files:
    pip install PyQt6-6.4.1-py3-none-win_amd64.whl

    pip install pyqt6_plugins-6.1.0.2.2-py3-none-win_amd64.whl

    Done! You have installed Pyqt6 in Python 3.11!
    All the credits to the user https://github.com/liudonghua123 that solved this problem to pyqt5 in https://github.com/altendky/pyqt-tools/issues/108, this is just the same solution but applied to pyqt6.

    声明:本文由 (博主)原创,依据 CC-BY-NC-SA 4.0 许可协议 授权,转载请注明出处。

    还没有人喜爱这篇文章呢

    发一条! 发一条!
    博客logo 博客 | 棋の小站 记录学习,心得,状态,生活。
    ICP 冀ICP备2023007665号 ICP 冀公网安备 13030202003453号

    🕛

    本站已运行 221 天 15 小时 53 分

    👁️

    今日访问量:492 昨日访问量:2564

    🌳

    建站:Typecho 主题:MyLife
    博客 | 棋の小站. © 2023 ~ 2023.
    网站logo

    博客 | 棋の小站 记录学习,心得,状态,生活。
     
     
     
     
    壁纸