初学者运行Pycharm程序可能会出现的问题,及解决办法

发布于:2025-06-12 ⋅ 阅读:(21) ⋅ 点赞:(0)

文章目录


前言

以下介绍初学者运行Pycharm程序可能会出现的问题,及解决办法。


一、ModuleNotFoundError: No module named ‘sklearn’

问题概括: Python 运行时提示找不到’sklearn’ 模块,说明系统未安装 scikit-learn 库。出现ModuleNotFoundError类似的错误,都是缺少库,安装引号内的库即可。部分库的名称和引号提示的名称不一致,例子的报错是’sklearn’,但安装的是scikit-learn。常见名称不一致的库如图1所示。

解决方案: 使用包管理器安装 scikit-learn,若用 pip 则执行pip install scikit-learn,若用 conda 则执行conda install scikit-learn。可在两个地方进行安装,如图2,图3。(注:安装库的前提是已经创建好虚拟环境,并把虚拟环境连接 PyCharm中。如果没进行这两步,可依次点击下面链接进行操作。)
创建虚拟环境
虚拟环境连接 PyCharm
图1

图1常见名称不一致的库

在这里插入图片描述

图2 pycharm中安装库

在这里插入图片描述

图3 Anaconda Prompt (ANaconda 3)中安装库

二、ImportError: cannot import name ‘show_config’ from ‘numpy’ (unknown location)

问题概括: 原因报错是由于Scipy与Numpy版本不兼容导致的,一般是由于Scipy版本过高,而Numpy版本过低。
Scipy 1.1.0 + Numpy 1.15.0 版本完全兼容

pip uninstall numpy
pip install numpy==1.15.0

三、Pycharm报错:“Original error was: DLL load failed: 找不到指定的模块“ 的解决办法

打开Anaconda prompt–-conda activate +你的虚拟环境名

pip uninstall -y numpy
pip uninstall -y setuptools
pip install setuptools
pip install numpy

四、ImportError: cannot import name ‘_imaging’ from 'PIL’ImportError: cannot import name ‘_imaging’ from ‘PIL’

卸载:pip uninstall Pillow
重装:pip uninstall Pillow

五、ImportError: bad magic number in ‘vlib.layers’: b’\x03\xf3\r\n’

解决办法:删除项目中所有的 .pyc 文件。

六、AttributeError: module ‘backend_interagg‘ has no attribute ‘FigureCanvas‘的解决办法

加入:

import matplotlib
matplotlib.use('TkAgg')

七、WARNING: Skipping D:\1Python_text\Anaconda3\envs\jupter_huan\Lib\site-packages\numpy-1.21.6.dist-info due to invalid metadata entry ‘name’

删除路径上numpy-1.21.6.dist-info文件夹

八、AttributeError: ‘NoneType’ object has no attribute ‘split’

应该是numpy的包出现问题,需要重新安装numpy+mkl

九、TypeError: warn() missing 1 required keyword-only argument: ‘stacklevel’

pip uninstall traitlets
pip install traitlets==5.9.0

十、ImportError: cannot import name ‘if_delegate_has_method’ from ‘sklearn.utils.metaestimators’ (D:\1Python_text\Anaconda3\envs\mmd\lib\site-packages\sklearn\utils\metaestimators.py)

pip install --user scikit-learn==0.23.1 -i https://pypi.tuna.tsinghua.edu.cn/simple

十一、Python错误:AttributeError: module ‘tensorflow’ has no attribute ‘Session’

import tensorflow.compat.v1 as tf

十二、ModuleNotFoundError: No module named ‘_distutils_hack’

使用记事本或者sublime Text等软件打开/home/user/anaconda3/envs/SWINTS/lib/python3.8/site-packages/distutils-precedence.pth这个文件,在该文件的import os;后边敲击一个回车键,然后保存即可。

十三、Could not build wheels for opencv-python which use PEP 517 and cannot be installed directly

conda install py-opencv

十四、ERROR: Could not build wheels for opencv-python which use PEP 517 and cannot be installed directly

pip install --upgrade pip

十五、ERROR: Could not build wheels for opencv-python, which is required to install pyproject.toml-based projects

pip install -i https://pypi.douban.com/simple/ pip install opencv-python==4.3.0.38

十六、ModuleNotFoundError: No module named ‘tensorflow.contrib‘

出现 “ModuleNotFoundError: No module named ‘tensorflow.contrib’” 错误通常是因为在 TensorFlow 2.0 版本中,tensorflow.contrib 模块已被移除。使用tensorfolw.1.xhttps://blog.csdn.net/weixin_

十七、AttributeError: module ‘wandb’ has no attribute ‘init’

pip install wandb

十八、CondaHTTPError: HTTP 000 CONNECTION FAILED for url https://repo.anaconda.com/pkgs/main/win-32/current_repodata.json

Elapsed: -

An HTTP error occurred when trying to retrieve this URL.
HTTP errors are often intermittent, and a simple retry will get you on your way.

If your current network has https://www.anaconda.com blocked, please file
a support request with your network engineering team.

SSLError(MaxRetryError(‘HTTPSConnectionPool(host=‘repo.anaconda.com’, port=443): Max retries exceeded with url: /pkgs/main/win-32/current_repodata.json (Caused by SSLError(SSLError(“bad handshake: Error([(‘SSL routines’, ‘ssl3_
get_server_certificate’, ‘certificate verify failed’)],)”,),))’,),)

解决方法参考

十九、‘tuple’ object has no attribute ‘size’

pip install torch-summary

二十、 UserWarning: CUDA initialization: CUDA unknown error - this may be due to an incorrectly set up environment, e.g. changing env variable CUDA_VISIBLE_DEVICES after program start. Setting the available devices to be zero. (Triggered internally at /opt/conda/conda-bld/pytorch_1614378083779/work/c10/cuda/CUDAFunctions.cpp:109.)

win+r打开运行,输入devmgmt.msc打开设备管理器
向下拉直到展开显示适配器,右键单击第二项
点击启用设备即可打开GPU

二十一、ERROR: pip’s dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conf licts. torchaudio 2.4.1 requires torch ==2.4.1, but you have torch 2.0.0+cu118 which is incompatible. torchvision 0.19.1 requires torch ==2.4.1, but you have torch 2.0.0+cu118 which is incompatible.

解决PyTorch相关库的版本冲突问题
即torch版本高,而torchvision torchaudio低的问题,安装制定版本的

pip install torchvision == 0.15.1  torchaudio ==2.0.1 -i https://pypi.tuna.tsinghua.edu.cn/simple