Martin Becuwe
Martin Becuwe

Reputation: 117

Use a Poetry built module inside of another Poetry built module

I have a custom general module module_a built with poetry and published to a Gitlab private package registry.
I wish to use this module in another more specific module module_b, but I cannot manage to add this dependency with the poetry add <package name> command.
For now I am only able to install module_a dependency inside the virtualenv created by Poetry on module_b using poetry run pip install ... --extra-index-url ...
What is the best practice in this use case?
Do you know how to add this dependency to the pyproject.toml file or should I always install both module module_a and module_b when I only want to use module_b?
Thanks!

Upvotes: 2

Views: 5447

Answers (2)

PyGuy
PyGuy

Reputation: 551

There are three ways to achieve this.

  1. The easiest one is to pass the GitHub path of the other library. This approach works both in development and production environments.

    poetry add git+https://github.com/your-github/your-module.git

    Poetry will automatically download, build, and install it in your current repo. You can also pinpoint your specific version by mentioning the tag or branch in the GitHub URL.

    poetry add "git+https://github.com/your-github/[email protected]"

  2. The second method is to directly pass the local path of the other library in your pyproject.toml file. This works when you are changing both repos at the same time while testing your project.

    [tool.poetry.dependencies]
    my-dependency = {path = "../path/to/your-module", develop = true}
    
  3. Another approach is to create a wheel file from your module, and use its path in your pyproject.toml file.

    [tool.poetry.dependencies]
    my-dependency = {path = "../path/to/built/your-module.whl"}
    

Upvotes: 2

2e0byo
2e0byo

Reputation: 5954

Poetry's dependencies are supposed to be public dependencies. If your module_b is going to be public, publish it to pypi and add it as you normally would. If it isn't, make module_a a git submodule of module_b in the right place, and access module_b that way.

Alternatively, set up a private repository, and add it to your pyproject.toml as such:

[[tool.poetry.source]]
name = "foo"
url = "https://foo.bar/simple/"

See the docs for more information.

Private Repositories

These are private python package repositories, not merely git repositories. You need a full server instance, with storage, to run a private repository, although there are services lke this one (never used; no affiliation) who will run one for you---for a fee, of course. See the python docs for more information on how package servers work.

If you go the submodule route, poetry doesn't handle the dependency at all---you just import directly from the code of module_a, so it needs to be cloned or symlinked into your source tree. This is a good deal easier than setting up a whole repository, but of course you have to handle the dependency yourself.

Upvotes: 0

Related Questions