使用Python 3.6中的setuptools打包后的ModuleNotFoundError

时间:2018-02-23 16:35:27

标签: python python-3.x setuptools

我正在尝试使用setuptools从Python3.6应用程序生成一个包。虽然打包终止没有错误,但setuptools生成的命令行程序无法导入包中的模块。 以下是我项目的目录树。

.
├── MANIFEST.in
├── README.md
├── README.rst
├── contributors.txt
├── setup.cfg
├── setup.py
├── sonicparanoid
│   ├── __init__.py
│   ├── __pycache__
│   │   ├── __init__.cpython-36.pyc
│   │   ├── inpyranoid.cpython-36.pyc
│   │   ├── length_difference_filter.cpython-36.pyc
│   │   ├── ortholog_detection.cpython-36.pyc
│   │   ├── reads_stats.cpython-36.pyc
│   │   ├── seq_tools.cpython-36.pyc
│   │   ├── sonicparanoid.cpython-36.pyc
│   │   ├── sys_tools.cpython-36.pyc
│   │   └── workers.cpython-36.pyc
│   ├── bin
│   │   └── mmseqs
│   ├── blast_tools.py
│   ├── compile_inpyranoid_c.py
│   ├── compile_mmseqs_parser_c.py
│   ├── config.json
│   ├── example
│   │   ├── test_input
│   │   │   ├── chlamydia_trachomatis
│   │   │   ├── deinococcus_radiodurans
│   │   │   ├── gloeobacter_violaceus
│   │   │   └── thermotoga_maritima
│   │   └── test_output
│   │       └── README.txt
│   ├── inpyranoid.py
│   ├── inpyranoid_c.c
│   ├── inpyranoid_c.cpython-36m-darwin.so
│   ├── inpyranoid_c.pyx
│   ├── length_difference_filter.py
│   ├── mmseqs2_src
│   │   ├── README.txt
│   │   └── mmseqs.tar.gz
│   ├── mmseqs_parser_c.c
│   ├── mmseqs_parser_c.cpython-36m-darwin.so
│   ├── mmseqs_parser_c.pyx
│   ├── mmseqs_parser_cython.py
│   ├── ortholog_detection.py
│   ├── quick_multi_paranoid
│   │   ├── Makefile
│   │   ├── Makefile.in
│   │   ├── config
│   │   ├── dump.cpp
│   │   ├── gen_header.cpp
│   │   ├── hashtable.c
│   │   ├── hashtable.h
│   │   ├── hashtable.o
│   │   ├── hashtable_itr.c
│   │   ├── hashtable_itr.h
│   │   ├── hashtable_private.h
│   │   ├── ortholog.c
│   │   ├── qa.h
│   │   ├── qa1
│   │   ├── qa1.cpp
│   │   ├── qa2
│   │   ├── qa2.cpp
│   │   ├── qp
│   │   ├── qp.c
│   │   └── qps.c
│   ├── reads_stats.py
│   ├── seq_tools.py
│   ├── setup_sonicparanoid.py
│   ├── sonicparanoid.py
│   ├── sys_tools.py
│   ├── test_blast_tools.py
│   ├── test_length_difference_filter.py
│   ├── test_ortholog_detection.py
│   ├── test_reads_stats.py
│   ├── test_seq_tools.py
│   ├── test_sonicparanoid.py
│   ├── test_sys_tools.py
│   └── workers.py
└── user_manual.pdf

如果我运行python3 sonicparanoid.py它没有问题,但如果我使用setuptools创建分发,每当主程序(sonicparanoid.py)尝试导入其中的任何其他.py模块时,我将收到导入错误package(sonicparanoid)目录。

以下是我的setup.py:

enter code here
from setuptools import setup, find_packages
from setuptools.extension import Extension
from Cython.Build import cythonize
import numpy

extensions = [
    Extension(
        "sonicparanoid.inpyranoid_c",
        ["sonicparanoid/inpyranoid_c.pyx"],
        include_dirs=[numpy.get_include()],
    ),
    Extension(
        "sonicparanoid.mmseqs_parser_c",
        ["sonicparanoid/mmseqs_parser_c.pyx"],
        include_dirs=[numpy.get_include()],
    ),
]


from codecs import open
from os import path

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

# Get the long description from the README file
with open(path.join(here, 'README.rst'), encoding='utf-8') as f:
    long_description = f.read()

