很久没配置 python 新环境了,最近新项目需要进行配置,在配置过程中发现了不少问题,记录下。
问题1:fatal error: longintrepr.h: 没有那个文件或目录
这个问题的原因是新环境的 python 版本(3.10以上)与本地的版本(3.8.x)差异过大造成的,降版本(≤3.9)即可。但后面看了下,longintrepr.h 是在编译 .c 文件中用到。
问题2:PIL _typing module ‘numpy’ has no attribute ‘ndarray’
这个原因主要是 PIL 和 numpy 版本不匹配的原因,安装下面版本可以解决:
numpy==1.20.3
Pillow==8.0.1
问题3:AttributeError: module ‘numpy’ has no attribute ‘float’
这个是因为 np.float 从 1.24 版本起被删除了,需要降版本或者将 numpy 的别名替换为 python 类型:
np.float = float
# np.float = np.float64
np.int = int
np.object = object
np.bool = bool
问题4:官网或者镜像网站找不到对应的python版本
由于本地的 python 版本比较低,官网上最低的版本都是 3.9,所以找了一圈也没到,但可以通过 conda create 创建新环境并指定相应的 python 版本号即可。
问题5:requirements.txt中的包的版本问题
在查找 python 依赖时使用了 pipreqs,然后发现生成 .txt 中某些包的版本比实际使用的版本要高,原因是它直接从 pypi.python.org 中找, 需要加上 --use-local 才是对本地包进行查找。
问题6:计算公式精度问题
.pyc可以用uncompyle6进行反编译,从而得到源码,极不安全,因此最好将代码编辑成动态链接库。然后问题就来了,代码里有一个计算公式,用了整分数(循环)表示,导致.pyd与.py文件的运行结果天差地别,最终将其用小数表示两者才一致。主要原因是两者对数据的截断存在差异。
问题7:PIL TypeError: Cannot handle this data type: (1, 1, 3), <f4
问题出在数组的 float (0–1) 类型上。将数组转换为 Uint (0–255):
im = Image.fromarray((x * 255).astype(np.uint8))
或者直接使用skimage:
from skimage.transform import resize
bottle_resized = resize(bottle, (140, 54))
参考:
https://blog.csdn.net/qq_47554267/article/details/129963722
https://zhidao.baidu.com/question/1702772342973084108.html
https://blog.csdn.net/qq_45934285/article/details/131120167
https://www.cnblogs.com/zhaopanpan/p/9383350.html
https://blog.csdn.net/juzicode00/article/details/124580320
https://docs.pingcode.com/baike/1192416
https://stackoverflow.com/questions/60138697/typeerror-cannot-handle-this-data-type-1-1-3-f4
https://stackoverflow.com/questions/48121916/numpy-resize-rescale-image