load_entry_point 没有看到其他模块 - "No module named" 错误

load_entry_point doesnt see other modules - "No module named" error

我在通过 pip install 创建的脚本 运行 安装我的 python 应用程序时遇到了问题。 ModuleNotFoundError 发生的原因对我来说是隐藏的:/

#jsdc
Traceback (most recent call last):
  File "/usr/local/bin/jsdc", line 6, in <module>
    from src.sensor_data_collector import main
  File "/usr/local/lib/python3.7/site-packages/src/sensor_data_collector.py", line 6, in <module>
    from gatherer import Gatherer
ModuleNotFoundError: No module named 'gatherer'

当我直接通过入口点 运行 应用时,一切 运行 都很顺利:

python /usr/local/lib/python3.7/site-packages/src/sensor_data_collector.py

项目 setup.py:

import os
import pathlib
import subprocess
from setuptools import setup, find_packages
from setuptools.command.install import install

HERE = pathlib.Path(__file__).parent

README = (HERE / "README.md").read_text()


def run(self):
    install.run(self)
    current_dir_path = os.path.dirname(os.path.realpath(__file__))
    create_service_script_path = os.path.join(current_dir_path, 'install_scripts', 'create_service.sh')
    subprocess.check_output([create_service_script_path])


setup(
    name="jacfal-sensor-data-collector",
    version="0.0.1",
    description="Collects data from iot sensors and sent them to defined target",
    long_description=README,
    long_description_content_type="text/markdown",
    url="https://github.com/Jacfal/SensorDataCollector",
    author="Jacfal",
    author_email="jacfal.tech@protonmail.com",
    license="MIT",
    classifiers=[
        "License :: OSI Approved :: MIT License",
        "Programming Language :: Python :: 3.7",
    ],
    packages=find_packages(),
    entry_points={
        "console_scripts": ["jsdc=src.sensor_data_collector:main", ]
    },
)

项目文件结构:

.
├── install_scripts
│   └── create_service.sh
├── install.sh
├── LICENSE
├── README.md
├── requirements.txt
├── setup.py
├── src
│   ├── configuration.py
│   ├── config.yml
│   ├── event.py
│   ├── gatherer.py
│   ├── helpers
│   │   ├── __init__.py
│   │   └── sensor_helpers.py
│   ├── __init__.py
│   ├── sensor_data_collector.py
│   ├── sensor.py
│   ├── sensors
│   │   ├── dummy_sensor.py
│   │   └── __init__.py
│   ├── targets
│   │   ├── influxdb_target.py
│   │   ├── __init__.py
│   │   └── log_target.py
│   └── target_system.py
└── tests

我是第一次使用 setup.py,所以我很乐意提供任何帮助或建议。谢谢。

图片中缺少一些东西。您似乎没有顶级包,并且您的发行版被构建为好像将 src 作为顶级包名称 - 尝试 import src.gatherer 并且它会 "work".

当然,正确的解决方法是拥有一个正确的顶级包,这意味着您需要在内部创建一个目录src(也许是sensor_data_collector?)并将所有文件移动到那个目录;并使用 find_packages('src')。然后你需要用这个前缀所有导入。