4

I write and maintain a Python library for quantum chemistry calculations called PyQuante. I have a fairly standard Python distribution with a setup.py file in the main directory, a subdirectory called "PyQuante" that holds all of the Python modules, and one called "Src" that contains source code for C extension modules.

I've been lucky enough to have some users donate code that uses Cython, which I hadn't used before, since I started PyQuante before either it or Pyrex existed. On my suggestion, they put the code into the Src subdirectory, since that's where all the C code went.

However, looking at the code that generates the extensions, I wonder whether I should have simply put the code in subdirectories of the Python branch instead. And thus my question is:

what are the best practices for the directory structure of python distributions with both Python and Cython source files?

  • Do you put the .pyx files in the same directory as the .py files?
  • Do you put them in in a subdirectory of the one that holds the .py files?
  • Do you put them in a child of the .py directory's parent?

Does the fact that I'm even asking this question betray my ignorance at distributing .pyx files? I'm sure there are many ways to make this work, and am mostly concerned with what has worked best for people.

Thanks for any help you can offer.

Rick
  • 1,784
  • 3
  • 15
  • 25
  • [This post](http://stackoverflow.com/questions/4505747/how-should-i-structure-a-python-package-that-contains-cython-code?rq=1) (which I only found after submitting my own) provides one answer. Is this what people think is the "best practice"? – Rick Jun 02 '13 at 22:37
  • Also, check out this [section of the documentation](http://docs.cython.org/src/reference/compilation.html#distributing-cython-modules) describing how to distribute packages that use Cython. – Will Apr 08 '14 at 03:33

1 Answers1

2

Putting the .pyx files in the same directory as .py files makes the most sense to me. It's what the authors of scikit-learn have done and what I've done in my py-earth module. I guess I think of Cython modules as optimized replacements for Python modules. I will often begin by writing a package in pure Python, then replace some modules with Cython if I need better performance. Since I'm treating Cython modules as replacements for Python modules, it makes sense to me to keep them in the same place. It also works well for test builds using the --inplace argument.

jcrudy
  • 3,921
  • 1
  • 24
  • 31
  • Thanks. I'm worried about cluttering everything up if I put the pyx and py files in the same directory (but I assume the scikit-learn guys know what they're doing). However, putting things in a separate directory messes with the nosetests somewhat. – Rick Jun 04 '13 at 03:14