Ash_s94
Ash_s94

Reputation: 817

Adding local package to poetry with docker

I'm currently pushing a few scripts to a docker image and using poetry to install the dependencies. One of the dependencies I'd like to use involves building one of the directories into a module I can import. Here's an example:

poetry-project
 │   ├ poetry_project
 │   │      └── ex1.py
 │   └ dockerfile
 │   └ pyproject.toml
 │   └ poetry.lock
 │   
 tools
     ├ python_tools
            └── project_tools.py

I'd like to do the following in my ex1.py class:

#ex1.py class
from python_tools import project_tools

project_tools.do_stuff()

An example of my dockerfile:

FROM python:3.6
COPY pyproject.toml pyproject.toml
RUN poetry update
COPY poetry_project/ poetry_project/

Ultimately I'd like to run a command like docker run python3 poetry_project/ex1.py and be able to utilize project_tools import. I've read that I can add the package to pyproject.toml by doing as so:

my_package = { path = "/project_name/tools/python_tools/" }

However this doesn't seem to work for me as I get the following error:

Directory does not seem to be a Python package

Any help would be appreciated.

Upvotes: 1

Views: 3596

Answers (1)

Antonis Adamakos
Antonis Adamakos

Reputation: 64

a) It is a good practice to specify an absolute location where the code lives inside the container:

COPY . /usr/src/mypythonapp
WORKDIR /usr/src/mypythonapp

So, your code will live in /usr/src/mypythonapp. And this dir will be the base directory where all commands will run. No need for ugly chaining cd xxx && do xxx && bla bla.

b) In ex1.py you can specify the additional "include path":

sys.path.append(os.path.abspath("/usr/src/mypythonapp/tools/"))

c) You can automatically run the file with CMD in Dockerfile:

CMD ["python3", "/full/path/to/your/script.py"]

Upvotes: 0

Related Questions