[英]Python pkg_resources cannot find module
I have this project template generated from cookiecutter . 我有这个从cookiecutter生成的项目模板。 It's a PyQT5
project template. 这是一个PyQT5
项目模板。
So I thought about just trying to run it. 所以我想到了只是尝试运行它。 And I got this error: 我得到了这个错误:
ModuleNotFoundError: No module named 'socket_messenger.images'; 'socket_messenger' is not a package
So here's how it looks like: 所以这是它的样子:
[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]$
The line, that throws the error (with followup) (it's the last line): 引发错误的行(包括后续操作)(这是最后一行):
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')
Is there something I am doing wrong? 我做错什么了吗? Should I maybe run this python file from a different directory or something? 我是否应该从其他目录或其他目录运行该python文件?
It does not work because you cd into socket_messenger
directory: python will search socket_messenger/images
inside socket_messenger
. 它不起作用,因为您cd进入socket_messenger
目录:python将在socket_messenger
搜索socket_messenger/images
。
Try to move socket_messenger.py
outside, for example in the parent directory. 尝试将socket_messenger.py
外部,例如在父目录中。
And probably you have to do this for getting the resource pathname: 可能您必须这样做才能获取资源路径名:
window_icon = pkg_resources.resource_filename('socket_messenger',
'images/ic_insert_drive_file_black_48dp_1x.png')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.