EMC
EMC

Reputation: 1940

How can I develop on a Python library package while testing it a project using Pycharm + Docker?

This is a pretty specific ask, so I'm open to helpful suggestions that get part of the way there.

I have a python project that runs inside a docker container configured to work with the Pycharm debugger. I have a package, installed in a virtual env with pip, used in this project, that I'd like to develop on.

I haven't found a way to link the package into my project's docker container in such a way that I can change the package and have the code update in my project. Currently, the debugger works on codepaths that enter into the package, as long as I don't change any code in the package.

These two problems combined make it hard to test changes to the package without installing it over and over.

Is there a better way to accomplish this goal?

Upvotes: 3

Views: 540

Answers (4)

EMC
EMC

Reputation: 1940

I think I've covered all the bases I outlined above with this system:

My project structure is:

projects
   projectA (my docker project)
   projectB (the library used in my docker project that I want to develop on)
  1. In projectA: project settings -> project interpreter -> add path mappings -> map local library path to remote install path on container

    ex. local path: /user/{username}/projects/projectB remote path: /usr/local/lib/python3.6/site-packages/projectB

  2. In projectA: project settings -> project structure -> add content root (projectB) -> choose mark as sources

  3. In projectA: mark path on to projectB in container as a volume in dockerfile

    ex. VOLUME /usr/local/lib/python3.6/site-packages/projectB

  4. In projectA: load local library as volume to library installation on container in docker-compose.yml

    ex.

volumes:

- ../projectB:/usr/local/lib/python3.6/site-packages/projectB

Using python 3.6 and Pycharm 2018.2

Upvotes: 1

David Maze
David Maze

Reputation: 159382

Given the two source trees that should work together:

  1. Create a virtual environment for them, python -m venv vpy.
  2. Activate it, setting relevant shell environment variables, . vpy/bin/activate.
  3. Install the library, cd library && pip install -e . (-e causes pip to remember a pointer to the live source tree.)
  4. Install the application, cd app && pip install -e .. (Pip should know you already have the library installed.)
  5. Do whatever you need to do, $EDITOR file.py; pytest; the_app; $SCM commit.
  6. Once it all works correctly, docker build && docker run.

I'd leave any interaction with Docker until the very end, once you've convinced yourself you've fixed the library bug or built the feature. That avoids troubles with your editor and the container disagreeing on paths, and it means you don't need root privileges for any of your ordinary development work.

Upvotes: 1

Kamil Niski
Kamil Niski

Reputation: 4765

You can try using combination of what @pbskumar proposed and docker volumes.

First run your container with option --volume /path/to/your/package/on/host/:/path/in/your/container

And then you execute this inside the container: pip install -e /path/in/your/container

It should work.

Upvotes: 0

pbskumar
pbskumar

Reputation: 1157

Install the package in editable mode.

pip install -e .

This will allow you to make changes to the code and update the package simultaneously.

Upvotes: 0

Related Questions