尝试使用包时出现 ModuleNotFoundError

ModuleNotFoundError when trying to use package

我们有一个自定义模块可以在 Windows 10 上完美运行,但是,它在我们的 Linux 云实例 (Debian Jessie) 上失败并出现 ModuleNotFoundError。 文件夹结构如下:

|-dashboard-miner (git repo)
  |-setup.py
    |-dashboard_miner (the actual package)

我们的setup.py如下:

from setuptools import setup, find_packages
import dashboard_miner
import os

MODULE_BASEDIR = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'dashboard_miner')

try:
    long_description = open('README.md').read()
except IOError:
    long_description = ''

try:
    reqs = open(os.path.join(os.path.dirname(__file__), 'requirements.txt')).read()
except (IOError, OSError):
    reqs = ''

print(find_packages(where=MODULE_BASEDIR))

setup(
    name='dashboard_miner',
    version=dashboard_miner.get_version(short=True),
    description='Data mining utilities for True Cloud Dashboard',
    long_description=long_description,
    url='https://gitlabe1.ext.net.nokia.com/TCI/dashboard-miner',
    author='Hodossy, Szabolcs',
    author_email='szabolcs.hodossy@nokia.com',
    license='NOKIA Confidential',
    packages=find_packages(where=MODULE_BASEDIR),
    package_dir={'': 'dashboard_miner'},
    install_requires=reqs,
    entry_points={
        'console_scripts': [
            'miner=dashboard_miner.cli:main',
        ]
    },
    zip_safe=False
)

检查 dashboard_miner (the actual package) 的文件结构/树,它必须包含一个 init.py 以便它可以被 Python 识别为一个模块.

实际上,find_packages 函数返回相对路径,因此必须从正确的文件夹中调用它。