简体   繁体   中英

Python module not found in container

I have a directory which has two files:

test
  -> __init__.py
  -> file1.py
  -> file2.py

file1 has contents:

CONFIG = {
"a": "b"
}

file2 is importing file1

import sys
import os
from file1 import config
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__))))

Now the files are put in docker container (volume mount)

I am getting error in container as no module named file1.

Though all the files exist in container.

When i run locally which python it gives me:

\usr\bin\python

when i exec into docker container, which python gives me:

\usr\local\bin\python

I do not understand why it says module not found, what possibly could be the reason.

For docker volumes, mount the share to the host system first, and then use docker volume to allow your docker container to use the volume.

In addition permissions and user can also be used from your host system, so permissions would not change when using a container and then using the files on the host system, you can allow this when using docker run

From the following article,

https://dille.name/blog/2018/07/16/handling-file-permissions-when-writing-to-volumes-from-docker-containers/

There is a parameter to set the user id and the group id for example, docker run -it --rm --volume $(pwd):/source --workdir /source --user $(id -u):$(id -g) ubuntu

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