使用安装工具打包后导入时出错

时间:2019-07-16 14:25:36

标签: pip python-3.6 setuptools egg

我正在尝试使用设置工具将我的项目打包为python中的库。作为我的第一次,我将项目安排在以下目录中。从其父目录导入程序包不会导致任何错误,并且可以正常工作。但是,当我使用安装工具安装它并尝试从常规目录导入时,出现了在Packa中导入文件的导入错误 目录结构如下:

    driver_scoring
│   ├── config
│   │   ├── config.py
│   ├── processing
│   │   ├── data_management.py
│   │   ├── scoring_components.py
│   │   └── validation.py
│   ├── __init__.py
│   ├── pipeline.py
│   ├── predict.py
│   ├── train_pipeline.py
│   └── VERSION
├── MANIFEST.in
├── requirements.txt
└── setup.py

在我的 init .py文件中,我包含了以下代码:

import os

from driver_scoring.config import config

with open(os.path.join(config.PACKAGE_ROOT, 'VERSION')) as version_file:
    __version__ = version_file.read().strip()

我收到以下错误:

      1 import os
      2 
----> 3 from driver_scoring.config import config
      4 
      5 with open(os.path.join(config.PACKAGE_ROOT, 'VERSION')) as version_file:

ModuleNotFoundError: No module named 'driver_scoring.config'

我的setup.py如下:

NAME = 'driver_scoring'
DESCRIPTION = ''
URL = ''
EMAIL = ''
AUTHOR = 'Nevin Baiju'
REQUIRES_PYTHON = '>=3.6.0'

def list_reqs(fname='requirements.txt'):
    with open(fname) as fd:
        return fd.read().splitlines()

here = os.path.abspath(os.path.dirname(__file__))

try:
    with io.open(os.path.join(here, 'README.md'), encoding='utf-8') as f:
        long_description = '\n' + f.read()
except FileNotFoundError:
    long_description = DESCRIPTION

ROOT_DIR = Path(__file__).resolve().parent
PACKAGE_DIR = ROOT_DIR / NAME
about = {}
with open(PACKAGE_DIR / 'VERSION') as f:
    _version = f.read().strip()
    about['__version__'] = _version

setup(
    name=NAME,
    version=about['__version__'],
    description=DESCRIPTION,
    long_description=long_description,
    long_description_content_type='text/markdown',
    author=AUTHOR,
    author_email=EMAIL,
    python_requires=REQUIRES_PYTHON,
    url=URL,
    packages=find_packages(exclude=('tests',)),
    package_data={'driver_scoring': ['VERSION']},
    install_requires=list_reqs(),
    extras_require={},
    include_package_data=True,
    license='MIT',
    classifiers=[
        'License :: OSI Approved :: MIT License',
        'Programming Language :: Python',
        'Programming Language :: Python :: 3',
        'Programming Language :: Python :: 3.6',
        'Programming Language :: Python :: Implementation :: CPython',
        'Programming Language :: Python :: Implementation :: PyPy'
    ],
)

当我尝试从项目的父目录导入它时,我没有收到任何错误,并且能够根据需要运行该项目。但是,当我尝试从已安装的目录安装和导入项目时,出现导入错误。

1 个答案:

答案 0 :(得分:0)

问题由我自己解决。我忘了在配置文件夹中添加 init .py文件,这就是导致导入错误的原因。