【发布时间】:2018-04-03 11:35:10
【问题描述】:
我希望我的包的版本号位于一个地方,所有需要它的东西都可以引用它。
我在Single Sourcing the Package Version 的 Python 指南中找到了一些建议,并决定尝试 #4,将其存储在我的项目根目录中名为 VERSION 的简单文本文件中。
这是我的项目目录树的缩短版本(你可以看到the full project on GitHub):
.
├── 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
由于VERSION 和setup.py 是兄弟姐妹,因此很容易读取安装脚本中的版本文件并用它做任何我想做的事情。
但是VERSION 和src/fluidspaces/__main__.py 不是兄弟,主模块不知道项目根目录的路径,所以我不能使用这种方法。
指南有这样的提醒:
警告:使用这种方法,您必须确保 VERSION 文件包含在您的所有源代码和二进制发行版中(例如,将 include VERSION 添加到您的 MANIFEST.in)。
这似乎是合理的 - 不需要项目根路径的包模块,版本文件可以在构建时复制到包中以便于访问 - 但我将该行添加到清单中,版本文件似乎仍然没有出现在任何地方的构建中。
为了构建,我从项目根目录和 virtualenv 中运行 pip install -U .。以下是在 <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='me@peterhenry.net',
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',
)
我发现这个 SO 问题 Any python function to get “data_files” root directory? 让我认为 pkg_resources 库是我问题的答案,但我无法弄清楚如何在我的情况下使用它。
我遇到了麻烦,因为我发现的大多数示例都直接在项目根目录中包含 python 包,而不是隔离在 src/ 目录中。我使用src/ 目录是因为以下建议:
- PyTest: Good Practices: Tests Outside Application Code
- Ionel Cristian Mărieș - Packaging a Python Library
- Hynek Schlawack - Testing and Packaging
我发现并尝试稍微扭转的其他旋钮是package_data、include_package_data 和data_files 用于setup() 的kwargs。不知道它们有多相关。似乎用这些声明的事物与清单中声明的事物之间存在一些相互作用,但我不确定细节。
【问题讨论】:
标签: python versioning setuptools setup.py pkg-resources