创建Python包的新手

2024-09-27 23:18:51 发布

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

我试着做一个发布在我的GitHub上,帮助用户下载程序的依赖项,但最终,它只会生成重复的文件。我希望(最终)有一个软件包,用户可以输入:

>>> import my_package
>>> my_package.main

但那没用。我看过几个不同的网站和不同的模板,但似乎没有什么进展。在


目录结构

^{pr2}$

在设置.py在

发件人:https://github.com/kennethreitz/setup.py

#!/usr/bin/env python
# -*- coding: utf-8 -*-

# Note: To use the 'upload' functionality of this file, you must:
#   $ pip install twine

import io
import os
import sys
from shutil import rmtree

from setuptools import find_packages, setup, Command

# Package meta-data.
NAME = 'wav2bin'
DESCRIPTION = 'GUI graphing tool used concurrently with lab.'
URL = 'https://github.com/jvanderen1/Kodimer_Project'
EMAIL = 'jvanderen1@gmail.com'
AUTHOR = 'Joshua Van Deren'

# What packages are required for this module to be executed?
REQUIRED = [
    'matplotlib',
    'numpy',
    'scipy'
]

# The rest you shouldn't have to touch too much :)
# ------------------------------------------------
# Except, perhaps the License and Trove Classifiers!
# If you do change the License, remember to change the Trove Classifier for that!

here = os.path.abspath(os.path.dirname(__file__))

# Import the README and use it as the long-description.
# Note: this will only work if 'README.md' is present in your MANIFEST.in file!
with io.open(os.path.join(here, 'README.md'), encoding='utf-8') as f:
    LONG_DESCRIPTION = '\n' + f.read()

# Load the package's __version__.py module as a dictionary.
about = {}
with open(os.path.join(here, NAME, '__version__.py')) as f:
    exec(f.read(), about)


class UploadCommand(Command):
    """Support setup.py upload."""

    description = 'Build and publish the package.'
    user_options = []

    @staticmethod
    def status(s):
        """Prints things in bold."""
        print('\033[1m{0}\033[0m'.format(s))

    def initialize_options(self):
        pass

    def finalize_options(self):
        pass

    def run(self):
        try:
            self.status('Removing previous builds…')
            rmtree(os.path.join(here, 'dist'))
        except OSError:
            pass

        self.status('Building Source and Wheel (universal) distribution…')
        os.system('{0} setup.py sdist bdist_wheel --universal'.format(sys.executable))

        self.status('Uploading the package to PyPi via Twine…')
        os.system('twine upload dist/*')

        sys.exit()


# Where the magic happens:
setup(
    name=NAME,
    version=about['__version__'],
    description=DESCRIPTION,
    long_description=LONG_DESCRIPTION,
    author=AUTHOR,
    author_email=EMAIL,
    url=URL,
    package_dir={'': 'wav2bin'},
    packages=find_packages(exclude=('tests', 'docs')),
    # If your package is a single module, use this instead of 'packages':
    # py_modules=['mypackage'],

     entry_points={
         'gui_scripts': ['wav2bin = wav2bin.__main__:main'],
     },
    install_requires=REQUIRED,
    python_requires='>=3',
    include_package_data=True,
    license='MIT',
    classifiers=[
        # Trove classifiers
        # Full list: https://pypi.python.org/pypi?%3Aaction=list_classifiers
        'License :: OSI Approved :: MIT License',
        'Programming Language :: Python :: 3 :: Only',
        'Natural Language :: English',
        'Topic :: Scientific/Engineering :: Human Machine Interfaces',
        'Topic :: Software Development :: User Interfaces'
    ],
    # $ setup.py publish support.
    cmdclass={
        'upload': UploadCommand,
    },
)

wav2bin/src/\uuu main\uy.py

代码片段:

if __name__ == '__main__':
    main()

Tags: thetopathpyimportselfpackageos
1条回答
网友
1楼 · 发布于 2024-09-27 23:18:51

在你对各种包装模块有了一些经验之后,你通常会做的是决定你希望你的包装有多专业?你想在pypi上托管它吗?从github分发它?把它传给朋友?在

这就是您选择打包方法的方式,但首先您可能应该对现有的打包模块有一些经验,最流行的两个模块是:

  1. setuptools这是我通常使用的方法,我把它链接到了一个好的教程中
  2. distutils一个旧的api来分发包,但它仍然存在,而且很好地了解它

如果你认为这是一个过度的杀戮,你想要一个不太专业的方法,你可以随时手动。在

安装到python包文件夹,对于pip来说,这通常意味着类似于进入packages根文件夹并输入

pip install .

或者,如果你确定

pip install -e .

用于在编辑模式下安装,如果您仍希望保持软件包的可塑性

或者在导入之前以其他方式将包放在python路径中是强制的。在

相关问题 更多 >

    热门问题