簡體   English   中英

Python 模塊在容器中未找到

[英]Python module not found in container

我有一個包含兩個文件的目錄:

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

file1 有內容:

CONFIG = {
"a": "b"
}

file2 正在導入 file1

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

現在文件放在 docker 容器中(卷掛載)

我在容器中收到錯誤,因為沒有名為 file1 的模塊。

盡管所有文件都存在於容器中。

當我在本地運行 python 它給了我:

\usr\bin\python

當我執行到 docker 容器中時,python 給了我:

\usr\local\bin\python

我不明白為什么它說找不到模塊,可能是什么原因。

對於 docker 卷,首先將共享掛載到主機系統,然后使用 docker 卷以允許您的 docker 容器使用該卷。

此外權限和用戶也可以從您的主機系統中使用,因此在使用容器然后使用主機系統上的文件時權限不會更改,您可以在使用docker run時允許這樣做

從下面的文章中,

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

有一個參數可以設置用戶id和組id 例如docker run -it --rm --volume $(pwd):/source --workdir /source --user $(id -u):$( id -g) ubuntu

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM