简体   繁体   English

创建Python软件包的新手

[英]New to creating a Python Package

I tried making one to post on my GitHub and help users download dependencies for the program, but in the end, all it does is generate duplicate files. 我尝试将其发布在GitHub上,并帮助用户下载该程序的依赖项,但最后,它所做的只是生成重复文件。 I was hoping to have (in the end) a package where the user could enter: 我希望有一个(最终)用户可以输入的软件包:

>>> import my_package
>>> my_package.main

but that isn't working. 但这不起作用。 I've looked at several different websites and different templates, but seem to be getting nowhere with this. 我看过几个不同的网站和不同的模板,但是似乎毫无用处。


Directory structure 目录结构

Kodimer_Project
    ├── LICENSE
    ├── MANIFEST.in
    ├── Makefile
    ├── README.md
    ├── requirements.txt
    ├── setup.py
    └── wav2bin
        ├── __version__.py
        ├── imgs
        │   ├── App_Icon.gif
        │   ├── App_Icon.ico
        │   └── __init__.py
        └── src
            ├── DrawGraph.py
            ├── GraphicInterface.py
            ├── SplashScreen.py
            ├── __init__.py
            ├── __main__.py
            └── helper_functions.py

setup.py setup.py

From: https://github.com/kennethreitz/setup.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/__main__.py wav2bin / src / __ main__.py

Snippet of code: 代码段:

if __name__ == '__main__':
    main()

After you have some experience with various packaging modules what you would usually do is decide how professional you want your packaging to be? 在具有各种包装模块的经验之后,通常要做的是确定包装的专业程度? Do you want to host it on pypi? 您要在pypi上托管它吗? Distribute it from github? 从github分发吗? Pass it along to friends? 传递给朋友?

That's how you pick your packaging method, but first you should probably get some experience with the existing packaging modules, the two most popular ones are: 这就是选择包装方法的方式,但是首先您应该应该对现有的包装模块有所了解,其中最受欢迎的两个是:

  1. setuptools which is what I usually go with and to which I linked a good tutorial 我通常使用的setuptools以及与之链接的很好的教程
  2. distutils an older api to distribute packages but it's still around and is also very good to know distutils是一个较旧的api,用于分发软件包,但它仍然存在,并且非常了解

If then you decide this is an overkill and you want a less professional approach you can always do it manually. 如果这样,您认为这是一个过大的选择,并且想要一种不太专业的方法,则可以始终手动进行。

Either installation to the python package folder, which for pip usually means something like entering the packages root folder and entering either 要么安装到python package文件夹,对于pip来说通常意味着进入包根目录文件夹,然后输入

pip install . 点安装。

If you are certain of your package, or 如果您确定自己的包裹,或者

pip install -e . pip install -e。

For installing in edit mode if you still wish to keep the package malleable 如果您仍希望保持软件包的可扩展性,则以编辑模式安装

or having the package in your python path in some other manner before import is mandatory. 或在强制导入之前以其他方式将包放入python路径中。

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

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