2017-10-23 7 views
0

私のパッケージのバージョン番号は、必要なものすべてが参照可能な単一の場所に存在します。`src /`ディレクトリのpythonパッケージからプロジェクトルートのテキストファイルにアクセスするには

このPythonガイドには、Single Sourcing the Package Versionといういくつかの提案があり、#4を試して、それをプロジェクトルートのVERSIONという単純なテキストファイルに保存することに決めました。

がここに私のプロジェクトのディレクトリツリーの短縮バージョン(あなたがthe full project on GitHubを見ることができます)です:VERSIONsetup.pyが兄弟であるので

. 
├── MANIFEST.in 
├── README.md 
├── setup.py 
├── VERSION 
├── src/ 
│   └── fluidspaces/ 
│    ├── __init__.py 
│    ├── __main__.py 
│    ├── i3_commands.py 
│    ├── rofi_commands.py 
│    ├── workspace.py 
│    └── workspaces.py 
└── tests/ 
   ├── test_workspace.py 
   └── test_workspaces.py 

、それがセットアップスクリプト内のバージョンのファイルを読み、何でもするのは非常に簡単です私はそれで欲しい。

VERSIONsrc/fluidspaces/__main__.pyは兄弟ではなく、メインモジュールはプロジェクトルートのパスを知らないため、このアプローチは使用できません。

警告:このアプローチを使用すると、VERSIONファイルは、すべてのソースとバイナリディストリビューション(例えば追加MANIFEST.inにVERSIONを含める)に含まれていることを確認する必要があります

ガイドでは、この通知を持っていました。代わりに、プロジェクトのルートパスを必要とするパッケージモジュールのバージョンのファイルを簡単にアクセスするためのビルド時にパッケージにコピーすることができ - - しかし、私はマニフェストと、バージョンファイルに行がまだdoesnのことを加えた合理的なように見えた

ビルドのどこにでも現れているようだ。

ビルドするには、pip install -U .をプロジェクトルートとvirtualenv内で実行しています。ここでは、結果として<virtualenv>/lib/python3.6/site-packagesに作成されますフォルダは、次のとおりです。私のコンフィギュレーションファイルの

fluidspaces/ 
├── i3_commands.py 
├── __init__.py 
├── __main__.py 
├── __pycache__/ # contents snipped 
├── rofi_commands.py 
├── workspace.py 
└── workspaces.py 
fluidspaces-0.1.0-py3.6.egg-info/ 
├── dependency_links.txt 
├── entry_points.txt 
├── installed-files.txt 
├── PKG-INFO 
├── SOURCES.txt 
└── top_level.txt 

より:

MANIFEST.in

include README.md 
include VERSION 
graft src 
prune tests 

setup.py

#!/usr/bin/env python3 

from setuptools import setup, find_packages 


def readme(): 
    '''Get long description from readme file''' 
    with open('README.md') as f: 
     return f.read() 


def version(): 
    '''Get version from version file''' 
    with open('VERSION') as f: 
     return f.read().strip() 


setup(
    name='fluidspaces', 
    version=version(), 
    description='Navigate i3wm named containers', 
    long_description=readme(), 
    author='Peter Henry', 
    author_email='[email protected]', 
    url='https://github.com/mosbasik/fluidspaces', 
    license='MIT', 
    classifiers=[ 
     'Development Status :: 3 - Alpha', 
     'Programming Language :: Python :: 3.6', 
    ], 
    packages=find_packages('src'), 
    include_package_data=True, 
    package_dir={'': 'src'}, 
    package_data={'': ['VERSION']}, 
    setup_requires=[ 
     'pytest-runner', 
    ], 
    tests_require=[ 
     'pytest', 
    ], 
    entry_points={ 
     'console_scripts': [ 
      'fluidspaces = fluidspaces.__main__:main', 
     ], 
    }, 
    python_requires='~=3.6', 
) 

私はこのような質問Any python function to get “data_files” root directory?私は、pkg_resourcesライブラリは私の問題の答えですが、私の状況でそれを使用する方法を理解することができていないと思う見つける。

私が見つけたほとんどの例では、src/ディレクトリではなく、プロジェクトルートに直接pythonパッケージがあるため、問題が発生しました。私は、これらのような勧告のsrc/ディレクトリを使用しています:

私が発見し、少しねじれ試した他のノブはpackage_datainclude_package_dataあります、setup()の場合はdata_files kwargsです。彼らがどれほど関連性があるのか​​分かりません。これらと宣言されたものとマニフェストで宣言されたものとの間には相互作用があるようですが、詳細についてはわかりません。

答えて

0

この問題についてFreenodeの#python IRCチャンネルに参加している人もいます。私が学んだ:

  • pkg_resourcesはおそらくどのよう私は私が求めていた何をすべきかすべきであるが、それは、パッケージディレクトリ内のバージョンファイルの代わりに、プロジェクトのルートを置くことが必要になりました。
  • setup.pyパッケージディレクトリからこのようなバージョンのファイルを読み込むことはできませんでしたが、パッケージ自体をインポートする必要はありませんでしたが、ルートからパッケージへのパスをハードコーディングする必要があります。避けたい

結局私はgitのタグからではなく、私のレポでファイル(他の誰かがそのパッケージとその引数と説得力のあったことをやっていた)からバージョン情報を取得するためにsetuptools_scmパッケージを使用することにしました。

その結果、私は非常に簡単にsetup.pyに私のバージョン番号を得た:

setup.py

from setuptools import setup, find_packages 

def readme(): 
    '''Get long description from readme file''' 
    with open('README.md') as f: 
     return f.read() 

setup(
    name='fluidspaces', 
    use_scm_version=True, # use this instead of version 
    description='Navigate i3wm named containers', 
    long_description=readme(), 
    author='Peter Henry', 
    author_email='[email protected]', 
    url='https://github.com/mosbasik/fluidspaces', 
    license='MIT', 
    classifiers=[ 
     'Development Status :: 3 - Alpha', 
     'Programming Language :: Python :: 3.6', 
    ], 
    packages=find_packages('src'), 
    package_dir={'': 'src'}, 
    setup_requires=[ 
     'pytest-runner', 
     'setuptools_scm', # require package for setup 
    ], 
    tests_require=[ 
     'pytest', 
    ], 
    entry_points={ 
     'console_scripts': [ 
      'fluidspaces = fluidspaces.__main__:main', 
     ], 
    }, 
    python_requires='~=3.6', 
) 

を私は何を示すハードコードされたパスを持つようになってしまいましたプロジェクトのルートは、私が以前に避けていたもののようなパッケージコードに関してでなければなりません。私はthis issue on the setuptools_scm GitHub repoがこれが必要な理由かもしれないと思います。

のsrc/fluidspaces/__ main__.py

import argparse 
from setuptools_scm import get_version # import this function 

def main(args=None): 
    # set up command line argument parsing 
    parser = argparse.ArgumentParser() 
    parser.add_argument('-V', '--version', 
         action='version', 
         version=get_version(root='../..', relative_to=__file__)) # and call it here 
関連する問題