MirrorYuChen
MirrorYuChen
Published on 2024-12-23 / 88 Visits
0
0

Python开发过程中问题处理记录笔记

Python开发过程中问题记录笔记

1.ImportError: cannot import name ‘escape‘ from ‘jinja2‘

  • 解决方案:将jinja2版本降级到 3.1.0
>> pip install jinja2==3.0.2

2.RuntimeError: Attempting to deserialize object on CUDA device 1 but torch.cuda.device_count() is 1. Please use torch.load with map_location to map your storages to an existing device.

  • 解决办法(1):使用 map_location字段,使用GPU设备载入模型
weight = torch.load("./weights/test.pth", map_location="cuda:0")
  • 解决方法(2):将模型存到cpu上,重新保存一下模型
import torch
import torch.nn as nn

# 示例模型
class SimpleModel(nn.Module):
    def __init__(self):
        super(SimpleModel, self).__init__()
        self.fc = nn.Linear(10, 1)

    def forward(self, x):
        return self.fc(x)

# 创建模型实例
model = SimpleModel()

# 假设模型原本在 GPU 上
model = model.to('cuda')

# 将模型移动到 CPU
model = model.to('cpu')

# 保存模型权重为 CPU 类型
torch.save(model.state_dict(), "model_cpu.pth")
print("模型已保存为 CPU 类型权重文件:model_cpu.pth")

3.ImportError: DLL load failed while importing onnx_cpp2py_export

​ 通常是由于 ONNX 库的版本不兼容或其他依赖问题导致的,将 ONNX 从最新版本(如 1.17.0)降级到 1.16.1 可以解决问题:

>> pip uninstall onnx
>> pip install onnx==1.16.1

4.ERROR: pymilvus 2.5.4 has requirement setuptools<70.1; python_version <= "3.8", but you'll have setuptools 75.3.0 which is incompatible.

>> pip install --upgrade setuptools==69.5.1
>> pip install pymilvus==2.5.4

5.安装 pycryptodome

>> sudo apt install python3-pycryptodome
# 1.低版本的python,如3.8
>> pip install pycryptodome
# 2.高版本的python,如3.10
>>  pip install pycryptodomex

6.ValueError: Can't patch loop of type <class 'uvloop.Loop'>

​ SyncElasticsearchStore 类的初始化过程中调用了 nest_asyncio.apply(),这导致了对事件循环的打补丁操作。由于当前环境使用了 uvloopnest_asyncio 无法对其进行处理,从而抛出了异常。处理办法是卸载掉uvloop库:

>> pip uninstall uvloop

7.UserWarning: CUDA initialization: Unexpected error from cudaGetDeviceCount(). Did you run some cuda functions before calling NumCudaDevices() that might have already set an error? Error 500: named symbol not found (Triggered internally at ../c10/cuda/CUDAFunctions.cpp:108.) return torch._C._cuda_getDeviceCount() > 0 False

  • 将cuda添加到系统环境变量中去:
>> vim ~/.zshrc
export CUDA_PATH=/usr/local/cuda
export LD_LIBRARY_PATH=$CUDA_PATH/lib64:$LD_LIBRARY_PATH

8.A module that was compiled using NumPy 1.x cannot be run in NumPy 2.1.2 as it may crash. To support both 1.x and 2.x versions of NumPy, modules must be compiled with NumPy 2.0. Some module may need to rebuild instead e.g. with 'pybind11>=2.12'.

  • 降级numpy到1.x
>> pip install numpy==1.26.4

9.libonnxruntime_providers_cuda.so with error: libcudnn.so.8: cannot open shared object file: No such file or directory

  • (1) cudnn官网下载一个v8.x.x的包:https://developer.nvidia.com/rdp/cudnn-archive
  • (2) 解压安装:
>> tar -xvf cudnn-linux-x86_64-8.9.7.29_cuda12-archive.tar.xz
>> cd cudnn-linux-x86_64-8.9.7.29_cuda12-archive
>> cp ./include/cudnn*.h /usr/local/cuda/include
>> cp ./lib/libcudnn* /usr/local/cuda/lib64
>> chmod a+r /usr/local/cuda/include/cudnn*.h /usr/local/cuda/lib64/libcudnn*
  • (3) 添加环境变量:
>> vim ~/.zshrc
>> export LD_LIBRARY_PATH=/usr/local/cuda/lib64:$LD_LIBRARY_PATH

参考资料


Comment