You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

已安装Python Wheel包但出现ModuleNotFoundError问题求助

问题排查与修复方案

核心原因分析

你的setup.py配置和文件夹结构不匹配,导致安装后的testai包并未包含预期的模块:

  1. 文件夹结构缺陷:

    • 子目录testai(dir with code)缺少__init__.py,无法被识别为Python包。
    • 你期望的主包是testai,但当前结构下只有src是合法子包,而testai本身不是包。
  2. setup.py配置错误:

    • package_dir={"": "testai"}将包的根目录指向了子目录testai,这意味着find_packages(where="testai")只会找到该目录下的src包。
    • 最终安装的内容是src包(而非testai包),所以尝试import testai时会提示模块找不到,而pip show testai显示的只是你在setup.py中定义的包元信息,和实际安装的模块结构无关。

修复步骤

步骤1:修正文件夹结构

给子目录testai(dir with code)添加__init__.py,让它它成为合法的testai主包,同时删除base-dir下多余的__init__.py:

testai(base-dir)
|__>testai(dir with code)
   |__>__init__.py  # 新增此文件
   |__>src(dir)
       |__>core(dir)
          |__>config.py
          |__>__init__.py
       |__>storage(dir)
          |__>dummystorage.py 
          |__>__init__.py
       |__>__init__.py
|__>setup.py
|__>README.md

步骤2:调整setup.py配置

修改package_dir和packages参数,让setuptools正确识别testai主包及其子包:

import setuptools
from setuptools import find_packages
import pathlib, os
root=pathlib.Path(__file__).parent
os.chdir(str(root))
with open("README.md", "r", encoding="utf-8") as fh:
  long_description = fh.read()

setuptools.setup(
name='testai',
version='0.1.0',
author='test',
author_email='test@test.ai',
description='Testing installation of Package',
long_description=long_description,
long_description_content_type="text/markdown",
# 修改:包的根目录为当前base-dir
package_dir={"": "."},  
# 修改:从当前目录查找所有包(包括testai主包和其下的src子包)
packages=find_packages(where="."),  

url='',
project_urls = {
    "Bug Tracker": "https://github.com/"
},
license='MIT',
install_requires=['requests','aiohttp','pydantic','urllib3','boto3','gcloud','google-cloud-storage','aiocache','databases'],
classifiers=[
    "Development Status :: 1 - Planning",
    "Intended Audience :: Developers",
    "Operating System :: Unix",
    "Operating System :: MacOS :: MacOS X",
    "Operating System :: Microsoft :: Windows",
    "Programming Language :: Python :: 3",
    "Programming Language :: Python :: 3.7",
    "Programming Language :: Python :: 3.8",
    "Programming Language :: Python :: 3.9",
    "Programming Language :: Python :: 3.10",
    "Programming Language :: Python :: 3 :: Only",
]
)

步骤3:重新构建并安装

执行以下命令重新生成wheel包并安装:

# 卸载旧包
pip uninstall -y testai
# 重新构建wheel
python setup.py sdist bdist_wheel
# 安装新包
pip install dist/testai-0.1.0-py3-none-any.whl

验证安装

安装完成后,尝试导入模块:

# 导入主包
import testai
# 导入子模块
from testai.src.core import config
from testai.src.storage import dummystorage

如果希望直接从testai导入core或storage,可以在testai(dir with code)/__init__.py中添加导入语句:

from .src.core import *
from .src.storage import *

之后就可以直接使用:

from testai import config

内容的提问来源于stack exchange,提问作者Prateek Singh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.12 19:35:15