Search code examples
pythonpackagesetuptoolspackaging

Setuptools: distribute package composed of a single module


I'm learning how to distribute python packages using setuptools and I have a problem.

setuptools is setting the name of the folder containing a single python file as the name of my package. Below is the structure of my repository:

gerador_endereco/
-- setup.py
-- my_package/
   -- __init__.py
   -- gerador_endereco.py

My setup.py is:

setup(
    name='gerador_endereco',
    version='1.0.4',
    author='Michel Metran',
    description='API para criação ...',
    url='https://github.com/open-dsa/gerador_endereco',
    packages=find_packages(),
    install_requires=requirements,
)

I understand that setuptools is related to the distribution of packages, composed of several modules. But I know that it is possible to distribute a package composed of a single module, but how can I import the package correctly, without the folder name appearing?

# Install
!pip install gerador-endereco

# Import work using "my_package" directory: bad...
from my_package.gerador_endereco import *

# I'd like import like this!!!
from gerador_endereco import *

# Run
listas = get_list_ceps_bairros(estado='sp', municipio='piracicaba')

The PyPi Package is in https://pypi.org/project/gerador-endereco/


Solution

  • setuptools is related to the distribution of packages, period. To install a module restructure you project:

    gerador_endereco/
        -- setup.py
        -- gerador_endereco.py
    

    and change setup.py; remove

    packages=find_packages(),
    

    and add

    py_modules = ['gerador_endereco']
    

    instead. See the docs at https://docs.python.org/3/distutils/setupscript.html#listing-individual-modules and https://packaging.python.org/guides/distributing-packages-using-setuptools/?#py-modules