【项目管理】【Python】终端报错:ImportError: cannot import name ‘str_to_long‘ from ‘math‘ (unknown location) 【解决】

一、问题描述

笔者最近在做一个Python密码学相关项目时,遇到如下报错:

ImportError: cannot import name 'str_to_long' from 'math' (unknown location)

报错截图:

【项目管理】【Python】终端报错:ImportError: cannot import name ‘str_to_long‘ from ‘math‘ (unknown location) 【解决】_第1张图片

出错的代码行(导入模组时出错):

from math import str_to_long, long_to_str, getbigprime

二、出错原因

笔者自定义了一个math数学计算模块,其中包含str_to_long和long_to_str等函数,和Python内置的math模块重名,导致无法成功导入。

三、解决方法

将math.py文件重命名为mymath.py即可。

【项目管理】【Python】终端报错:ImportError: cannot import name ‘str_to_long‘ from ‘math‘ (unknown location) 【解决】_第2张图片

导入模组的代码变成:

from mymath import str_to_long, long_to_str, getbigprime

问题解决,成功运行。

【项目管理】【Python】终端报错:ImportError: cannot import name ‘str_to_long‘ from ‘math‘ (unknown location) 【解决】_第3张图片

你可能感兴趣的:(python,密码学,项目管理,python,开发语言,pycharm)