python import from egg文件_从python bdist-egg或bdist-wheel中排除单个源文件

不幸的是,两者都没有

distutils

也不

setuptools

提供排除单个模块的可能性,因此您必须解决它。

更新:

我描述了一个更好的解决方案

here

模拟了包排除

设置工具

在中执行

find_packages()

. 您必须覆盖

build_py

安装脚本中可接受排除模式列表的命令,与

exclude

列出

find_packages

. 在您的情况下,它将是:

import fnmatch

from setuptools import find_packages, setup

from setuptools.command.build_py import build_py as build_py_orig

exclude = ['src.file_to_exclude']

class build_py(build_py_orig):

def find_package_modules(self, package, package_dir):

modules = super().find_package_modules(package, package_dir)

return [(pkg, mod, file, ) for (pkg, mod, file, ) in modules

if not any(fnmatch.fnmatchcase(pkg + '.' + mod, pat=pattern)

for pattern in exclude)]

setup(

...,

packages=find_packages(),

cmdclass={'build_py': build_py},

)

我觉得这个更强大

distutils

-比下面的溶液更符合要求。它还允许通过通配符匹配排除多个模块,例如

exclude = ['src.file*']

将排除从开始的所有模块

file

在里面

src

包装,或

exclude = ['*.file1']

将排除

file1.py

在所有包中。

原始答案

将要排除的模块放入单独的包中

你可以利用这个事实

设置工具

可以排除包(包含

__init__.py

文件),但它需要一些重构。创建

package_to_exclude

file_to_exclude.py

并修复所有最终导入错误:

project

âââ setup.py

âââ src

âââ __init__.py

âââ file1.py

âââ file2.py

âââ package_to_exclude

âââ __init__.py

âââ file_to_exclude.py

现在可以排除

包\到\排除

在安装脚本中:

from setuptools import find_packages, setup

setup(

...,

packages=find_packages(exclude=['src.package_to_exclude'])

)

排除包,添加要通过

py_modules

如果不能或不想在单独的包中移动模块,则可以排除

SRC

打包并将所有模块添加到

SRC

除了

file_to_exclude

在里面

PY_模块

. 例子:

import os

from setuptools import find_packages, setup

excluded_files = ['file_to_exclude.py']

included_modules = ['src.' + os.path.splitext(f)[0]

for f in os.listdir('src')

if f not in excluded_files]

setup(

...,

packages=find_packages(exclude=['src']),

py_modules=included_modules,

)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值