简体   繁体   English

如何为单个Python文件创建包?

[英]How can I create a Package for a single Python file?

I'm currently experimenting with Python packages. 我目前正在尝试使用Python软件包。 I have a tiny project which I would like to share with some people. 我有一个小项目,想与一些人分享。 This project consists of exactly one Python file, so I thought it should not be too difficult to create a Python package for it. 该项目仅包含一个Python文件,因此我认为为其创建一个Python包应该不太困难。

I've managed to register the project with the following setup.py at PyPI: 我已经在PyPI上通过以下setup.py注册了项目:

from setuptools import setup

setup(
    name='LumixMaptool',
    version='1.0.4',
    author='Martin Thoma',
    author_email='info@martin-thoma.de',
    packages=['lumix-maptool'],
    scripts=['lumix-maptool/lumix-maptool.py'],
    url='http://pypi.python.org/pypi/LumixMaptool/',
    license='LICENSE',
    description='Manage GPS information for Panasonic Lumix cameras.',
    long_description="""Panasonic offers GPS metadata to add to a SD card. This metadata can contain
tourist information that might be useful for sightseeing. This maptool helps
to copy the data from Lumix DVD to the SD card that is inserted into your 
computer (the camera has not to be connected).""",
    install_requires=[
        "argparse >= 1.2.1",
        "pyparsing >= 2.0.1",
        "pyparsing >= 2.0.1",
    ],
    entry_points={
        'console_scripts':
            ['lumixmaptool = lumixmaptool:main']
    }
)

with the command 用命令

python setup.py register

and later updated with 然后更新为

python setup.py sdist upload

Now it's here: https://pypi.python.org/pypi/LumixMaptool 现在在这里: https : //pypi.python.org/pypi/LumixMaptool

But I currently have problems with the following entries: 但是我目前对以下条目有疑问:

  • packages
  • scripts
  • entry_points

What do I have to fill in there? 我必须在那里填写什么? Do I have to have a certain project structure / some files? 我是否必须具有特定的项目结构/一些文件?

I currently have: 我目前有:

  • Readme.txt readme.txt文件
  • LICENSE.txt LICENSE.TXT
  • setup.py setup.py
  • lumix-maptool.py lumix-maptool.py

The projects GitHub site is here: https://github.com/MartinThoma/lumix_map_tool 项目GitHub站点在这里: https : //github.com/MartinThoma/lumix_map_tool

Every package on PyPI needs to have a file called setup.py at the root of the directory. PyPI上的每个软件包都需要在目录的根目录下有一个名为setup.py的文件。 If your'e using a markdown-formatted read me file you'll also need a setup.cfg file. 如果您使用的是markdown格式的自述文件,则还需要setup.cfg文件。 Also, you'll want a LICENSE.txt file describing what can be done with your code. 另外,您将需要一个LICENSE.txt文件来描述可以用您的代码执行的操作。 So if I've been working on a library called mypackage , my directory structure would look like this: 因此,如果我一直在使用名为mypackage的库,那么我的目录结构将如下所示:

root-dir/   # arbitrary working directory name
        setup.py
        setup.cfg
        LICENSE.txt
        README.md
        mypackage/
            __init__.py
            foo.py
            bar.py
            baz.py

Refer this link to know more about packaging. 请参考此链接以了解有关包装的更多信息。

  • Entry Point 入口点

EntryPoints provide a persistent, filesystem-based object name registration and name-based direct object import mechanism (implemented by the setuptools package). EntryPoints提供持久的,基于文件系统的对象名称注册和基于名称的直接对象导入机制(由setuptools包实现)。

They associate names of Python objects with free-form identifiers. 它们将Python对象的名称与自由格式的标识符相关联。 So any other code using the same Python installation and knowing the identifier can access an object with the associated name, no matter where the object is defined. 因此,使用相同的Python安装并知道标识符的任何其他代码都可以访问具有关联名称的对象,无论该对象在何处定义。 The associated names can be any names existing in a Python module; 关联的名称可以是Python模块中存在的任何名称。 for example name of a class, function or variable. 例如类,函数或变量的名称。 The entry point mechanism does not care what the name refers to, as long as it is importable. 入口点机制并不关心名称所指的是什么,只要它是可导入的即可。

An "entry point" is typically a function (or other callable function-like object) that a developer or user of your Python package might want to use, though a non-callable object can be supplied as an entry point as well (as correctly pointed out in the comments!). “入口点”通常是Python包的开发人员或用户可能希望使用的函数(或其他可调用的类似函数的对象),尽管也可以提供非调用对象作为入口点(正确地在评论中指出!)。

The most popular kind of entry point is the "console_script" entry point, which points to a function that you want made available as a command-line tool to whoever installs your package. 最受欢迎的一种入口点是“ console_script”入口点,它指向您想要作为命令行工具提供给安装软件包的任何人的功能。

  • Packages

Packages are used to include all the python packages present in your root-dir. 软件包用于包含根目录中存在的所有python软件包。 you can use find_packages() . 您可以使用find_packages()

For simple projects, it's usually easy enough to manually add packages to the packages argument of setup(). 对于简单的项目,通常很容易将包手动添加到setup()的packages参数中。 However, for very large projects (Twisted, PEAK, Zope, Chandler, etc.), it can be a big burden to keep the package list updated. 但是,对于非常大的项目(Twisted,PEAK,Zope,Chandler等),要使软件包列表保持更新可能是一个很大的负担。 That's what setuptools.find_packages() is for. 这就是setuptools.find_packages()目的。 Refer docs . 参考文档

  • Scripts 脚本

Refer this docs for scripts. 有关脚本,请参阅此文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM