记录本人Windows下python代码在Linux中出现的错误
Windows中代码无问题,但是Linux中出现的问题:
1./root/ENV/lib/python3.7/site-packages/sklearn/ensemble/base.py:158: DeprecationWarning: np.int
is a deprecated alias for the builtin int
. To silence this warning, use int
by itself. Doing this will not modify any behavior and is safe. When replacing np.int
, you may wish to use e.g. np.int64
or np.int32
to specify the precision. If you wish to review your current use, check the release note link for additional information.
Deprecated in NumPy 1.20; for more details and guidance: https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations
dtype=np.int)
该警告由于numpy版本过高引起,使用 pip install -U numpy==1.16.5(与windows中一致),将Linux中numpy版本1.21.6改为1.16.5后解决。
2.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 conflicts.
matplotlib 3.5.3 requires numpy>=1.17, but you have numpy 1.16.5 which is incompatible.
由于matplotlib 3.5.3需要numpy>=1.17,故pip install -U matplotlib == 3.2.1(与windows中一致),解决。
3./root/ENV/lib/python3.7/site-packages/sklearn/utils/fixes.py:230: DeprecationWarning: distutils Version classes are deprecated. Use packaging.version instead.
if _joblib.version >= LooseVersion(‘0.12’):
(前半部分单独输入ChatGPT后,给出的信息)这是一个警告,它告诉你正在使用的 distutils 版本已经过时了。你需要使用新的 packaging.version 模块来代替。你可以在代码中找到 fixes.py 文件,然后将 distutils.version 改为 packaging.version。例如,将代码行from distutils.version import LooseVersion改为from packaging.version import LooseVersion。这样就可以避免这个警告了。
将 distutils.version 改为 packaging.version后,出现错误:ImportError: cannot import name ‘LooseVersion’ from ‘packaging.version’ (/root/ENV/lib/python3.7/site-packages/packaging/version.py),ChatGPT提示这个错误通常是由于 packaging 库版本过低引起的。你可以使用以下命令升级 packaging 库,但我的packaging 库为最新版本,故重新改回distutils.version;
(全部输入ChatGPT,给出的信息)这是一个 DeprecationWarning 警告信息,告诉我们 distutils Version classes 已经被弃用了,并建议我们使用 packaging.version 来代替。这个警告信息不会影响到程序的正常运行,但是我们需要尽快修改代码以避免可能出现的问题。可以尝试升级 joblib 库来解决这个问题,或者忽略这个警告。如果你使用的是 Python 3.7 及以上版本,可以使用 warnings 模块来禁止特定的警告信息。
升级 joblib 库后,警告依然存在。
最后在fixes.py中from distutils.version import LooseVersion前加入
import warnings
warnings.filterwarnings(“ignore”, category=DeprecationWarning)
忽略该警告,运行代码后,警告被忽视。
python3.7.16下安装torch cpu版本
使用指令:pip install torch1.10.1+cpu torchvision0.11.2+cpu torchaudio==0.10.1 -f https://download.pytorch.org/whl/cpu/torch_stable.html
而后进入python,import torch 成功,torch.version 显示 ‘1.10.1+cpu’
import torchvision失败,显示错误
Traceback (most recent call last):
File “”, line 1, in
File “/root/ENV/lib/python3.7/site-packages/torchvision/init.py”, line 7, in
from torchvision import datasets
File “/root/ENV/lib/python3.7/site-packages/torchvision/datasets/init.py”, line 1, in
from .lsun import LSUN, LSUNClass
File “/root/ENV/lib/python3.7/site-packages/torchvision/datasets/lsun.py”, line 10, in
from .utils import verify_str_arg, iterable_to_str
File “/root/ENV/lib/python3.7/site-packages/torchvision/datasets/utils.py”, line 11, in
import lzma
File “/root/training/Python-3.7.16/lib/python3.7/lzma.py”, line 27, in
from _lzma import *
ModuleNotFoundError: No module named ‘_lzma’
解决:
sudo yum install xz-devel
pip install backports.lzma
在/root/training/Python-3.7.16/lib/python3.7/lzma.py路径的lzma.py文件中,
from _lzma import *
from _lzma import _encode_filter_properties, _decode_filter_properties
改为
try:
from _lzma import *
from _lzma import _encode_filter_properties, _decode_filter_properties
except:
from backports.lzma import *
from backports.lzma import _encode_filter_properties, _decode_filter_properties