我希望我的包的版本号位于一个地方,所有需要它的东西都可以引用它。
我在此 Python guide to Single Sourcing the Package Version中找到了一些建议,并决定尝试 #4,将其存储在我的项目根目录中名为VERSION
.
这是我项目目录树的缩短版本(您可以在 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)。
这似乎是合理的 - 而不是需要项目根路径的包模块,版本文件可以在构建时复制到包中以便于访问 - 但我将该行添加到清单中并且版本文件似乎仍然没有显示在任何地方构建。
要构建,我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
我的更多配置文件:
清单.in:
include README.md
include VERSION
graft src
prune tests
设置.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 question Any python function to get “data_files” root directory?这让我认为pkg_resources
图书馆是我问题的答案,但我无法弄清楚如何在我的情况下使用它。
我遇到了麻烦,因为我发现的大多数示例都直接在项目根目录中包含 python 包,而不是在src/
目录中隔离。src/
由于以下建议,我正在使用目录:
我发现并尝试稍微扭曲的其他旋钮是package_data
,include_package_data
和data_files
kwargs setup()
。不知道它们有多相关。似乎用这些声明的事物与清单中声明的事物之间存在一些相互作用,但我不确定细节。