Docker interpreter doesn't work

已回答

Hello, 

My goal is to use docker to make running my code on new machines easier. In particular, I'd like to use a combination of PyTorch and ROS. To this end, I've created a Dockerfile, built an image and configured my run and interpreter parameters to use it. I can successfully run my code without issue. However, the interpreter doesn't work at all. As you can see in the screenshot below, print() and exit() functions remain unresolved references. 

I assume that PyCharm isn't automatically able to find where python is situated in my docker image. As shown in the screenshot below, the “interpreter paths” panel is empty. To my understanding this shouldn't be the case. 



I have performed the usual debug techniques of turning on and off my PC, invalidating the caches and uninstalling/reinstalling PyCharm using Ubuntu Software.  At this point, I don't know what else to do. I could side step the issue by building my docker image with a requierments.txt file with all the packages I'd need and create a local virtual environment from the same requierments.txt. This doesn't feel satisfactory as a resolution. 

I'm hoping someone could impart some wisdom that will help me solve my issue.

System: 
Ubuntu 22.04.4 LTS  64-bit
PyCharm 2024.1.2 (Professional Edition)

Dockerfile to make docker image “basic-efficient-cv”:

FROM ubuntu:20.04
ENV DEBIAN_FRONTEND=noninteractive

# Set the working directory in the container
WORKDIR /Workspace

# Install system dependencies
RUN apt-get update && apt-get install -y \
    python3-pip \
    build-essential \
    cmake \
    libsm6 \
    libxext6 \
    libxrender-dev \
    libglib2.0-0 \
    libx11-dev \
    x11-apps \
    tzdata \
    git \
    && apt-get clean

RUN apt-get update && apt-get install ffmpeg libsm6 libxext6  qt5-default -y


# Clone your GitHub repository
#RUN git clone https://github.com/DRGladwell/basicEfficientCV.git /app

# Change to the repository directory
#WORKDIR /app/basicEfficientCV

# Install Python dependencies
RUN pip install --upgrade pip
RUN pip install opencv-python ultralytics torch


# Make port 8000 available to the world outside this container (if needed)
EXPOSE 8000

# Define environment variable
ENV DISPLAY=:0

# Run app.py when the container launches (if you have a main script to run)
#CMD ["python", "app.py"]


Code working from docker image “basic-efficient-cv”



Code running for IDE configuration:

Code running for IDE output:

Code:

import cv2

# Initialize video capture
video_path = 'VID_20240530_152607.mp4'  # Replace with your video path
cap = cv2.VideoCapture(video_path)

print("Press 'q' to quit.")

# Check if video opened successfully
if not cap.isOpened():
    print("Error: Could not open video.")
    exit()

# Create Background Subtractor object
fgbg = cv2.createBackgroundSubtractorMOG2()

while True:
    ret, frame = cap.read()
    frame = cv2.resize(frame, (600,600))

    if not ret:
        break  # Break the loop when video ends

    # Apply the GMM background subtractor to get the foreground mask
    fgmask = fgbg.apply(frame)

    # Display the original frame and the foreground mask
    cv2.imshow('Original Frame', frame)
    cv2.imshow('Foreground Mask', fgmask)

    # Break the loop on 'q' key press
    if cv2.waitKey(30) & 0xFF == ord('q'):
        break

# Release video capture object and close all OpenCV windows
cap.release()
cv2.destroyAllWindows()


 

0

I was able to successfully see the interpreter paths using the same Dockerfile provided on a brand new project. Make sure to update PyCharm to the latest 2024.1.3 and also create a brand new project with it

1

I haven't checked yet, but I think I found my issue: I installed docker using snap store rather than installing it through command line interface as described by docker docs. This fixed similar issues I had on Visual Studios Code, and I assume it will fix the issues on Pycharm. 

I appreciate your help, Miguel and confirming it's not an issue with my Dockerfile. 

0

请先登录再写评论。