构建并安装Python wheel包后导入出现ModuleNotFoundError的问题
问题:Wheel包安装成功但导入失败(ModuleNotFoundError)
我把完整Python项目打包成wheel文件,按网上步骤构建并安装成功,但在同一环境中导入时出现ModuleNotFoundError。操作及报错信息如下:
(sancus) C:\Users\XXXX\Downloads\sancus-master\sancus-master\sancus\dist>pip install sancus_extraction-0.0.1-py3-none-any.whl Processing c:\users\XXXX\downloads\sancus-master\sancus-master\sancus\dist\sancus_extraction-0.0.1-py3-none-any.whl Installing collected packages: sancus-extraction Successfully installed sancus-extraction-0.0.1
(sancus) C:\Users\XXXX\Downloads\sancus-master\sancus-master\sancus\dist>python Python 3.9.13 (main, Aug 25 2022, 23:51:50) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32 Warning: This Python interpreter is in a conda environment, but the environment has not been activated. Libraries may fail to load. To activate this environment please see https://conda.io/activation Type "help", "copyright", "credits" or "license" for more information. >>> import sancus_extraction Traceback (most recent call last): File "<stdin>", line 1, in <module> ModuleNotFoundError: No module named 'sancus_extraction' >>>
解决方法
重新激活conda环境:命令行前缀虽显示
(sancus),但Python提示环境未激活。重新执行激活命令后再尝试导入:conda activate sancus python >>> import sancus_extraction核对模块实际导入名称:wheel包名称
sancus-extraction不一定等于导入名称。可以:- 执行
pip show sancus-extraction查看包的安装路径 - 进入该路径,查看里面的文件夹/模块文件名,比如如果是
sancus文件夹,导入命令应为import sancus
- 执行
检查打包配置文件:确认
setup.py或pyproject.toml中packages参数是否正确指向要打包的模块,示例:setup( name="sancus-extraction", packages=["sancus_extraction"], # 需与实际模块名一致 # 其他配置项... )验证pip与Python环境一致性:执行
where python和where pip(Windows),或which python和which pip(Linux/macOS),确认两者路径都属于sancus环境,避免pip安装到其他环境而Python调用的是另一个解释器。
内容的提问来源于stack exchange,提问作者hitesh
相关产品推荐
相关产品推荐

