[英]How do I use setuptools to create a python package with multiple modules?
I want to organize a distribution with several modules. 我想组织一个包含多个模块的发行版。 Eventually there will be a C extension module, and at the same 'level', several pure python modules.
最终将有一个C扩展模块,并在同一'级别',几个纯python模块。 I am not sure whether the top level should just be considered a namespace.
我不确定顶层是否应该被视为命名空间。
For starters, I would like to create a module monty.spam. 对于初学者,我想创建一个模块monty.spam。 It will be a C extension.
它将是C扩展。 I lifted this out of the Extending Python docs.
我将其从扩展Python文档中解脱出来。
monty/spam/spammodule.c 蒙蒂/垃圾邮件/ spammodule.c
#include <Python.h>
static PyObject * spam_system (PyObject * self, PyObject * args)
{
const char * command;
int sts;
if (!PyArg_ParseTuple (args, "s", &command))
return NULL;
sts = system (command);
return Py_BuildValue ("i", sts);
}
static
PyMethodDef SpamMethods [] = {
{ "system", spam_system, METH_VARARGS, "Execute a shell command" },
{ NULL, NULL, 0, NULL }
};
PyMODINIT_FUNC
initspam (void)
{
(void) Py_InitModule ("spam", SpamMethods);
}
I created a setup tools setup.py in the same directory, ran "python setup.py develop" and the module worked fine. 我在同一目录中创建了一个安装工具setup.py,运行“python setup.py develop”,模块工作正常。
monty/spam/setup.py 蒙蒂/垃圾邮件/ setup.py
from setuptools import setup, Extension
module = Extension ('spam', sources = [ 'spammodule.c' ])
setup (
name = 'MontyP',
version = '0.2',
description = 'pure spam',
ext_modules = [ module ]
)
In the monty directory: 在monty目录中:
python -c 'import monty.spam'
Traceback (most recent call last):
File "<string>", line 1, in <module>
ImportError: No module named monty.spam
So now I would like to create a distribution in which spam "lives in" monty. 所以现在我想创建一个垃圾邮件“生活在”monty的发行版。 I have tried a number of variations on the setup.py them in the monty directory.
我在monty目录中尝试了一些setup.py它们的变种。 (Both directories have an empty
__init__.py
) (两个目录都有一个空的
__init__.py
)
My latest try at monty/setup.py: (yes, the find_packages () has no utility in this version.) 我最近在monty / setup.py上尝试:(是的,find_packages()在这个版本中没有实用程序。)
from setuptools import setup, Extension, find_packages
module = Extension ('spam', sources = [ './spam/spammodule.c' ])
print find_packages ()
setup (
name = 'MontyP',
version = '0.2',
namespace_packages = [ 'monty' ],
packages = find_packages (),
description = 'pure spam',
ext_modules = [ module ]
)
No Joy! 没有喜悦!
python setup.py develop
['spam']
error in MontyP setup command: Distribution contains no modules or packages for namespace package 'monty'
You just need two or three changes to make it work. 您只需要进行两到三次更改即可使其正常工作。
First, instead of placing it at monty/spam
, setup.py
should be placed in the same level as monty/
: 首先,不应将其放在
monty/spam
,而应将setup.py
置于与monty/
相同的级别:
setup.py
always goes in the root of the project. setup.py
始终位于项目的根目录中。
Also, create an __init__.py
file in monty
but no __init__.py
inside spam
. 另外,在
monty
创建一个__init__.py
文件,但在spam
没有__init__.py
。
Now, in setup.py
, in the line " module = Extension ('spam', sources = [ 'spammodule.c' ])
", replace spam
by monty.spam
and spammodule.c
by monty/spam/spammodule.c
: 现在,在
setup.py
,在线“ module = Extension ('spam', sources = [ 'spammodule.c' ])
更换spam
通过monty.spam
和spammodule.c
由monty/spam/spammodule.c
:
module = Extension ('monty.spam', sources = [ 'monty/spam/spammodule.c' ])
This is necessary because external modules should be declared with their full names ( monty.spam
instead of spam
). 这是必要的,因为外部模块应使用其全名声明(
monty.spam
而不是spam
)。 Also, since now setup.py
is in the root of the project, it should receive a relative but complete path to the C source file. 此外,由于现在
setup.py
位于项目的根目录中,因此它应该接收到C源文件的相对但完整的路径。
That's it, your module should work now. 就是这样,你的模块现在应该工作了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.