Python, namespace vs module with underscores

modulesnamespacepackagespython

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:

Foo
 |-Bar1
 |  |-Bar2
 |-Foo1
 |  |-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)

Best Answer

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.