Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

packaging common python namespaces

I'm looking to package and upload a library I have to PyPI in the next few days, but I'm a little unsure about my approach to the namespace.

I have a few otherwise unrelated projects with a similar approach and wanted to give them all the same namespace. For example:

  • Library 1 namespace: abc.seo
  • Library 2 namespace: abc.ajax
  • Library 3 namespace: abc.ecommerce
  • etc

The problem is that I'm not sure if it's possible for two separate packages (eg eggs) to co-exist with the same parent namespace. Is this approach problematic, or is there a way around it? What's the best approach?

The libraries should not be packaged together, they are too unrelated. I would like to get it right before uploading so as to avoid painful namespace changes after making an "official" release.

(NB abc is not the real name, I wanted my question to be free from advertising)

UPDATE

I went with the following, to be nice to the people without setuptools installed:

try:
    __import__('pkg_resources').declare_namespace(__name__)
except ImportError:
    __path__ = __import__('pkgutil').extend_path(__path__, __name__)

With the following in setup.py:

setup(
    ...
    namespace_packages = ['rollyourown'],
    ...
like image 611
Will Hardy Avatar asked Feb 21 '11 10:02

Will Hardy


People also ask

What are namespace packages in Python?

In Python, a namespace package allows you to spread Python code among several projects. This is useful when you want to release related libraries as separate downloads.

How do you do packaging in Python?

Follow the below steps to create a package in PythonCreate a directory and include a __init__.py file in it to tell Python that the current directory is a package. Include other sub-packages or files you want. Next, access them with the valid import statements.

What is __ all __ in Python?

In the __init__.py file of a package __all__ is a list of strings with the names of public modules or other objects. Those features are available to wildcard imports. As with modules, __all__ customizes the * when wildcard-importing from the package.


1 Answers

In each project base directory, create the following structure:

/setup.py
/abc/__init__.py
/abc/seo/

/abc/__init__.py contains :

__import__('pkg_resources').declare_namespace(__name__)

setup.py contains :

setup(...,
    packages: ['abc', 'abc.seo'],
    namespace_packages = ['abc']
    ...
)

Reference documentation: namespace packages.

like image 106
jd. Avatar answered Oct 09 '22 04:10

jd.