Setup.py inside installed module

穿精又带淫゛_ 提交于 2019-12-10 18:58:40

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!