3

For a directory structure like the following, I haven't been able to make xy an importable package.

xy
├── __init__.py
├── z
│   ├── __init__.py
│   └── stuff.py
└── setup.py

If the setup.py were a directory up, I could use

from setuptools import setup
setup(name='xy',
      packages=['xy'])

but short of that, no combination of package_dir and packages has let me import xy, only import z. Unfortunately, moving the setup.py a directory up isn't really an option due to an excessive number of hard-coded paths.

Community
  • 1
  • 1
Alex Riina
  • 801
  • 7
  • 11
  • I'm not certain I understand your question, but do you want to import xy so you have access to xy.setup? – Reti43 Jan 17 '14 at 23:42
  • I'm aiming to get access to `xy.z`, not necessarily `xy.setup`, but I would presume `xy.setup` to be accessible unless somewhere explicitly removed. – Alex Riina Jan 17 '14 at 23:47
  • If you write `import z` in the xy/__init__.py, by importing xy, you should have access to xy.z with no further fuss. – Reti43 Jan 17 '14 at 23:49
  • Unfortunately I'm trying to add a setup.py to a much larger codebase than listed here and importing the entire codebase into the `__init__` file isn't going to be pleasant even with some `__import__ find_packages()` loop. – Alex Riina Jan 17 '14 at 23:52
  • This is beyond my knowledge, but I'm under the impression that you can't escape the fact that eventually you have declare the desired imports explicitly. Would [`__all__`](http://docs.python.org/2/tutorial/modules.html#importing-from-a-package) make it any pleasant at all? – Reti43 Jan 18 '14 at 00:20

1 Answers1

1

See the following answer for ideas how to use package_dir and packages to help with such projects: https://stackoverflow.com/a/58429242/11138259

In short for this case here:

#!/usr/bin/env python3
import setuptools
setuptools.setup(
    # ...
    packages=['xy', 'xy.z'],
    #packages=setuptools.find_packages('..')   # only if parent directory is otherwise empty
    package_dir={
        'xy': '.',
    },
)
sinoroc
  • 18,409
  • 2
  • 39
  • 70
  • You'd think this would work, but it doesn't work for `-e` installs. The 'xy' package is not accessible as `import xy` and `import xy.z`. However, the files within xy are importable and the xy.z package is importable as `import z`. – M.Kerr Apr 14 '22 at 21:46
  • 1
    @m.kerr Yes, it's true, `package_dir` modifications and _editable_ installations do not mix well. But there is a complete rewrite of editable installations happening right now, so it might work in the future. See here if you are interested: https://discuss.python.org/t/pep-660-and-setuptools/14855/ – sinoroc Apr 14 '22 at 22:02