Python Packaging: Difference between revisions

From Littledamien Wiki
Jump to navigation Jump to search
Tag: wikieditor
 
(5 intermediate revisions by the same user not shown)
Line 1: Line 1:
[[Category:Python]][[Category:Web Development]]
== Overview ==
== Overview ==


Line 4: Line 5:


== Configuration ==
== Configuration ==
=== Setup.py ===


A setup config file, `setup.py`, in the repo root directory should contain a single call to `setuptools.setup()`, like so: <ref>[https://python-packaging.readthedocs.io/en/latest/minimal.html How To Package Your Python Code] - Python Packaging Tutorial</ref>
A setup config file, `setup.py`, in the repo root directory should contain a single call to `setuptools.setup()`, like so: <ref>[https://python-packaging.readthedocs.io/en/latest/minimal.html How To Package Your Python Code] - Python Packaging Tutorial</ref>
Line 10: Line 13:
from setuptools import setup
from setuptools import setup


setup(name='my_package',
setup(name='namespace-my-package',
       version='0.1',
       version='0.1',
       description='My package description',
       description='My package description',
       url='http://github.com/dbarchowsky/my-package',
       url='http://github.com/dbarchowsky/namespace-my-package',
       author='Damien Barchowsky',
       author='Damien Barchowsky',
       author_email='dbarchowsky@gmail.com',
       author_email='dbarchowsky@gmail.com',
       license='MIT',
       license='MIT',
       packages=['my_package'],
       packages=['namespace.my_package'],
      install_requires=['list',
            'of',
            'dependencies',
            ],   
       zip_safe=False)
       zip_safe=False)
</syntaxhighlight>
</syntaxhighlight>
Line 30: Line 37:


<syntaxhighlight lang="python">
<syntaxhighlight lang="python">
>>> import my_package
>>> from namespace.my_package import my_module
>>> my_package.some_routine()
>>> my_module.some_routine()
</syntaxhighlight>
</syntaxhighlight>


== Installation from GitHub ==
=== Namespaces ===
 
It's recommended to use namespaces when creating packages. <ref>[https://packaging.python.org/guides/packaging-namespace-packages/ Packaging Namespace Packages] - PyPA Packaging Guide</ref> Even if the package is not being publicly distributed, the names of dependencies from other packages can clash.
 
The recommended file structure is this:
 
<pre>
mynamespace-subpackage-a/
    setup.py
    mynamespace/
        subpackage_a/
            __init__.py
            module_aa.py
</pre>
 
Note that there is no `__init__.py` in the `mynamespace` directory. Only packages should have `__init__.py` files.
 
=== Including sub-packages in distribution ===
 
Each module in the package must be explicitly added to the distribution under the `packages` setting in `setup.py`.
 
For a project with packages under the top-level package, e.g.:
 
<pre>
mynamespace-subpackage-a/
    setup.py
    mynamespace/
        my_package/
            __init__.py
            subpackage_a/
                __init__.py
            subpackage_b/
                __init__.py
            module_aa.py
</pre>
 
To include the non-top-level packages, add them to `packages` in `setup.py`:


=== Links ===
<syntaxhighlight lang="python">
from setuptools import setup


* [https://stackoverflow.com/questions/4830856/is-it-possible-to-use-pip-to-install-a-package-from-a-private-github-repository Is is possible to use pip to install a package from a private GitHub repository] - StackOverflow
setup(name='mynamespace-my-package',
    ''' [...] '''
    packages=['mynamespace.my_package',
              'mynamespace.my_package.subpackage_a',
              'mynamespace.my_package.subpackage_b',
    ],
    ''' [...] '''


</syntaxhighlight>
== Installation from GitHub ==
<p class="alert alert-warning">Legacy information below. See [[Installing Private Python Packages Hosted on GitHub]]</p>


=== Prerequisites ===
=== Prerequisites ===


Create and store a local SSH key. (One each for all computers where development is happening.) See [https://help.github.com/articles/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent/ Generating a new SSH key and adding it to the ssh-agent] - GitHub Help
Create and store a local SSH key. (One each for all computers where development is happening.) See [https://help.github.com/articles/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent/ Generating a new SSH key and adding it to the ssh-agent] (GitHub Help)


Register the key on the GitHub account. See [https://help.github.com/articles/adding-a-new-ssh-key-to-your-github-account/ Adding a new SSH key to your GitHub account] - GitHub Help
Register the key on the GitHub account. See [https://help.github.com/articles/adding-a-new-ssh-key-to-your-github-account/ Adding a new SSH key to your GitHub account] (GitHub Help)


One key pair will suffice for all ssh requests, i.e. for a computer with the key installed, the key can be used for all repositories in the GitHub account.  
One key pair will suffice for all ssh requests, i.e. for a computer with the key installed, the key can be used for all repositories in the GitHub account.  
Line 51: Line 106:
=== Installing a package ===
=== Installing a package ===


To install a package from a private GitHub repository:
To install a package from a private GitHub repository: <ref>[https://stackoverflow.com/questions/4830856/is-it-possible-to-use-pip-to-install-a-package-from-a-private-github-repository Is is possible to use pip to install a package from a private GitHub repository] - StackOverflow</ref>


<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
Line 57: Line 112:
</syntaxhighlight>
</syntaxhighlight>


The packages dependencies will be installed along with it as long as they are listed in the package's `requirements.txt`.
The packages dependencies are listed under `install_requires=[]` in the package's `setup.py`.


=== Updating a package ===
=== Updating a package ===
Line 65: Line 120:
<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
$ pip install git+ssh://git@github.com/username/my-repo.git --upgrade
$ pip install git+ssh://git@github.com/username/my-repo.git --upgrade
  (or)  
 
(or)  
 
$ pip install git+ssh://git@github.com/username/my-repo.git -U
$ pip install git+ssh://git@github.com/username/my-repo.git -U
</syntaxhighlight>
</syntaxhighlight>

Latest revision as of 17:05, 22 July 2023

Overview[edit]

Create python packages that can be distributed with pip from GitHub (and not PyPi).

Configuration[edit]

Setup.py[edit]

A setup config file, setup.py, in the repo root directory should contain a single call to setuptools.setup(), like so: [1]

from setuptools import setup

setup(name='namespace-my-package',
      version='0.1',
      description='My package description',
      url='http://github.com/dbarchowsky/namespace-my-package',
      author='Damien Barchowsky',
      author_email='dbarchowsky@gmail.com',
      license='MIT',
      packages=['namespace.my_package'],
      install_requires=['list', 
            'of', 
            'dependencies',
            ],    
      zip_safe=False)

After which the package can be installed locally with

$ pip install .

And once installed the package can be used like this:

>>> from namespace.my_package import my_module
>>> my_module.some_routine()

Namespaces[edit]

It's recommended to use namespaces when creating packages. [2] Even if the package is not being publicly distributed, the names of dependencies from other packages can clash.

The recommended file structure is this:

mynamespace-subpackage-a/
    setup.py
    mynamespace/
        subpackage_a/
            __init__.py
            module_aa.py

Note that there is no __init__.py in the mynamespace directory. Only packages should have __init__.py files.

Including sub-packages in distribution[edit]

Each module in the package must be explicitly added to the distribution under the packages setting in setup.py.

For a project with packages under the top-level package, e.g.:

mynamespace-subpackage-a/
    setup.py
    mynamespace/
        my_package/
            __init__.py
            subpackage_a/
                __init__.py
            subpackage_b/
                __init__.py
            module_aa.py

To include the non-top-level packages, add them to packages in setup.py:

from setuptools import setup

setup(name='mynamespace-my-package',
    ''' [...] '''
    packages=['mynamespace.my_package',
              'mynamespace.my_package.subpackage_a',
              'mynamespace.my_package.subpackage_b',
    ],
    ''' [...] '''

Installation from GitHub[edit]

Legacy information below. See Installing Private Python Packages Hosted on GitHub

Prerequisites[edit]

Create and store a local SSH key. (One each for all computers where development is happening.) See Generating a new SSH key and adding it to the ssh-agent (GitHub Help)

Register the key on the GitHub account. See Adding a new SSH key to your GitHub account (GitHub Help)

One key pair will suffice for all ssh requests, i.e. for a computer with the key installed, the key can be used for all repositories in the GitHub account.

Installing a package[edit]

To install a package from a private GitHub repository: [3]

$ pip install git+ssh://git@github.com/username/my-repo.git

The packages dependencies are listed under install_requires=[] in the package's setup.py.

Updating a package[edit]

After changes have been committed to a repository, they can be retrieved with:

$ pip install git+ssh://git@github.com/username/my-repo.git --upgrade

(or) 

$ pip install git+ssh://git@github.com/username/my-repo.git -U

See also[edit]