[Python] Pytorch, opencv installation problem in python2 having cv library installed in python3

Discussão em 'Python' iniciado por Stack, Julho 2, 2025.

  1. Stack

    Stack Membro Participativo

    I am running Python code on Ubuntu 18.04.

    I needed opencv library for the code so I installed opencv using python3:

    pip3 install opencv-python


    But the code requires and compatible with Python 2.

    I tried to install the library using just pip instead of pip3.

    When I tried to install opencv using pip, I get these messages:

    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)


    but running my file python train.py shows no module named cv2 some body please help me to solve this problem

    Same going with my pytorch library also

    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


    So I tried installing 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)

    Continue reading...

Compartilhe esta Página