Search code examples
pythonpython-3.xpython-modulepypipython-packaging

Python3 preparing packages for PyPi not including submodules


Notes:

  1. I am using Python v3.6
  2. I have read the documentation regarding Modules and Packages
  3. I have read and gone through the Packaging project tutorial
  4. I have looked at the Sample Project (different from tutorial project)

I have a simple package I want to make

myPackage/                      The project directory
    myPackage/                  Top-level package
      __init__.py               Initialize my package
      myClass.py                a file with a single class in it

    setup.py
    README.md
    ...
    dist/

where myClass.py is just

class myClass:
    __init__(self):
        print('here')

and my myPackage/__init__.py file is just like the demo

name = "myPackage"

I have successful got it on pypi and pip installed it.

I can import myPackage but unlike the tutorial where

example_pkg.name

works,

myPackage.name

does not.

The above linked Sample project doesn't exactly elaborate any more on the init.py file.

So my question is as follows:

suppose one had a package like that under the documentation for Modules and Packages:

sound/                          Top-level package
      __init__.py               Initialize the sound package
      formats/                  Subpackage for file format conversions
              __init__.py
              wavread.py
              wavwrite.py
              aiffread.py
              aiffwrite.py
              auread.py
              auwrite.py
              ...
      effects/                  Subpackage for sound effects
              __init__.py
              echo.py
              surround.py
              reverse.py
              ...
      filters/                  Subpackage for filters
              __init__.py
              equalizer.py
              vocoder.py
              karaoke.py
              ...

where the actual file structure is (following the packaging project tutorial)

sound/
    sound/ (Top-level package)
        __init__.py
        ...
    dist/
    build/
    sound.egg/
    README.md
    ...

What goes in sound/__init__.py (or myPackage/__init__.py) so that I can import it and access the functions, submodules, and classes therein?

Sorry for being an noob at python packaging.

# contents of setup.py
import setuptools

with open("README.md", "r") as fh:
    long_description = fh.read()

setuptools.setup(
    name="progil",
    version="0.0.4",
    author="name",
    author_email="name@web",
    description="Progress In Line",
    long_description=long_description,
    long_description_content_type="text/markdown",
    url="https://pypi.org/project/progil/",
    packages=setuptools.find_packages(),
    classifiers=[
        "Programming Language :: Python :: 3",
        "License :: OSI Approved :: MIT License",
        "Operating System :: OS Independent",
    ],
)

Solution

  • Sanity checking the distribution's top-level import names by using my project johnnydep:

    # pip install johnnydep
    $ johnnydep progil --fields name summary versions_available import_names
    name    summary           versions_available    import_names
    ------  ----------------  --------------------  --------------
    progil  Progress In Line  0.0.2, 0.0.3, 0.0.4   progril
    

    You are going to kick yourself. It looks like you named the distribution:

    progil
    

    But you named the package in source code

    progril
    

    pip installing progil and importing progril works. You didn't notice at setup time because you've used find_packages() and, actually, there is no reason the distribution name needs to match the package name(s).