using setup.py for finding packages under directory with different name that of package

Viewed 80

I have the following directory structure:

foo/
└── setup.py
└── src/
    └── bar.py
    └── __init__.py

Here I am trying to install the package src under the name foo. My setup.py looks like this:

from setuptools import setup, find_packages

setup(
    name='foo',
    version='0.0.1',
    packages = find_packages(where='src'),
    package_dir = {'foo': 'src'},
    python_requires='>=3.8')

I installed the package using pip install -e . from the directory foo. When I try import foo or import foo.bar, I get a ModuleNotFoundError but import src works. How do I modify setup.py, so that import foo works?

Some additional information: here is my __init__.py:

from foo import bar

and bar.py

def bar():
  return "bar"
1 Answers

setup.py would need to be outside of the directory foo then modify setup.py to contain:

packages = ['foo'],
package_dir={'foo': 'foo/src'},
package_data={'foo': ['src/*']},

The import would then be from foo import barand the use bar.bar()

Related