setup(
    name='sonicparanoid',
    version='0.0.2',  # Required
    description='Whatever',
    long_description=long_description,  # Optional
    url='http://iwasakilab.bs.s.u-tokyo.ac.jp/sonicparanoid/',  # Optional
    author='Me',
    author_email='my@email.com',
    classifiers=[  # Optional
        'Development Status :: 5 - Production/Stable',
        'Environment :: Console',
        'Intended Audience :: Science/Research',
        'Topic :: Scientific/Engineering :: Bio-Informatics',
        'License :: OSI Approved :: GNU General Public License v3 (GPLv3)',
        'Programming Language :: Python :: 3.4',
        'Programming Language :: Python :: 3.5',
        'Programming Language :: Python :: 3.6',
    ],
    packages = ['sonicparanoid',], # Required
    install_requires=['numpy>=1.14.0', 'pandas>=0.22.0', 'cython>=0.27.0',     'sh>=1.12.14', 'setuptools>=24.2.0'], # specify minimum version
    python_requires='>=3.5, <3.7',
    ext_modules = cythonize(extensions, compiler_directives={'language_level': 3}),
    package_dir={'sonicparanoid': 'sonicparanoid/'},
    include_package_data=True,
    package_data={'sonicparanoid': ['example/test_output/*', 'example/test_input/*', 'mmseqs2_src/*', 'quick_multi_paranoid/*']
                },
    entry_points={  # Optional
        'console_scripts': [
            'sonicparanoid = sonicparanoid.sonicparanoid:main',
        ],
    },
)

导入语句在sonicparanoid.py中的显示方式:

import os
import sys
import platform
import seq_tools as seqtools

目前一切正常,如果我执行python3 sonicparanoid.py,但是当我使用使用setuptools生成的程序时,我收到以下错误:

Traceback (most recent call last):
  File "/usr/local/bin/sonicparanoid", line 11, in <module>
    load_entry_point('sonicparanoid==0.0.2', 'console_scripts',     'sonicparanoid')()
  File "/usr/local/lib/python3.6/site-packages/pkg_resources/__init__.py", line 572, in load_entry_point
    return get_distribution(dist).load_entry_point(group, name)
  File "/usr/local/lib/python3.6/site-packages/pkg_resources/__init__.py", line 2755, in load_entry_point
    return ep.load()
  File "/usr/local/lib/python3.6/site-packages/pkg_resources/__init__.py", line 2408, in load
    return self.resolve()
  File "/usr/local/lib/python3.6/site-packages/pkg_resources/__init__.py",     line 2414, in resolve
    module = __import__(self.module_name, fromlist=['__name__'], level=0)
  File "/usr/local/lib/python3.6/site-packages/sonicparanoid-0.0.2-py3.6-    macosx-10.13-x86_64.egg/sonicparanoid/sonicparanoid.py", line 5, in <module>
    import seq_tools as seqtools
ModuleNotFoundError: No module named 'seq_tools'

我尝试使用此question中提出的解决方案, 但如果我更改导入,例如from sonicparanoid import seq_tools as seqtools并运行python3 sonicparanoid.py,我会收到导入错误ImportError: cannot import name 'seq_tools'

任何帮助都会非常感激,这是我第一次尝试包装,这非常令人沮丧。

1 个答案:

答案 0 :(得分:4)

Python使用sys.path查找模块/包,并将脚本的目录预先添加到sys.path的开头。考虑到这一点,让我来看看导入的细节。

运行python sonicparanoid/sonicparanoid.py时,Python会将目录sonicparanoid/添加到sys.path。现在,当目录位于sys.path时,脚本可以直接导入seq_tools,因为模块seq_tools.py位于sys.path的目录中。

当您安装软件包并运行生成的入口点sonicparanoid时,目录sonicparanoid/不在sys.path中(但其父项是),Python无法导入seq_tools。您必须将其导入为sonicparanoid.seq_tools。但这意味着当您将sonicparanoid.py作为脚本运行时,无法从sonicparanoid.py导入它!

底线:不要将sonicparanoid.py作为脚本运行,因为sys.path与运行入口点导入sonicparanoid包太不相同了。

此外,您的脚本不能与Python包的名称相同。当你有一个包sonicparanoid和一个脚本sonicparanoid.py并且脚本试图import sonicparanoid时 - Python尝试从脚本导入(因为它是sys.path中的第一个)而不是包裹失败了。

您可以为脚本命名sonicparanoid(不.py扩展名,Python不会尝试从中导入)或sonic_paranoid.py但不是sonicparanoid.py