ModuleNotFoundError: No module named 'layout_colorwidget'

Heads up! You've already completed this tutorial.

Paul_Fishback | 2021-04-28 19:18:24 UTC | #1

Running PyQt6 on a Mac (Big Sur 11.2.3) with Atom 155.0 as my IDE.

Trying to replicate the example on p. 71 of the book (PyQt6 edition):

python
import sys
from PyQt6.QtCore import Qt
from PyQt6.QtWidgets import QApplication, QMainWindow, QVBoxLayout,QWidget
from layout_colorwidget import Color

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("My App")
        layout = QVBoxLayout()
        layout.addWidget(Color("red"))
        layout.addWidget(Color("green"))
        layout.addWidget(Color("blue"))
        widget = QWidget()
        widget.setLayout(layout)
        self.setCentralWidget(widget)

app = QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec()

Obtain error message, "ModuleNotFoundError: No module named 'layout_colorwidget'"


martin | 2021-04-28 19:18:11 UTC | #2

Hi @Paul_Fishback welcome to the forum!

The code for the layout_colorwidget file is given in the previous code block -- this needs to be saved to a separate file with the name layout_colorwiget.py so it can be imported. This isn't exactly clear, so I need to improve this.

PyQt/PySide 1:1 Coaching with Martin Fitzpatrick — Get one on one help with your Python GUI projects. Working together with you I'll identify issues and suggest fixes, from bugs and usability to architecture and maintainability.

Book Now 60 mins ($195)

You can also find the file in the source code downloads with the book, in the same folder with the layout examples.

Create GUI Applications with Python & Qt6 by Martin Fitzpatrick — (PyQt6 Edition) The hands-on guide to making apps with Python — Over 10,000 copies sold!

More info Get the book


The complete guide to packaging Python GUI applications with PyInstaller.
[[ discount.discount_pc ]]% OFF for the next [[ discount.duration ]] [[discount.description ]] with the code [[ discount.coupon_code ]]

Purchasing Power Parity

Developers in [[ country ]] get [[ discount.discount_pc ]]% OFF on all books & courses with code [[ discount.coupon_code ]]
Well done, you've finished this tutorial! Mark As Complete
[[ user.completed.length ]] completed [[ user.streak+1 ]] day streak
Martin Fitzpatrick

ModuleNotFoundError: No module named 'layout_colorwidget' was written by Martin Fitzpatrick .

Martin Fitzpatrick has been developing Python/Qt apps for 8 years. Building desktop applications to make data-analysis tools more user-friendly, Python was the obvious choice. Starting with Tk, later moving to wxWidgets and finally adopting PyQt. Martin founded PythonGUIs to provide easy to follow GUI programming tutorials to the Python community. He has written a number of popular https://www.martinfitzpatrick.com/browse/books/ on the subject.