使用setup.py构建Python包后运行报错ModuleNotFoundError的解决方法
解决Python包hawkeye安装后运行命令报ModuleNotFoundError的问题
我正在尝试通过setup.py构建名为hawkeye的开源Python包,使其可通过pip3直接安装。安装过程显示成功,但运行console_scripts定义的hawk_eye命令时,出现ModuleNotFoundError,提示找不到'hawk_eye'模块。已调整目录结构但问题仍存在,相关信息如下:
setup.py代码
from setuptools import setup, find_packages setup( name='hawkeye', version='0.1.0', description='A powerful scanner to scan your Filesystem, S3, MySQL, Redis, Google Cloud Storage and Firebase storage for PII and sensitive data.', url='e', author='Rohit Kumar', author_email='', packages=find_packages('src'), package_dir={'': 'src'}, entry_points={ 'console_scripts': [ 'hawk_eye=hawk_eye:main', ], }, license='Apache License 2.0', install_requires=['pyyaml', 'rich', 'mysql-connector-python', 'redis', 'boto3'], classifiers=[ 'Development Status :: 3 - Alpha', 'Intended Audience :: Developers', 'Topic :: Software Development :: Build Tools', 'License :: OSI Approved :: Apache License 2.0', 'Programming Language :: Python :: 3', 'Programming Language :: Python :: 3.7', ], keywords='pii secrets sensitive-data cybersecurity scanner', )
项目结构
├── LICENSE ├── __init__.py ├── assets │ ├── banner.png │ ├── hawk-eye.jpg │ ├── preview.png │ └── preview2.png ├── connection.yml ├── connection.yml.sample ├── fingerprint.yml ├── hawk_eye.py ├── pyvenv.cfg ├── readme.md ├── requirements.txt ├── setup.py └── src ├── __init__.py └── hawk_eye ├── commands │ ├── __init__.py │ ├── firebase.py │ ├── fs.py │ ├── gcs.py │ ├── mysql.py │ ├── redis.py │ └── s3.py ├── internals │ └── system.py └── main.py
根目录hawk_eye.py代码
import sys import os # Add the root directory to sys.path root_dir = os.path.dirname(os.path.realpath(__file__)) sys.path.insert(0, root_dir) from src import main if __name__ == '__main__': main.main()
报错信息
Attempting uninstall: hawkeye Found existing installation: hawkeye 0.1.0 Uninstalling hawkeye-0.1.0: Successfully uninstalled hawkeye-0.1.0 DEPRECATION: hawkeye is being installed using the legacy 'setup.py install' method, because it does not have a 'pyproject.toml' and the 'wheel' package is not installed. pip 23.1 will enforce this behaviour change. A possible replacement is to enable the '--use-pep517' option. Discussion can be found at https://github.com/pypa/pip/issues/8559 Running setup.py install for hawkeye ... done Successfully installed hawkeye-0.1.0 [notice] A new release of pip is available: 23.0.1 -> 23.2.1 [notice] To update, run: pip install --upgrade pip (venv) rohitcoder@Rohits-MacBook-Pro hawk-eye % hawk_eye Traceback (most recent call last): File "/Users/rohitcoder/Desktop/Projects/hawk-eye/venv/bin/hawk_eye", line 33, in <module> sys.exit(load_entry_point('hawkeye==0.1.0', 'console_scripts', 'hawk_eye')()) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/rohitcoder/Desktop/Projects/hawk-eye/venv/bin/hawk_eye", line 25, in importlib_load_entry_point return next(matches).load() ^^^^^^^^^^^^^^^^^^^^ File "/opt/homebrew/Cellar/python@3.11/3.11.4/Frameworks/Python.framework/Versions/3.11/lib/python3.11/importlib/metadata/__init__.py", line 202, in load module = import_module(match.group('module')) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/opt/homebrew/Cellar/python@3.11/3.11.4/Frameworks/Python.framework/Versions/3.11/lib/python3.11/importlib/__init__.py", line 126, in import_module return _bootstrap._gcd_import(name[level:], package, level) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "<frozen importlib._bootstrap>", line 1204, in _gcd_import File "<frozen importlib._bootstrap>", line 1176, in _find_and_load File "<frozen importlib._bootstrap>", line 1126, in _find_and_load_unlocked File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed File "<frozen importlib._bootstrap>", line 1204, in _gcd_import File "<frozen importlib._bootstrap>", line 1176, in _find_and_load File "<frozen importlib._bootstrap>", line 1140, in _find_and_load_unlocked ModuleNotFoundError: No module named 'hawk_eye' (venv) rohitcoder@Rohits-MacBook-Pro hawk-eye %
问题原因
你的console_scripts配置路径错误:指定的hawk_eye=hawk_eye:main尝试从hawk_eye根模块导入main函数,但实际上main函数位于hawk_eye.main子模块(对应src/hawk_eye/main.py文件)中。此外,legacy安装方式可能导致包结构未正确生成。
修复步骤
修正setup.py的entry_points配置
将console_scripts行修改为指向正确的模块和函数:entry_points={ 'console_scripts': [ 'hawk_eye=hawk_eye.main:main', ], },清理旧安装并重新安装
先卸载已安装的包:pip uninstall -y hawkeye用PEP517标准重新安装(解决deprecation警告):
pip install --use-pep517 .或者先安装wheel再执行常规安装:
pip install wheel pip install .可选:添加pyproject.toml适配现代构建标准
在项目根目录创建pyproject.toml文件,避免后续安装的警告:[build-system] requires = ["setuptools>=61.0"] build-backend = "setuptools.build_meta"验证修复
重新安装后直接运行hawk_eye命令,即可正常执行。
额外说明
根目录的hawk_eye.py是本地调试脚本,和pip安装后的console_scripts无关,安装后不会被纳入包中,无需修改。
内容的提问来源于stack exchange,提问作者rohitcoder
相关产品推荐
相关产品推荐

