I'm developing a project that uses several independent modules, but all related to the same project.

However, I face this choice: module names with underscore vs. namespaces.

For example there is this tree:

 |  |-Bar2
 |  |-Foo2
 |  |-Foo3

Let's say for example, that Foo3 depends on Bar2, but Foo2 and Bar2 are standalone.

What are the best practices regarding performance, and ease to package (to pypi) and code readability:

using _ : Foo_Bar_Bar1, Foo_Foo1_Foo2 and Foo_Foo1_Foo3

or using namespaces: Foo.Bar.Bar1, Foo.Foo1.Foo2 and Foo.Foo1.Foo3

It is more beautiful to use module and namespace, but on pip, independent packages are saved in different location. I've also read there is a bug when combining package installed from pypi and editable package that uses the same namespace

see also : https://github.com/pypa/pip/issues/3

Also, another problem about namespace is that Foo can't have any content and can't be a package (a namespace module must contain only a namespace declaration) like read here : https://pythonhosted.org/setuptools/setuptools.html#namespace-packages (Or I didn't understand)

  • Not sure what you mean by but on pypi, independent packages are saved in different location.. Can you please elaborate on that? – Martijn Pieters Mar 20 '15 at 15:21
  • sorry, I meant pip : when you install two packages (that use namespace modules, they are installed in a different location rather than in subdirectories like when you developp a single big package – hl037_ Mar 20 '15 at 15:23
  • So? That doesn't make a difference to how you import them. – Martijn Pieters Mar 20 '15 at 15:25
  • So you identified several very specific issues that limit how you can use namespaces. I never ran into the pip issue (I never install packages as editable with pip as I use buildout for that usecase). Don't use a shared namespace in that case, you are not forced to. That has little to do with performance however. – Martijn Pieters Mar 20 '15 at 15:40

I'd not be concerned about performance here, but about usability.

Modules are loaded just once in a Python program's lifetime. Referencing elements within modules and packages, including nested modules, is virtually free. Performance doesn't matter here.

Use modules to group your project into logical units; it is more readable and maintainable if things that are closely related conceptually are clustered within a namespace.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.