0

I'm new to Docker. When I am running my docker container, the last line in the Dockerfile is the following:

CMD ["python3", "./poses/server/server_multithreaded.py"]

In the server_multithreaded.py file described above, I am importing another file, as seen below:

from poses.poseapp.poseapp_sockets import PoseAppWSockets

When I run container using the command docker run -p 8089:8089 zoheezus/capstone I get the following error:

Traceback (most recent call last):
   File "./poses/server/server_multithreaded.py", line 18, in <module>
      from poses.poseapp.poseapp_sockets import PoseAppWSockets
ImportError: No module named 'poses'

From what I understand, the 'poses' directory is not accessible or I am not accessing it the right way. What do I need to do for server_multithreaded.py to be able to access the other files when I run it?

The file structure of the project is the following:

enter image description here

zoheezus
  • 35
  • 1
  • 7
  • Does it work outside of docker? Do those directories contain `__init__.py` files? – jordanm Dec 03 '20 at 22:57
  • Assuming it works outside of docker, what is the full path to the script above in the container, and what is the value of WORKDIR in the Dockerfile – Moylin Dec 03 '20 at 23:13
  • @Moylin Here is a [link] (https://github.com/zoheezus/capstone-pose-estimation/blob/master/Dockerfile) to the Dockerfile and WORKDIR is specified in there. – zoheezus Dec 04 '20 at 00:50
  • Checkout out atline's answer below, I find option 3 is my personal preference. WORKDIR usually solves this for me because of the framework I use adds working directory to syspath. In your dockerfile try ENV PYTHONPATH /usr/pose_recognizer – Moylin Dec 04 '20 at 17:00

2 Answers2

0

Python won't add current working path into module search path, it will just add top level script's path into search path, of course PYTHONPATH, sys path etc. also be searched.

For your case, the current working path /usr/pose_recognizer won't be searched, only /usr/pose_recognizer/poses/server will be searched. So, definitely, it won't find module named 'poses'.

To make it work for you, give you some options:

Option 1: Execute the server_multithreaded.py as module:

python -m poses.server.server_multithreaded

Option 2: Change sys.path in server_multithreaded.py as next before from poses.poseapp.poseapp_sockets import PoseAppWSockets:

import sys
import os
file_path = os.path.abspath(os.path.dirname(__file__)).replace('\\', '/')
lib_path = os.path.abspath(os.path.join(file_path, '../..')).replace('\\', '/')
sys.path.append(lib_path)

Option 3: Change PYTHONPATH in dockerfile:

WORKDIR /usr/pose_recognizer
ENV PYTHONPATH=.
CMD ["python3", "./poses/server/server_multithreaded.py"]
atline
  • 28,355
  • 16
  • 77
  • 113
  • For the 3rd option, would you recommend something like: `COPY requirements.txt /usr/pose_recognizer/requirements.txt` `WORKDIR /usr/pose_recognizer` `RUN pip3 install -r requirements.txt` `COPY tf_pose_estimation ./tf_pose_estimation` `COPY . /usr/pose_recognizer` `RUN cd tf_pose_estimation && python3 setup.py install && cd ..` `ENV PYTHONPATH=.` `CMD ["python3", "./poses/server/server_multithreaded.py"]` Or do I have this formatted wrong? – zoheezus Dec 04 '20 at 23:26
  • Yes, any error for this? In fact you could also define `ENV PYTHONPATH=/usr/pose_recognizer`, same as `.` in your scenario. – atline Dec 07 '20 at 02:18
-1

this problem is most likely not related to docker but to your PYTHONPATH environment variable (inside the docker container). You must make sure that capstone-pose-estimation is in it. Furthermore, you should make poses and poseapp a package (containing __init___.py) in order to import from it

Raphael
  • 1,731
  • 2
  • 7
  • 23