在Docker容器中导入python文件 [英] Importing python file in docker container
问题描述
我正在docker容器中执行一个python文件,并且需要将我放置的所有函数导入一个名为 base_functions
的单独的python文件中。但是,从base_functions import * 写入会引发错误,即
ModuleNotFoundError:没有名为 base_functions的模块
,即使 base_functions.py
与主python文件位于同一目录中。我怎样才能做到这一点?我是否需要在settings.ini或其他内容中预先指定要导入的python脚本?
这是Dockerfile的内容:
从amancevice /熊猫:0.24.1-alpine
RUN apk更新
RUN apk添加build-base
运行apk添加gcc musl-dev libc-dev util-linux-dev linux-headers python3-dev postgresql-libs postgresql-dev git libffi-dev libmemcached-dev zlib-dev \
ca证书zlib-dev jpeg-dev freetype-dev libpng
RUN pip3 install --upgrade pip
COPY requirements.txt。
RUN pip3 install -r requirements.txt
复制src / vdp
WORKDIR /
ENTRYPOINT [ python3,- m, vdp]
这些都是项目目录中的所有文件:
/ home / cr / docker / 71119 /。
├──docker-compose.yaml
├──Dockerfile
├──Makefile
├──README.md
├──要求.txt
├──settings.ini
└──src
├──base_functions.py
├──influx.py
├──__init__.py
└──__main__.py
您实际上是在重命名您的软件包(闻起来像一个软件包,具有 __ init __。py
和 __ main __。py
)
我建议:
- 将
src /...
重命名为src / vdp /...
(或vdp /...
)
–此时,您应该可以在python -m vdp
中运行主机上的src
目录,并且一切正常。 - 将Dockerfile节更改为
复制src / src
WORKDIR / src
ENTRYPOINT [ python3,-m, vdp]
–此等同于您在主机上运行它的方式。
另一种选择是删除 / src
并将其更改为 COPY vdp / src / vdp
。
当然,第三个选择是设置适当的Python封装,让您的 setup.py
构造一个适当的轮子,然后将其安装在Docker容器中。 / p>
I'm executing a python file inside a docker container, and need to import all the functions that I put into a separate python file called base_functions
. However, writing from base_functions import *
throws the error, that ModuleNotFoundError: No module named 'base_functions'
, even though base_functions.py
is in the same directory as the main python file. How can I do this? Do I need to specify which python scripts I want to import beforehand, in the settings.ini or something?
This is the content of the Dockerfile:
FROM amancevice/pandas:0.24.1-alpine
RUN apk update
RUN apk add build-base
RUN apk add gcc musl-dev libc-dev util-linux-dev linux-headers python3-dev postgresql-libs postgresql-dev git libffi-dev libmemcached-dev zlib-dev \
ca-certificates zlib-dev jpeg-dev freetype-dev libpng
RUN pip3 install --upgrade pip
COPY requirements.txt .
RUN pip3 install -r requirements.txt
COPY src /vdp
WORKDIR /
ENTRYPOINT ["python3", "-m", "vdp"]
These are all the files in the directory of the project:
/home/cr/docker/71119/.
├── docker-compose.yaml
├── Dockerfile
├── Makefile
├── README.md
├── requirements.txt
├── settings.ini
└── src
├── base_functions.py
├── influx.py
├── __init__.py
└── __main__.py
You're practically renaming your package (it smells like a package, having __init__.py
and __main__.py
) from "src" to "vdp" when you're creating the dockerfile.
I'd recommend:
- Rename
src/...
tosrc/vdp/...
(orvdp/...
) – at this point you should be able to runpython -m vdp
within thesrc
directory on your host machine and things should work. - Change the Dockerfile stanzas to
COPY src /src
WORKDIR /src
ENTRYPOINT ["python3", "-m", "vdp"]
– this'd be equivalent to how you're running it on the host machine.
Another option would be to drop the /src
in the repository and change things to COPY vdp /src/vdp
.
The third option, of course, would be to set up proper Python packaging, have your setup.py
build a proper wheel, then simply install that in the Docker container.
这篇关于在Docker容器中导入python文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!