ModuleNotFoundError: No module named ....
Closed this issue · 4 comments
Do have a little program to test:
[]https://github.com/stefanino-ch/HelloWorld
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton
from Klasse import MeineKlasse
# Subclass QMainWindow to customize your application's main window
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
klasse = MeineKlasse()
klasse.sayHello()
self.setWindowTitle("My App")
button = QPushButton("Press Me!") # Set the central widget of the Window.
self.setCentralWidget(button)
app = QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec_()
All source files are in the same folder:
Does run locally from the command line.
Does run locally if built with pyinstaller.
Does not run if built as github action. Get the error message:
C:\Users\user\Downloads\HelloWorld> .\HelloWorld.exe
Traceback (most recent call last):
File "src\HelloWorld.py", line 4, in <module>
ModuleNotFoundError: No module named 'Klasse'
[7996] Failed to execute script HelloWorld
If I comment out the two lines:
# klasse = MeineKlasse()
# klasse.sayHello()
all is smoothly working. So it is the import of a class in a local file which fails. After almost a day of googling I have no clue where to dig further into....
Just for completeness:
class MeineKlasse():
def __init__(self):
'''
Constructor
'''
def sayHello(self):
print("Hello")
Hey!
I'd recommend checking out this post on imports in Python
https://realpython.com/absolute-vs-relative-python-imports/
Hopefully it might help, where I would start is by using a relative import as opposed to absolute for your class (eg, use from .Klasse import MeineKlasse
)
It's probably a system difference between Windows and Linux because this action technically uses linux & wine to package a windows executable
Let me know how you go!
Welcome!
Did some more testing and nop, don't see the forest trough the trees.
from .Klasse import MeineKlasse
Does run from command line
Locally created pyinstaller package creates the error message:
Traceback (most recent call last):
File "HelloWorld.py", line 4, in <module>
ImportError: attempted relative import with no known parent package
[6320] Failed to execute script HelloWorld
Github created package creates the error message:
Traceback (most recent call last):
File "src\HelloWorld.py", line 4, in <module>
ImportError: attempted relative import with no known parent package
[3236] Failed to execute script HelloWorld
Found some more tips on:
[]https://stackoverflow.com/questions/60593604/importerror-attempted-relative-import-with-no-known-parent-package
from . import MeineKlasse
Does not even run from the command line:
File "C:\Users\user\git\HelloWorld\src\HelloWorld.py", line 4, in <module>
from . import Klasse
ImportError: attempted relative import with no known parent package
import Klasse.MeineKlasse
Does not even run from the command line:
File "C:\Users\user\git\HelloWorld\src\HelloWorld.py", line 4, in <module>
import Klasse.MeineKlasse
ModuleNotFoundError: No module named 'Klasse.MeineKlasse'; 'Klasse' is not a package
Which makes totally sense.
Two things came into my mind:
1st
Several times I've read that the original ModuleNotFoundError
is related to the fact that the PYTHONPATH does not contain the project directory. If I check the output during the pyinstaller run on github I see
Extending PYTHONPATH with paths
['Z:\\github\\workspace', 'C:\\Users\\user\\git\\HelloWorld\\src']
which triggers the question if there is a path issue? The 1st path doesn't fit together with the paths displayed during the github run. The 2nd one is the one read from the spec file and only valid locally on my computer.
2nd
Something which could help maybe is to look at the warnings file:
Warnings written to /tmp\HelloWorld\warn-HelloWorld.txt
Did several tries to upload-artifact this file at the end of the action, but was not able to determine the correct path.
Oddly, I was getting Module Not Found: "pyodbc" from an exe generated with the python3-10-pyinstaller-5-3 branch. I'll see if I can make a minimal test repo to demonstrate.
Apologies, I was missing a requirements.txt file. My previous comment is incorrect.