首页 > 解决方案 > 未找到自定义 python 包

问题描述

我从 python.org遵循本教程 并设法上传到 PyPI 并使用 pip 安装,但我得到的只是

ModuleNotFoundError: No module named 'tomaszslittlehelpers'

有什么建议么?

导入在本地工作,即从上面文件夹中的文件导入时。

包名是tomaszslittlehelpers

setup.py

import setuptools

with open('README.md', 'r') as fh:
    long_description = fh.read()

setuptools.setup(
        name='tomaszslittlehelpers',
        version='0.0.2',
        author='TomaszAndrzej',
        author_email='',
        description='Tomasz\'s Little Helpers',
        long_description=long_description,
        long_description_content_type='text/markdown',
        url='',
        packages=setuptools.find_packages(),
        classifiers=[
                'Programming Language :: Python :: 3',
                'License :: OSI Approved :: MIT License',
                'Operating System :: OS Independent',
                ],
        python_requires='>=3.7',

        )

__init__.py

name='tomaszslittlehelpers'

项目树:

tomaszslittlehelpers
    build
        bdist.win-amd64
    dist
        tomaszslittlehelpers-0.0.2-py3-none-any.whl
        tomaszslittlehelpers-0.0.2.tar.gz
    tomaszslittlehelpers.egg-info
        dependency_links.txt
        PKG-INFO
        SOURCES.txt
        top_level.txt
    __init__.py
    LICENSE
    README.md
    setup.py

pip install tomaszslittlehelpers
安装到

C:\users ... \python37\Lib\site-packages\tomaszslittlehelpers-0.0.1.dist-info 没有tomaszslittlehelpers文件夹

标签: pythonpippypi

解决方案


你的包装有问题。您的代码不会添加到您的发行版中。您正在使用packages=setuptools.find_packages(),,但似乎没有任何要寻找的包。看起来您的代码位于__init__.py项目根目录的文件中。这很可能是行不通的。

两种解决方案:

  • 重命名__init__.pytomaszslittlehelpers.py并替换packages=setuptools.find_packages(),py_modules=['tomaszslittlehelpers'],.

  • 将您的移动__init__.py到一个tomaszslittlehelpers子目录,并且find_packages()应该能够找到它。

在这两种情况下,您都应该能够像这样导入您的代码:import tomaszslittlehelpers.


推荐阅读