【问题标题】:pytorch,opencv installation problem in python2 having cv library installed in python3python2中的pytorch,opencv安装问题,在python3中安装了cv库
【发布时间】:2020-11-24 05:25:04
【问题描述】:

haiiii 我在 ubuntu 18.04 中运行 python 代码

我需要用于代码的 opencv 库,所以我使用 python3 安装了 opencv

 pip3 install opencv-python

但代码需要并兼容python 2

所以我尝试只使用 pip 而不是 pip3 来安装库

当我尝试使用 pip 安装 opencv 时,它说

Defaulting to user installation because normal site-packages is not writeable
Requirement already satisfied: opencv-python in /home/sri/.local/lib/python3.6/site-packages (4.4.0.46)
Requirement already satisfied: numpy>=1.13.3 in /home/sri/.local/lib/python3.6/site-packages (from opencv-python) (1.19.4)

但运行我的文件 python train.py 显示没有名为 cv2 的模块 请有人帮我解决这个问题

我的 pytorch 库也一样

 Traceback (most recent call last):
 File "train.py", line 6, in <module>
   from model import CANNet
 File "/home/sri/Downloads/Context-Aware-Crowd-Counting-master/model.py", line 1, in <module>
import torch.nn as nn
ImportError: No module named torch.nn

所以我尝试安装 pytorch

pip install torch==1.7.0+cpu torchvision==0.8.1+cpu torchaudio==0.7.0 -f https://download.pytorch.org/whl/torch_stable.html


 Defaulting to user installation because normal site-packages is not writeable
 Looking in links: https://download.pytorch.org/whl/torch_stable.html
 Requirement already satisfied: torch==1.7.0+cpu in /home/sri/.local/lib/python3.6/site-packages (1.7.0+cpu)
 Requirement already satisfied: torchvision==0.8.1+cpu in /home/sri/.local/lib/python3.6/site-packages (0.8.1+cpu)
Requirement already satisfied: torchaudio==0.7.0 in /home/sri/.local/lib/python3.6/site-packages (0.7.0)
Requirement already satisfied: numpy in /home/sri/.local/lib/python3.6/site-packages (from torch==1.7.0+cpu) (1.19.4)
Requirement already satisfied: future in /home/sri/.local/lib/python3.6/site-packages (from torch==1.7.0+cpu) (0.18.2)
Requirement already satisfied: dataclasses in /home/sri/.local/lib/python3.6/site-packages (from torch==1.7.0+cpu) (0.8)
Requirement already satisfied: typing-extensions in /home/sri/.local/lib/python3.6/site-packages (from torch==1.7.0+cpu) (3.7.4.3)
Requirement already satisfied: pillow>=4.1.1 in /home/sri/.local/lib/python3.6/site-packages (from torchvision==0.8.1+cpu) (8.0.1)

【问题讨论】:

  • 尝试使用命令“python3 my_file.py”而不是“python my_file.py”来运行代码
  • xrange 在 python3 中被修改为范围,这就是为什么我试图将文件作为 python my_file.py 运行
  • 我认为您需要将 Python 2 代码转换为 Python 3,因为从长远来看这将是最佳选择。

标签: python python-3.x python-2.7 pytorch ubuntu-18.04


【解决方案1】:

您正在对您的版本进行故障排除。您说您的代码不适用于 python3,因此您尝试在 python2 上运行它。似乎您的 pip 链接的是您的 python 3.6 版本而不是 python 2.X。处理这些问题的最佳方法是虚拟环境:

在激活的环境中,命令pythonpip 与环境的版本相关联。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-14
    • 2021-12-23
    • 1970-01-01
    相关资源
    最近更新 更多