python - 执行 python setup.py 测试时出现导入错误

标签 python bash unit-testing import

我正在制作一个简单的Python应用程序。我不知道我这样做是否正确,所以请在评论中纠正我,或者您对此有答案

错误:

ImportError: No module named 'taskhandler'

和:

ImportError: No module named 'styles' while doing `python3 setup.py test

文件结构:

.
├── MANIFEST.in
├── pydotask.egg-info
│   ├── dependency_links.txt
│   ├── not-zip-safe
│   ├── PKG-INFO
│   ├── SOURCES.txt
│   └── top_level.txt
├── README.md
├── setup.py
├── task_mod
│   ├── __init__.py
│   ├── __pycache__
│   │   ├── __init__.cpython-35.pyc
│   │   ├── pydo.cpython-35.pyc
│   │   └── taskhandler.cpython-35.pyc
│   ├── pydo.py
│   ├── styles
│   │   ├── __init__.py
│   │   ├── __pycache__
│   │   │   ├── __init__.cpython-35.pyc
│   │   │   ├── termcolor.cpython-35.pyc
│   │   │   └── text_style.cpython-35.pyc
│   │   ├── termcolor.py
│   │   └── text_style.py
│   ├── taskhandler.py
│   └── tasks.csv
└── update.txt

5 directories, 22 files

'task_mod/pydo.py':

#!/usr/bin/env python3

''' To Do App in Python '''

import sys, os
import taskhandler as task
from styles import text_style as text
from styles import termcolor

task_mod/taskhandler.py:

#!/usr/bin/env python3

import sys, os
import csv
from styles import termcolor
from styles import text_style as text

setup.py

from setuptools import setup

def readme():
    with open('README.md') as readme:
        return readme.read()

setup(
        name = 'pydotask',
        version = '0.2',
        description = 'PyDo is a CLI Application to keep you on track with your tasks and projects',
        long_description = readme(),
        classifiers = [
            'Development Status :: 3 - Alpha',
            'Programming Language :: Python :: 3.5',
            'Topic :: Office/Business :: Scheduling'
            ],
        keywords = 'utilities office schedule task reminder',
        url = '',
        author = 'Abhishta Gatya',
        author_email = 'abhishtagatya@yahoo.com',
        packages = ['task_mod'],
        scripts = ['task_mod/pydo'],
        python_requires = '>=3',
        include_package_data = True,
        zip_safe = False
        )

如何解决这个问题?

注意:如果我运行 python3 task_mod/pydo.py 它工作正常!但是当我尝试测试它时,它给出了 2 个 ImportErrors。

最佳答案

您必须指定 setup.py 中使用的所有模块,而不仅仅是顶级文件夹。因此,在 setup.py 文件中,将行 packages = ['task_mod'], 替换为 packages = ['task_mod', 'task_mod.styles', 'task_mod.taskhandler'], .

或者,在不更改 setup.py 的情况下,您可以使用 import task_mod.styles 或使用 from task_mod import styles 进行导入。然后您可以使用 styles.termcolor 等样式的文件。

或者,您可以使用setuptool的黑魔法函数find_packages,如下所示:packages = find_packages(),

Related SO post

关于python - 执行 python setup.py 测试时出现导入错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46504262/

相关文章:

python - 与南方陷入移民冲突

arrays - 是否可以将 bash 数组作为变量传递给 awk?

python - 在输入提示符下测试 python stdout

java - Android Studio单元测试: Issues in writing mock tests for volley request (finished with non-zero exit value 1)

vb.net - 使用 Microsoft Fakes 模拟数据库

python - 内置模块计算准确时差

python - 如何将 Python 数据集(之前从 IDL 导出)保存回 IDL 格式

python - Python 3.5 支持的 dask 版本是什么?

linux - 在 bash 脚本中获取子字符串

linux - Grep 查找行中的多个值并仅输出多个值