Search code examples
pythonpython-3.xmodulepython-modulepypi

How to set up a Pypi package without the dot notation in absolute import - python3


Most "professional" python modules can be imported like so:

from pythonfile import class

And that is how I usually import my own classes that are in my local file system/current directory.

But after looking at several posts and python.org documents, I still can't get my python module (on Pypi.org) to import like that.

The only way I can get my module to import is like so:

from example.example import Example

(Example is a class within example.py)

How to I get my module to import like so:

from example import Example

My module on Pypi has a folder structure is like this and installs fine via pip:

example/
|
├── example/
│   ├── example.py
│   ├── __init__.py
│   └── __pycache__
│       └── __init__.cpython-36.pyc
|
|
├── __init__.py
├── LICENSE
|
├── README.md
└── setup.py
  • I've omitted the build/, dist/, and egg-info directories for clarity.

Should I add import statements to one of the __init__.py files?

I want developers to be able to install the package via pip, and then use a simple

from example import Example and not from example.example import Example

Thank you.


Solution

  • when you have a look at some other big packages they usually have in their top __init__.py something like

    from .modulex import *
    from .moduley import *
    

    the dot means, python is looking in current package before rest of the PYTHONPATH.