Python中常见错误有哪些总结?

2026-06-10 20:181阅读0评论SEO资源
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计848个文字,预计阅读时间需要4分钟。

Python中常见错误有哪些总结?

在使用Python进行开发时,经常会遇到各种各样的问题。以下是一些常见错误,以及如何快速解决它们:

1. `IndentationError: unexpected indent`:这种错误表明缩进格式不正确。确保所有代码块的缩进一致,通常使用4个空格或一个制表符。

2. `NameError: name 'variable' is not defined`:当你尝试使用一个未定义的变量时,会出现此错误。确保所有使用的变量在引用之前已经声明和初始化。

3. `TypeError: 'int' object is not iterable`:试图迭代一个整数或其他非可迭代对象时,会出现此错误。确保你尝试迭代的变量是一个列表、元组、字典或其他可迭代的类型。

4. `IndexError: list index out of range`:当你尝试访问列表中不存在的索引时,会出现此错误。确保索引值在列表的有效范围内。

5. `ValueError: invalid literal for int() with base 10: 'text'`:尝试将非数字字符串转换为整数时,会出现此错误。确保输入值可以转换为所需的数据类型。

通过注意这些常见错误,你可以更快地解决Python开发中的问题。

在使用python进行开发的时候,经常会遇到各种各样的问题,在此总结一些常见的错误,可以帮助快速的排除故障,解决问题

1.

IndentationError: unexpected indent

这种错误显而易见,是缩进出现了问题,检查一下缩进,即可解决

2.

ImportError: No module named '_tkinter'

  说明: 这个模块其实是内置,但是依赖于你的系统

  1) centos

    a)查看是否存在

yum list installed | grep ^tk

tk.x86_64 1:8.5.13-6.el7 @base
tk-devel.x86_64 1:8.5.13-6.el7 @base
tkinter.x86_64 2.7.5-86.el7 @base

    b)不存在,则进行安装

yum install -y tkinter
yum install -y tk-devel

    c)正常安装之后,一般需要重新编译一下python才生效

进入python安装包中

cd /usr/local/Python-3.5.2/
./configure --prefix=/usr/local/python35
make
make install

rm /usr/bin/python3
ln /usr/local/python35/bin/python3.5 /usr/bin/python3

注: 已经存在python3软连,需要先删除

3.may have been in progress in another thread when fork

mac下执行开启多进程执行任务

解决:

  

打开pycharm,点击run,点击editConfig,点击environment varibles 将下面一段copy进去即可
OBJC_DISABLE_INITIALIZE_FORK_SAFETY=YES

终端执行命令

Python中常见错误有哪些总结?

// 终端中执行以下命令
sudo echo "export OBJC_DISABLE_INITIALIZE_FORK_SAFETY=YES" >> ~/.bash_profile && source ~/.bash_profile

4.

ModuleNotFoundError: No module named '_ctypes'

原因:Python3中有个内置模块叫ctypes,它是Python3的外部函数库模块,它提供兼容C语言的数据类型,并通过它调用Linux系统下的共享库(Shared library),此模块需要使用CentOS7系统中外部函数库(Foreign function library)的开发链接库(头文件和链接库)。
由于在CentOS7系统中没有安装外部函数库(libffi)的开发链接库软件包,所以在安装pip的时候就报了"ModuleNotFoundError: No module named '_ctypes'"的错误。

安装外部函数库(libffi)

yum install libffi-devel -y

重新安装python

yum install python

5.ERROR: Can not execute​​setup.py​​since setuptools is not available in the build environment.

pip3 install --upgrade setuptools


本文共计848个文字,预计阅读时间需要4分钟。

Python中常见错误有哪些总结?

在使用Python进行开发时,经常会遇到各种各样的问题。以下是一些常见错误,以及如何快速解决它们:

1. `IndentationError: unexpected indent`:这种错误表明缩进格式不正确。确保所有代码块的缩进一致,通常使用4个空格或一个制表符。

2. `NameError: name 'variable' is not defined`:当你尝试使用一个未定义的变量时,会出现此错误。确保所有使用的变量在引用之前已经声明和初始化。

3. `TypeError: 'int' object is not iterable`:试图迭代一个整数或其他非可迭代对象时,会出现此错误。确保你尝试迭代的变量是一个列表、元组、字典或其他可迭代的类型。

4. `IndexError: list index out of range`:当你尝试访问列表中不存在的索引时,会出现此错误。确保索引值在列表的有效范围内。

5. `ValueError: invalid literal for int() with base 10: 'text'`:尝试将非数字字符串转换为整数时,会出现此错误。确保输入值可以转换为所需的数据类型。

通过注意这些常见错误,你可以更快地解决Python开发中的问题。

在使用python进行开发的时候,经常会遇到各种各样的问题,在此总结一些常见的错误,可以帮助快速的排除故障,解决问题

1.

IndentationError: unexpected indent

这种错误显而易见,是缩进出现了问题,检查一下缩进,即可解决

2.

ImportError: No module named '_tkinter'

  说明: 这个模块其实是内置,但是依赖于你的系统

  1) centos

    a)查看是否存在

yum list installed | grep ^tk

tk.x86_64 1:8.5.13-6.el7 @base
tk-devel.x86_64 1:8.5.13-6.el7 @base
tkinter.x86_64 2.7.5-86.el7 @base

    b)不存在,则进行安装

yum install -y tkinter
yum install -y tk-devel

    c)正常安装之后,一般需要重新编译一下python才生效

进入python安装包中

cd /usr/local/Python-3.5.2/
./configure --prefix=/usr/local/python35
make
make install

rm /usr/bin/python3
ln /usr/local/python35/bin/python3.5 /usr/bin/python3

注: 已经存在python3软连,需要先删除

3.may have been in progress in another thread when fork

mac下执行开启多进程执行任务

解决:

  

打开pycharm,点击run,点击editConfig,点击environment varibles 将下面一段copy进去即可
OBJC_DISABLE_INITIALIZE_FORK_SAFETY=YES

终端执行命令

Python中常见错误有哪些总结?

// 终端中执行以下命令
sudo echo "export OBJC_DISABLE_INITIALIZE_FORK_SAFETY=YES" >> ~/.bash_profile && source ~/.bash_profile

4.

ModuleNotFoundError: No module named '_ctypes'

原因:Python3中有个内置模块叫ctypes,它是Python3的外部函数库模块,它提供兼容C语言的数据类型,并通过它调用Linux系统下的共享库(Shared library),此模块需要使用CentOS7系统中外部函数库(Foreign function library)的开发链接库(头文件和链接库)。
由于在CentOS7系统中没有安装外部函数库(libffi)的开发链接库软件包,所以在安装pip的时候就报了"ModuleNotFoundError: No module named '_ctypes'"的错误。

安装外部函数库(libffi)

yum install libffi-devel -y

重新安装python

yum install python

5.ERROR: Can not execute​​setup.py​​since setuptools is not available in the build environment.

pip3 install --upgrade setuptools