LoginSignup
2
2

More than 3 years have passed since last update.

Python の Pull Request を生まれて初めて書いて学んだこと

Posted at

私は普段は、まったくPythonに触れたことはないのだが、偶然 Azure Functions の Kafka extensionsのバージョンアップと、ブラッシュアップを実施することになったので、その一環として、Python のライブラリをアップデートする必要が生じた。

あまりに言語仕様やツールを知らないので苦労したので、学んだことを記録しておきたい。

Virtual Environment

Python 3.4 以降は、Virtual Environment という仕組みが標準搭載されています。これは、ライブラリをインストールするときに、環境を分けてくれるものです。Ruby でいうbundle とか、node でいう npm のライブライの管理の仕組みに近しいものがあります。

具体的には次のコマンドを実行します。

$ python -m venv .venv

すると、.venvというディレクリが作成されます。venvを有効化するためには下記のコマンドを実行します。

$ source .venv/bin/activate

これによって、今後ライブラリをインストールすると、.venv以下に保存されるようになり、.venvがアクティベートされている間は、python のプログラムがこの下にインストールされているライブラリを参照できるようになります。

setup.py

もし、ライブラリを追加したくなったら、setup.pyにエントリを追加します。わたしがコントリビュートしたリポジトリ

$ python -m pip install -U -e ./[dev\]

こちらのコマンドで、setup.pyextras_requiredevの箇所もインストールされるようになります。尚、-UUpgrade の意味で、パッケージをより新しいバージョンに更新します。-eEditableの意味で、インストールされたライブラリが編集可能になります。通常は編集可能な形式になっていません。ライブラリのデバッグをするときは、実験したりソースを見たりしたいはずなので、-eをつけるのがお勧めです。

setup.py

from setuptools import setup
from azure.functions import __version__


setup(
    name='azure-functions',
    version=__version__,
    description='Azure Functions for Python',
    long_description='Python support for Azure Functions is based on '
                     'Python3.[6|7|8], serverless hosting on Linux and the '
                     'Functions 2.0 and 3.0 runtime. This module provides the '
                     'rich binding definitions for Azure Functions for Python '
                     'apps.',
    author='Microsoft Corporation',
    author_email='azpysdkhelp@microsoft.com',
    classifiers=[
        'License :: OSI Approved :: MIT License',
        'Intended Audience :: Developers',
        'Programming Language :: Python :: 3',
        'Operating System :: Microsoft :: Windows',
        'Operating System :: POSIX',
        'Operating System :: MacOS :: MacOS X',
        'Environment :: Web Environment',
        'Development Status :: 5 - Production/Stable',
    ],
    license='MIT',
    packages=['azure.functions'],
    package_data={
        'azure.functions': ['py.typed']
    },
    extras_require={
        'dev': [
            'flake8~=3.7.9',
            'mypy',
            'pytest',
            'requests==2.*',
            'coverage'
        ]
    },
    include_package_data=True,
    test_suite='tests'
)

Lint

まだ試してないけど、PythonのLintは結構癖があるので、Linter を入れておくのが良さげ

PR

生まれて初めてPythonのPR

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2