Zarządzanie pakietami w Pythonie

Artur Smęt
kontakt@artursmet.com

Agenda

  1. Od distutils to setuptools
  2. Rodzaje pakietów (egg vs wheel)
  3. Publikacja pakietu na PyPI

Instalacja pakietów

  • distutils
  • setuptools
    • easy_install
    • pip
  • egg
  • wheel

distutils

  • Tworzenie pakietu
                    $ python setup.py sdist
                
  • Instalacja pakietu
                    $ python setup.py install
                
    • Zalety
    • Instaluje pakiety
    • Pozwala zdefiniować listę zależności

      Wady
    • Nie instaluje zależności
    • Nie można usunąć pakietu
    • Nie można wyświetlić listy zainstalowanych pakietów

    setuptools (easy_install)

    Instalacja pakietu

                
                    $ easy_install foo
                
            

                
                    $ easy_install requests
    Searching for requests
    Reading https://pypi.python.org/simple/requests/
    Best match: requests 2.6.0
    Downloading https://pypi.python.org/packages/source/r/requests/requests-2.6.0.tar.gz#md5=25287278fa3ea106207461112bb37050
    Processing requests-2.6.0.tar.gz
    Writing /tmp/easy_install-ukAPnp/requests-2.6.0/setup.cfg
    Running requests-2.6.0/setup.py -q bdist_egg --dist-dir /tmp/easy_install-ukAPnp/requests-2.6.0/egg-dist-tmp-WEqEXR
    Adding requests 2.6.0 to easy-install.pth file
    
    Installed /tmp/wrocpy/lib/python2.7/site-packages/requests-2.6.0-py2.7.egg
    Processing dependencies for requests
    Finished processing dependencies for requests
    
                
            

    • Używa repozytorium pakietów PyPI
    • Obsługuje pakiety egg

    • Nie można usunać pakietu

    setuptools (pip)

    Instalacja pakietu

        
            $ pip install foo
        
        

                
                    $ pip install requests
    Downloading/unpacking requests
      Downloading requests-2.6.0-py2.py3-none-any.whl (469kB): 469kB downloaded
    Installing collected packages: requests
    Successfully installed requests
    Cleaning up...
    
                
            

    • Używa repozytorium pakietów PyPI
    • Obsługuje pakiety wheel
    • Jest dużo bardziej rozbudowany od easy_install

    • Nie obsługuje pakietów egg

    Rodzaje pakietów

    egg
    • Spakowany pakiet gotowy do użycia
    • Binarny (zawiera również pliki .pyc)
    • Obsługiwany tylko przez easy_install
    wheel
    • Wprowadzony by zastąpić pakiety egg
    • Binarny (bez plików .pyc)
    • Zdefiniowany przez PEP 427
    • {distribution}-{version}-{python}-{abi}-{platform}.whl
    • requests-2.6.0-py2.py3-none-any.whl

    Jak opublikować własny pakiet?

    
                #! /usr/bin/env python
    from setuptools import setup, find_packages
    
    setup(
        name='helloworldtest',
        author='John Doe',
        author_email='john.doe@somecompany.com',
        description="My beautiful library",
        license='MIT',
        version='0.1.0',
        url='http://somecompany.com/',
        packages=find_packages(),
        include_package_data=True,
        install_requires=[
            'requests==2.6.0'
        ],
        tests_require=[
            'mock'
        ],
        test_suite='helloworldtest.tests.suite',
        classifiers=[
            'Development Status :: 5 - Production/Stable',
            'Intended Audience :: Developers',
            'Natural Language :: English',
            'License :: OSI Approved :: MIT License',
            'Operating System :: OS Independent',
            'Programming Language :: Python',
            'Programming Language :: Python :: 2.7',
            'Topic :: Software Development :: Libraries :: Python Modules',
        ]
    )
    
            

    setup.cfg

                
                    [wheel]
    universal = 1
                
            

    Budowa pakietu

    Początkowa struktura katalogów
                
                ├── helloworldtest
    │   └── __init__.py
    ├── __init__.py
    ├── LICENSE.txt
    ├── README.txt
    ├── setup.cfg
    └── setup.py 
                
            

    MANIFEST.in

  • Pozwala zdefniować które pliki będą zawarte w pakiecie
  •             
                    prune static
    graft templates
    
                
            

    Budowa pakietu

                
                    $ python setup.py sdist
                
            
                
                    $ python setup.py bdist_wheel
                
            

    Końcowa struktura katalogów

                
                    ├── build
    │   ├── bdist.linux-x86_64
    │   └── lib.linux-x86_64-2.7
    │       └── helloworldtest
    │           └── __init__.py
    ├── dist
    │   ├── helloworldtest-0.1.0-py2.py3-none-any.whl
    │   └── helloworldtest-0.1.0.tar.gz
    ├── helloworldtest
    │   └── __init__.py
    ├── helloworldtest.egg-info
    │   ├── dependency_links.txt
    │   ├── PKG-INFO
    │   ├── requires.txt
    │   ├── SOURCES.txt
    │   └── top_level.txt
    ├── __init__.py
    ├── LICENSE.txt
    ├── README.txt
    ├── setup.cfg
    └── setup.py
                
            

    Rejestracja na PyPI

  • Użyjemy testowego serwera https://testpypi.python.org/
  • Plik ~/.pypirc

                
                    [distutils]
    index-servers=
        test
    
    [test]
    repository = https://testpypi.python.org/pypi
    username = johndoe
    password = somefancypassword
                
            

    Rejestracja i publikacja pakietu

                
                    $ python setup.py register -r test
                
            
                
                    $ python setup.py sdist upload -r test
                
            
                
                    $ python setup.py bdist_wheel upload -r test
                
            

    https://mirumee.com
    https://github.com/mirumee
    @mirumeelabs