【发布时间】: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