Terza
Terza

Reputation: 41

Docker can't find my files given in the Python path, although I am using os.path.dirname(__file__)

I have a weird problem, which I was able to solve in another project in the past, but my solution doesn't work on this one.

So, a bit about the project. I have a Django application that is used to scan check boxes. The application takes a couple of templates from a blank form, which is divided into .png images. The .png images are inside my Pycharm project and they have been uploaded to Docker, because my Docker setting is:

COPY . .

The Django application works locally just fine, but when I start it from the Docker image link, I get an error telling me Docker can't find the templates. My templates are being read in the following way (not the actual names of paths or images):

ROOT_FOLDER = os.path.dirname(__file__)
TEMPLATE_1 = cv2.imread(os.path.join(ROOT_FOLDER, 'path', 'path', 'path', 'image'))
TEMPLATE_2 = cv2.imread(os.path.join(ROOT_FOLDER, 'path', 'path', 'path', 'image'))
TEMPLATE_3 = cv2.imread(os.path.join(ROOT_FOLDER, 'path', 'path', 'path', 'image'))
TEMPLATE_4 = cv2.imread(os.path.join(ROOT_FOLDER, 'path', 'path', 'path', 'image'))

My attempts at a solution were:

  1. I first tried to remove the ROOT_FOLDER from the os.path.join, but that didn't work.
  2. I tried the following:
    ROOT_FOLDER = os.path.dirname(__file__)
    TEMPLATE_1 = cv2.imread(os.path.join(os.path.sep, ROOT_FOLDER, 'path', 'path', 'path', 'image'))
    TEMPLATE_2 = cv2.imread(os.path.join(os.path.sep, ROOT_FOLDER, 'path', 'path', 'path', 'image'))
    TEMPLATE_3 = cv2.imread(os.path.join(os.path.sep, ROOT_FOLDER, 'path', 'path', 'path', 'image'))
    TEMPLATE_4 = cv2.imread(os.path.join(os.path.sep, ROOT_FOLDER, 'path', 'path', 'path', 'image'))

But that didn't help either. Does anybody have an idea what could be wrong ?

Upvotes: 0

Views: 1136

Answers (1)

Terza
Terza

Reputation: 41

I solved the problem by simply adding the Linux path and deleting the WIndows one. Somehow, this solution works on Windows as well:

TEMPLATE_1 = cv2.imread('./data/templates/images/"name of files")

I am not entirely sure why this works, but it does.

Upvotes: 1

Related Questions