简体   繁体   中英

How can I access other files in the same Docker container?

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:

在此处输入图像描述

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"]

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM