如何从`src/`目录下的python包访问project root中的文本文件

2024-05-14 21:12:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我希望我的软件包的版本号可以放在一个地方,任何需要它的东西都可以引用它。在

我在这本Python指南中找到了一些关于Single Sourcing the Package Version的建议,并决定尝试使用#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

由于VERSIONsetup.py是同级的,所以很容易读取安装脚本中的版本文件并对其执行任何操作。在

但是VERSION和{}不是同级的,主模块不知道项目根的路径,所以我不能使用这种方法。在

导游有这样的提醒:

Warning: With this approach you must make sure that the VERSION file is included in all your source and binary distributions (e.g. add include VERSION to your MANIFEST.in).

这似乎是合理的-不是包模块需要项目根路径,而是可以在构建时将版本文件复制到包中以便于访问-但是我将这一行添加到清单中,版本文件似乎仍然没有出现在构建中的任何地方。在

为了构建,我从项目根目录运行pip install -U .,并在virtualenv中运行。以下是在<virtualenv>/lib/python3.6/site-packages中创建的文件夹:

^{pr2}$

我的更多配置文件:

清单.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',
)

我发现这个问题Any python function to get “data_files” root directory?让我认为pkg_resources库是我问题的答案,但我还没能想出如何在我的情况下使用它。在

我遇到了麻烦,因为我发现的大多数例子都是python包直接放在项目根目录中,而不是孤立在src/目录中。我使用src/目录,因为建议如下:

我发现并尝试稍微扭转一下的其他旋钮是package_datainclude_package_data,以及data_fileskwargs for setup()。不知道他们有多相关。似乎这些声明的东西和清单中声明的东西之间有一些相互作用,但我不确定细节。在


Tags: the项目inpy版本srcpackagedata
1条回答
网友
1楼 · 发布于 2024-05-14 21:12:34

在Freenode上的python IRC频道上与一些人讨论了这个问题。我学到了:

  • pkg_resources可能是如何我应该做我所要求的,但是它需要将版本文件放在包目录中,而不是项目根目录中。在
  • setup.py中,我可以在不导入包本身的情况下从包目录读取这样一个版本文件(出于一些原因,这是不可能的),但它需要硬编码从根目录到包的路径,这是我想要避免的。在

最后,我决定使用^{}包从git标记中获取版本信息,而不是从repo中的文件中获取(其他人正在使用他们的包来获取版本信息,而且他们的参数很有说服力)。在

因此,我很容易地在setup.py中获得了我的版本号:

设置.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='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'),
    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/\uu main\uy.py

^{pr2}$

相关问题 更多 >

    热门问题