0

I am using Poetry to build some package. For simplicity sake the structure of the repository looks something like this:

my_package
    pyproject.toml
    service
        client
            client.py
            data.py
            __init__.py
        __init__.py

But I want the package is named my_package_client and includes just client directory. So that after installation I could import like this:

from my_package_client import Client

I tried to write this in pyproject.toml:

name = "my_project_client"
packages = [
    {include = "client", from = "service"}
]

But obviously after installation I only can import this way:

from service.client import Client
  • I added an answer below, but unfortunately I didn't have time to fully test it. Let me know if this works or helps. If not, I can either fix it or remove it. Let me know. – DV82XL Feb 19 '22 at 14:36

1 Answers1

0

As far as I know, there is no way to "alias" the package name in Poetry. I worked around this by adding an extra package between service and client, like this:

my_package
    pyproject.toml
    service
        my_project_client
            client
                client.py
                data.py
                __init__.py
            __init__.py

pyproject.toml (note dashes vs. underscores):

name = "my-project-client"
packages = [
    {include = "my_project_client", from = "service"}
]

Add this to the __init__.py of my_project_client to get the namespace you want:

from .client import Client

After publishing to PyPI, install the package in a different repo:

pip install my-project-client

Then you can import like this:

from my_project_client import Client
DV82XL
  • 5,350
  • 5
  • 30
  • 59