【问题标题】:New to creating a Python Package创建 Python 包的新手
【发布时间】:2018-02-11 08:48:23
【问题描述】:

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

>>> import my_package
>>> my_package.main

但这不起作用。我查看了几个不同的网站和不同的模板,但似乎无济于事。


目录结构

Kodimer_Project ├── 执照 ├── MANIFEST.in ├── 生成文件 ├── README.md ├── requirements.txt ├── setup.py └── wav2bin ├── __version__.py ├── imgs │   ├── App_Icon.gif │   ├── App_Icon.ico │   └── __init__.py └── 源 ├── DrawGraph.py ├── 图形接口.py ├── SplashScreen.py ├── __init__.py ├── __main__.py └── helper_functions.py

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

​​>

代码片段:

if __name__ == '__main__':
    main()

【问题讨论】:

  • 显示您的setup.py 和其他相关代码(如果适用)。
  • 太宽泛了。通过the tutorial 并在此过程中返回更具体的问题。
  • 不应该是my_package.main()吗?
  • 试着把你的 my_package 文件放在最后: if name == 'main': #[your main function here] 然后运行它使用 python -m my_package
  • 我只是做了一些编辑来展示适用的代码。

标签: python setup.py


【解决方案1】:

在您对各种打包模块有一定的经验后,您通常会决定您希望自己的打包有多专业?你想把它托管在 pypi 上吗?从 github 分发它?转发给朋友?

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

  1. setuptools 这是我经常使用的,我链接了一个很好的教程
  2. distutils 一个较旧的 api 分发包,但它仍然存在,也很高兴知道

如果您认为这是一种矫枉过正的做法,并且您想要一种不太专业的方法,您可以随时手动完成。

要么安装到 python 包文件夹,对于 pip 通常意味着像进入包根文件夹并进入其中一个

点安装。

如果您确定自己的包裹,或

pip install -e.

如果您仍然希望保持包的可塑性,可以在编辑模式下安装

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

【讨论】:

  • 我想从我的 GIthub 分发这个,向潜在公司展示我从事的一些项目。它应该是 1) 易于安装 2) 易于运行
  • 除非你有充分的理由,如果我是你,我会尝试 setuptools
  • 是的,这就是我正在尝试的。但是,它对我来说不太有效。当我输入“python3 -m wav2bin”时,它似乎无法识别该命令。
  • 安装 pip 并将其用作您的安装程序(并了解 virutalenv 问题,它非常成熟)您可能正在查看一些假设安装了 anaconda3 的教程
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-22
  • 2014-07-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-28
相关资源
最近更新 更多