How to using PyPI publish a Python package

PyPI & Python package

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 发布文章使用:只允许注册用户才可以访问!


How to using PyPI publish a Python package的更多相关文章

  1. Python package下载中遇到ReadTimeoutError: HTTPSConnectionPool?

    问题描述: Python package下载中遇到ReadTimeoutError: HTTPSConnectionPool? 问题解决: 方法1:继续重复下载 pip install virtual ...

  2. 在pypi上发布python包详细教程

    使用Python编程中Python的包安装非常方便,一般都是可以pip来安装搞定:pip install <package name>,我们自己写的python也可以发布在pypi上,很简 ...

  3. ros2中创建一个python package

    完整的python package的目录结构如下: source /opt/ros/dashing/setup.bash cd ros2_ws/src && ros2 pkg crea ...

  4. 利用setuptools发布Python程序到PyPI,为Python添砖加瓦

    pip install的东西从哪里来的? 从PyPI (Python Package Index)来的,官网是:  https://pypi.python.org/pypi/执行pip install ...

  5. Python package钓鱼

    Python package钓鱼   一.概述 在收录该文之后,知道创宇404安全实验室对该文中所提到的攻击方式进行跟进.整理分析原作者公布的钓鱼数据.值得一提的是,在跟进的过程中,我们发现了新的钓鱼 ...

  6. Ghost.py 0.1b3 : Python Package Index

    Ghost.py 0.1b3 : Python Package Index Ghost.py 0.1b3 Download Ghost.py-0.1b3.tar.gz Webkit based web ...

  7. pyrailgun 0.24 : Python Package Index

    pyrailgun 0.24 : Python Package Index pyrailgun 0.24 Download pyrailgun-0.24.zip Fast Crawler For Py ...

  8. qrcode 4.0.4 : Python Package Index

    qrcode 4.0.4 : Python Package Index qrcode 4.0.4 Download qrcode-4.0.4.tar.gz QR Code image generato ...

  9. bottle-session 0.3 : Python Package Index

    bottle-session 0.3 : Python Package Index bottle-session 0.3

随机推荐

  1. 知道 Redis-Cluster 么?说说其中可能不可用的情况

    Redis 集群模式简述 一个集群模式的官方推荐最小最佳实践方案是 6 个节点,3 个 Master 3 个 Slave 的模式,如 图00 所示. key 分槽与转发机制 Redis 将键空间分为了 ...

  2. SpringBoot - 实现文件上传2(多文件上传、常用上传参数配置)

    在前文中我介绍了 Spring Boot 项目如何实现单文件上传,而多文件上传逻辑和单文件上传基本一致,下面通过样例进行演示. 多文件上传 1,代码编写 1)首先在 static 目录中创建一个 up ...

  3. vim快捷键收藏版

    总述 附加一篇介绍文哈,关于vim快捷键的介绍.vim和vscode 到底谁更好用,大家争得不可开交,然后我就在vscode里面装了一个vim插件,完美得解决了这个问题,用完之后觉得真香,所以我就整理 ...

  4. c++nullptr(空指针常量)、constexpr(常量表达式)

    总述     又来更新了,今天带来的是nullptr空指针常量.constexpr(常量表达式)C++的两个用法.Result result_fun = nullptr;constexpr stati ...

  5. POJ - 3376 Finding Palindromes(拓展kmp+trie)

    传送门:POJ - 3376 题意:给你n个字符串,两两结合,问有多少个是回文的: 题解:这个题真的恶心,我直接经历了5种错误类型 : ) ... 因为卡内存,所以又把字典树改成了指针版本的. 字符串 ...

  6. 【bzoj 2038】 [2009国家集训队]小Z的袜子(算法效率--莫队分块算法 模版题)

    题意:小Z有N只袜子,有不同的颜色.他有M个提问,问从编号为[L,R]的袜子中随机选一双同色的袜子的概率,用最简分数表示. 解法:经典的莫队算法--无修改.不强制在线(可离线).状态转移可以一步完成. ...

  7. 【uva 1614】Hell on the Markets(算法效率--贪心)

    题意:有一个长度为N的序列A,满足1≤Ai≤i,每个数的正负号不知.请输出一种正负号的情况,使得所有数的和为0.(N≤100000) 解法:(我本来只想静静地继续做一个口胡选手...←_← 但是因为这 ...

  8. 分组背包 例题:hdu 1712 ACboy needs your help

    分组背包需求 有N件物品,告诉你这N件物品的重量以及价值,将这些物品划分为K组,每组中的物品互相冲突,最多选一件,求解将哪些物品装入背包可使这些物品的费用综合不超过背包的容量,且价值总和最大. 解题模 ...

  9. HDU 2176 取(m堆)石子游戏 && HDU1850 Being a Good Boy in Spring Festivaly

    HDU2176题意: m堆石子,两人轮流取.只能在1堆中取.取完者胜.先取者负输出No.先取者胜输出Yes,然后输出怎样取子. 通过 SG定理 我们可以知道每一个数的SG值,等于这个数到达不了的前面数 ...

  10. Codeforces Round #501 (Div. 3) B. Obtaining the String (思维,字符串)

    题意:有两个字符串\(S\)和\(T\),判断\(T\)是否能由\(S\)通过交换某位置的相邻字符得到,如果满足,输出交换次数及每次交换的位置,否则输出\(-1\). 题解:首先判断不满足的情况,只有 ...