简体   繁体   English

如何使用setuptools创建包含多个模块的python包?

[英]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.spamspammodule.cmonty/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.

相关问题 如何使用 python setuptools 将多个单个模块打包为子包 - How to package multiple single modules as sub-package with python setuptools 我如何使用setuptools或distutils来分发脚本而不是Python包? - How do I use setuptools or distutils to distribute a script as opposed to a Python package? 如何解决未完全安装的软件包(python3-setuptools)? - How do I resolve not fully installed package (python3-setuptools)? 如何配置 python setuptools 将模块作为单个包安装在根目录中? - How to configure python setuptools to install modules in the root directory as a single package? 使用setuptools / distribute创建python包时,是否需要* .egg-info目录? - Do I need *.egg-info directories when using setuptools/distribute to create a python package Setuptools如何仅打包一些模块和文件 - Setuptools how to package just some modules and files 如何在python中创建包和模块? - How to create a package and modules in python? 如何使用setuptools打包Python守护程序 - How to package a Python daemon with setuptools 如何为我用于多个不同项目的模块创建本地 Python 包 - How to create a local Python package for modules that I use for several different projects python setuptools:如何用cython子模块安装包? - python setuptools: how can I install package with cython submodules?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM