Python namespace packages in Python3

The topic of namespace packages seems a little confusing to the uninitiated, and it doesn’t help that previous versions of Python implemented this in several ways or that a lot of Q&A on StackOverflow is dated. I am looking for a solution at or later . Python 3.5

Scenario:

I am reorganizing a bunch of Python code in modules and submodules and working to ensure that each of these projects works independently from each other, sitting in the same namespace.

Ultimately, we will use an internal PyPi server that serves these packages on our internal network and do not want to confuse them with external (public) PyPi packages.

Example: I have 2 modules, and I would like to be able to do the following:

from org.client.client1 import mod1
from org.common import config

The reflected modules will be divided as such:

Repository 1:

org_client_client1_mod1/
  setup.py
  mod1/
    __init__.py
    somefile.py

Repository 2:

org_common_config/
  setup.py
  config/
    __init__.py
    someotherfile.py

My Git repositories are already set up like org_client_client1_mod1and org_common_configso I just need to do the setup on the packaging and __init__.pyfiles, I suppose.

Questions:

# 1

C __init__.py, which one should I use (if any) ?:

from pkgutil import extend_path
__path__ = extend_path(__path__, __name__)

Or:

import pkg_resources
pkg_resources.declare_namespace(__name__)

# 2

setup.pyI still need to add a parameter with me namespace_modules, and if so, would I use namespace_modules=['org.common'], or namespace_modules=['org', 'common']?

# 3

Can I refuse all of the above, just somehow implement it? Perhaps something simpler or more "python"?

+26
source share
2 answers

, Python!

# 1:

init.py, ( )?:

, , :

  1. . PEP 420 Python 3.3 . , - Python 3 pip.

  2. pkgutil. , Python 2 3, pip python setup.py.

  3. pkg_resources. , , , zip-safe.

# 2 (pkgutil-style) # 3 (pkg_resources-style), __init__.py. , __init__.py.

# 2:

setup.py, namespace_modules, , namespace_modules = ['org.common'] namespace_modules = ['org', 'common']?

, , namespace_packages setup().

# 3:

, ? , - ""?

python, , , , , , Python - . .


, :

PEP420, Python, , , . , , , . Git, .

, . setuptools.setup(package=[]), . / . , , setuptools find_namespace_package(),

:

find_namespace_packages() ( setuptools , 40.1.0): https://setuptools.readthedocs.io/en/latest/setuptools.html#find-namespace-packages

(08/09/2019):

, .

Python 3. 3+,

Python 3.5 , .

:

/ Python: org

: org_client, org_common

Python: 3.3+

setuptools: 40.1.0

from org.client.client1 import mod1
from org.common import config

, . org_client_client1_mod1 org_common_config,

1:

org_client_client1_mod1/
  setup.py
  org/
    client/
      client1/
        __init__.py
        submod1/
          __init__.py
        mod1/
          __init__.py
          somefile.py
        file1.py

setup.py

from setuptools import find_namespace_packages, setup
setup(
    name="org_client",
    ...
    packages=find_namespace_packages(), # Follows similar lookup as find_packages()
    ...
)

2:

org_common_config/
  setup.py
  org/
    common/
      __init__.py
      config/
        __init__.py
        someotherfile.py

setup.py:

from setuptools import find_namespace_packages, setup
setup(
    name="org_common",
    ...
    packages=find_namespace_packages(), # Follows similar lookup as find_packages()
    ...
)

( pip):

(venv) $ pip3 install org_common_config/
(venv) $ pip3 install org_client_client1_mod1/

:

(venv) $ pip3 list
...
org_client
org_common
...

, , org.client org.common.

, ( , venv):

(venv) $ cd venv/lib/python3.5/site-packages/
(venv) $ ls -l | grep org

, org_client org_common, .

(venv) $ cd venv/lib/python3.5/site-packages/org/
(venv) $ ls -l
client/
common/
...
+5

. - -, _ __init__.py .

:

__init__.py, ( )?

  • __init__.py , . () , Python ( setup.py.). , .

setup.py, namespace_modules, , namespace_modules=['org.common'] namespace_modules=['org', 'common']?

  • ! name= packages=. packages= arg .
  • package= arg: setup.py for namespace package
  • : example_package / a / <subpackage>

, ? , - ""?

  • , , .

:

, , .

, , . 2 :

org_client_client1_mod1/
  setup.py
  mod1/
    __init__.py
    somefile.py

&

org_common_config/
  setup.py
  config/
    __init__.py
    someotherfile.py

!!!

, :

, , 3 , , , , , :

org-client/
  setup.py
  org/
    client/
      client1/
        __init__.py
        mod1/
          __init__.py
          somefile.py

&

org-common-but-also-note-this-name-doesnt-matter/
  setup.py
  org/
    common/
      __init__.py
      config/
        __init__.py
        someotherfile.py

, name= & packages= args stuptools.setup() setup.py.

:

name='org_client',
...
packages=['org.client']

&

name='org_common'
...
packages['org.common']

.

pip install. .

somefile.py, someotherfile.py. 2 org .

, : https://packaging.python.org/guides/packaging-namespace-packages/#packaging-namespace-packages

: https://github.com/pypa/sample-namespace-packages/tree/master/native

+2

Source: https://habr.com/ru/post/1666650/


All Articles