如何在python安装包中包含文本文件?

问题描述 投票:0回答:2

我创建了一个如下所示的python包:

/command
    /command
        module.py
        __main__.py
    README.md
    setup.py
    file.txt

要安装i run:

sudo python setup.py install

现在我打电话的时候

$ command

它显示了这个错误:

FileNotFoundError: [Errno 2] No such file or directory: '../file.txt'

大概有模块setup.py,__ main__.py和module.py的内容

setup.朋友

import setuptools

setuptools.setup(
    name='command',
    ...
    entry_points={
        'console_scripts': [
            'command = command.__main__:main'
        ]
    }
)

__卖弄__.朋友

from . import module

def main():
    module.run('arg')

module.朋友

import shutil

# copy file.txt from command project into the directory where it is running
def run(arg):
    shutil.copyfile('../file.txt', './file.txt')

通过以下方式安装此包后:

sudo python setup.py install

并在命令行调用该程序

$ command

我收到以下错误

FileNotFoundError: [Errno 2] No such file or directory: '../file.txt'

怎么能够看到和使用这样的文件,属于已安装的软件包,但我想在我运行该程序的环境中使用它?

编辑:

This a simplification of the problem you can download and test:

https://github.com/mctrjalloh/project_initializer

python include install text-files setup.py
2个回答
0
投票

经过大量研究后,我发现了解决方案,以及它是如何工作的。这有点令人困惑,其他堆栈溢出答案都没有解释。我想在这里试试:

我做了一个示例项目,目的只是为了演示和测试解决方案。我提出了两个解决方案:一个使用setup()函数的data_files参数,另一个使用我最喜欢的package_data参数。

Here is the link to the github repo you can download and test

在安装运行后使用它

proj init <some-name>

但简而言之,每种方法都有最重要的模块。

使用data_files = ARGUMENT方法:

项目结构:

project_initializer
    project_initializer
        __init__.py
        __main__.py
        init.py
    README.md
    setup.py

在setup.py中

import setuptools
import os
import sys


PROJECT_NAME = "project_initializer"
DATA_DIR = os.path.join(
    sys.prefix, "local/lib/python3.6/dist-packages", PROJECT_NAME)


setuptools.setup(
    name='project_initializer',
    version='0.1.0',
    packages=setuptools.find_packages(),
    install_requires=[
        'docopt'
    ],
    data_files=[         # is the important part
        (DATA_DIR, [
            "README.md",
            ".gitignore"
        ])               
    ],
    entry_points={
        'console_scripts': [
            'proj = project_initializer.__main__:main'
        ]
    }
)

在init.py中

import subprocess
import os
import shutil
import sys

"""Create a new project and initialize it with a .gitignore file
@params project: name of a project to be initialized
effects:
    /project
        README.md
    README.md in the created project directory must be the same as the README.md in THIS directory 
"""

PROJECT_NAME = "project_initializer"
DATA_DIR = os.path.join(
    sys.prefix, "local/lib/python3.6/dist-packages", PROJECT_NAME)


def run(project):
    os.mkdir(project)
    shutil.copyfile(os.path.join(DATA_DIR, "README.md"),
                    f"{project}/README.md")  # problem solved


if __name__ == '__main__':
    run("hello-world")

使用package_data = ARGUMENT METHOD(我更喜欢)

项目结构:

project_initializer
    project_initializer
        data/
            README.md  # the file we want to copy
        __init__.py
        __main__.py
        init.py
    README.md
    setup.py

在setup.py中

import setuptools


setuptools.setup(
    name='project_initializer',
    version='0.1.0',
    packages=setuptools.find_packages(),
    package_dir={'project_initializer': 'project_initializer'}, # are the ... 
    package_data={'project_initializer': [ # ... important parameters
        'data/README.md', 'data/.gitignore']},
    install_requires=[
        'docopt'
    ],
    entry_points={
        'console_scripts': [
            'proj = project_initializer.__main__:main'
        ]
    }
)

在init.py中

import subprocess
import os
import shutil
import sys

PROJECT_DIR = os.path.dirname(__file__)

"""Create a new project and initialize it with a .gitignore file
@params project: name of a project to be initialized
effects:
    /project
        .gitignore
    .gitignore in the created project directory must be the same as the gitignore in THIS directory 
"""


def run(project):
    os.mkdir(project)
    shutil.copyfile(os.path.join(PROJECT_DIR, 'data/README.md'),
                    f"{project}/README.md")  # problem solved


if __name__ == '__main__':
    run("hello-world")

我之所以喜欢这种最后一种方法,是因为你不必在setup.py模块中导入任何东西,这可能是一个不好的做法。我想在setup.py文件中不应该导入任何内容,因为它是主包的外部文件。

有关两个参数之间差异的更详细说明,请查看python文档

Using data_files= argument

Using package_data= argument


-1
投票

默认情况下,包中只包含python文件。

要包含更多内容,请添加MANIFEST.in并列出该文件。 For example

Here is a comprehensive tutorial

© www.soinside.com 2019 - 2024. All rights reserved.