问题
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.
回答1:
I stumbled upon the same problem and did not find any proper solution (read "using predefined setup options").
I ended up making an ugly patch: I move everything into a new subdirectory named as the package, then move everything back afterwards.
import os, errno
# create directory
directory = 'xy/'
try:
os.makedirs(directory)
except OSError as e:
if e.errno != errno.EEXIST:
raise
# move important files
move = [ ... ]
for fname in move:
os.rename(fname, directory + fname)
setup(
...
package_dir = {'': '.'},
...
)
# move back
for fname in move:
os.rename(directory + fname, fname)
回答2:
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': '.',
},
)
来源:https://stackoverflow.com/questions/21197347/setup-py-inside-installed-module