zoukankan      html  css  js  c++  java
  • xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

    How to using PyPI publish a Python package

    PyPI & Python package

    https://pypi.org/static/images/logo-large.72ad8bf1.svg

    https://pypi.org/

    main

    make a file that can be both imported as a module and run as a script.

    To do this, place script code inside if name == "main".

    This ensures that it won't be run if the file is imported.

    # module.py
    def function():
       print("This is a module function")
    
    if __name__=="__main__":
       print("This is a script")
    
    
    # app.py
    from module import function
    
    function();
    
    
    $ python3 module.py
    # "This is a script"
    
    $ python3 app.py
    # This is a module function
    
    

    When the Python interpreter reads a source file, it executes all of the code it finds in the file.
    Before executing the code, it defines a few special variables.

    For example, if the Python interpreter is running that module (the source file) as the main program, it sets the special name variable to have a value "main". If this file is being imported from another module, name will be set to the module's name.

    directory structure

    project/
       LICENSE.txt
       README.txt
       setup.py
       emoji/
          __init__.py
          module.py
          module2.py
          ...
    
    

    In Python, the term packaging refers to putting modules you have written in a standard format, so that other programmers can install and use them with ease.
    This involves use of the modules setuptools and distutils.

    The first step in packaging is to organize existing files correctly.
    Place all of the files you want to put in a library in the same parent directory.

    This directory should also contain a file called __init__.py, which can be blank but must be present in the directory.

    This directory goes into another directory containing the readme and license, as well as an important file called setup.py.

    init.py

    setup.py

    This contains the information necessary to assemble the package so it can be uploaded to PyPI and installed with pip (name, version, etc.).

    from distutils.core import setup
    
    setup(
       name='project', 
       version='0.1dev',
       packages=['emoji',],
       license='MIT', 
       long_description=open('README.txt').read(),
    )
    
    

    After creating the setup.py file, upload it to PyPI, or use the command line to create a binary distribution (an executable installer).

    To build a source distribution, use the command line to navigate to the directory containing setup.py, and run the command python setup.py sdist.
    Run python setup.py bdist or, for Windows, python setup.py bdist_wininst to build a binary distribution.
    Use python setup.py register, followed by python setup.py sdist upload to upload a package.
    Finally, install a package with python setup.py install.

    #  build a source distribution
    $ python setup.py sdist
    
    # upload a package
    $ python setup.py register
    
    # build a binary distribution
    $ python setup.py bdist
    
    # install a package
    $ python setup.py install
    
    

    Packaging

    package a Python script to executable file

    However, many computer users who are not programmers do not have Python installed.

    Therefore, it is useful to package scripts as executable files for the relevant platform, such as the Windows or Mac operating systems.

    This is not necessary for Linux, as most Linux users do have Python installed, and are able to run scripts as they are.

    for Windows, use py2exe, PyInstaller, cx_Freeze

    For Macs, use py2app, PyInstaller, cx_Freeze

    Python Style Guide

    Zen of Python

    Beautiful is better than ugly.
    Explicit is better than implicit.
    Simple is better than complex.
    Complex is better than complicated.
    Flat is better than nested.
    Sparse is better than dense.
    Readability counts.
    Special cases aren't special enough to break the rules.
    Although practicality beats purity.
    Errors should never pass silently.
    Unless explicitly silenced.
    In the face of ambiguity, refuse the temptation to guess.
    There should be one-- and preferably only one --obvious way to do it.
    Although that way may not be obvious at first unless you're Dutch.
    Now is better than never.
    Although never is often better than right now.
    If the implementation is hard to explain, it's a bad idea.
    If the implementation is easy to explain, it may be a good idea.
    Namespaces are one honking great idea -- let's do more of those!

    PEP 8

    Python Enhancement Proposals (PEP)

    PEP 8 is a style guide on the subject of writing readable code.

    refs

    https://pypi.org/manage/projects/

    https://packaging.python.org/

    https://www.sololearn.com/Course/Python/

    https://www.sololearn.com/Play/Python

    https://www.sololearn.com/Certificate/Python/jpg/

    https://www.sololearn.com/Profile/3476348/Python



    ©xgqfrms 2012-2020

    www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!


  • 相关阅读:
    基线 css
    a与a:link、a:visited、a:hover、a:active
    去除iPhone的默认input样式
    iphone 数字字段颜色兼容问题
    javascript高级程序设计
    a标签 打电话 发邮件
    给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组。
    先编写函数EncryptChar,按照下述规则将给定的字符c转化(加密)为新的字符
    一个数如果恰好等于它的因子之和,这个数就称为“完数”。例如6=1+2+3。编程找出1000以内的所有完数。
    古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少?
  • 原文地址:https://www.cnblogs.com/xgqfrms/p/13522683.html
Copyright © 2011-2022 走看看