我有一个从cookiecutter生成的项目模板。这是一个PyQT5
项目模板。
所以我想到了只是尝试运行它。我得到了这个错误:
ModuleNotFoundError: No module named 'socket_messenger.images'; 'socket_messenger' is not a package
所以它是这样的:
[dabljues@manjaro socket_messenger]$ tree
.
├── LICENSE
├── pytest.ini
├── README.rst
├── setup.py
└── socket_messenger
├── images
│ ├── ic_help_black_48dp_1x.png
│ ├── ic_insert_drive_file_black_48dp_1x.png
│ ├── ic_open_in_new_black_48dp_1x.png
│ ├── __init__.py
│ └── __pycache__
│ └── __init__.cpython-37.pyc
├── __init__.py
├── __pycache__
│ └── socket_messenger.cpython-37.pyc
├── socket_messenger.py
└── tests
├── __init__.py
└── test_socket_messenger.py
5 directories, 14 files
[dabljues@manjaro socket_messenger]$ cd socket_messenger/
[dabljues@manjaro socket_messenger]$ ls
images __init__.py __pycache__ socket_messenger.py tests
[dabljues@manjaro socket_messenger]$ python socket_messenger.py
Traceback (most recent call last):
File "socket_messenger.py", line 131, in <module>
main()
File "socket_messenger.py", line 121, in main
window = Messenger()
File "socket_messenger.py", line 20, in __init__
'ic_insert_drive_file_black_48dp_1x.png')
File "/usr/lib/python3.7/site-packages/pkg_resources/__init__.py", line 1142, in resource_filename
return get_provider(package_or_requirement).get_resource_filename(
File "/usr/lib/python3.7/site-packages/pkg_resources/__init__.py", line 359, in get_provider
__import__(moduleOrReq)
ModuleNotFoundError: No module named 'socket_messenger.images'; 'socket_messenger' is not a package
[dabljues@manjaro socket_messenger]$
抛出错误的行(跟进)(这是最后一行):
import sys
import pkg_resources
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import (QAction, QApplication, QDesktopWidget, QDialog, QFileDialog,
QHBoxLayout, QLabel, QMainWindow, QToolBar, QVBoxLayout, QWidget)
class Messenger(QMainWindow):
"""Create the main window that stores all of the widgets necessary for the application."""
def __init__(self, parent=None):
"""Initialize the components of the main window."""
super(Messenger, self).__init__(parent)
self.resize(1024, 768)
self.setWindowTitle('Messenger')
window_icon = pkg_resources.resource_filename('socket_messenger.images',
'ic_insert_drive_file_black_48dp_1x.png')
我在做错什么吗?我是否应该从其他目录或其他目录运行该python文件?
答案 0 :(得分:0)
它不起作用,因为您进入了socket_messenger
目录:
python会在socket_messenger/images
内搜索socket_messenger
。
尝试将socket_messenger.py
移到外部,例如移到父目录中。
也许您必须这样做才能获取资源路径名:
window_icon = pkg_resources.resource_filename('socket_messenger',
'images/ic_insert_drive_file_black_48dp_1x.png')