python 源码模块打包发布

python 源码模块打包发布

使用 setuptools Cython 打包2进制包 wheel

1 安装依赖:
pip install setuptools
pip install Cython
pip install wheel
2 某源码模块 my_module
my_module/
__init__.py  setup.py  test1.py  test2.py  test3.py

其中 test1.py

def fun1(p):
    print(f"fun1:{p}")

其中 test2.py

def fun2(p):
    print(f"fun2:{p}")

其中 test3.py

def fun3(p):
    print(f"fun3:{p}")
3 编写setup.py文件
from setuptools import Extension, find_packages, setup
from Cython.Build import cythonize

# 必须与 my_module 模块文件夹名相同
pkg_name = "my_module"

# 模块下需要打包的py文件
files = ["test1.py" , "test2.py" , "test3.py"]

setup(
    name=pkg_name,
    version="0.0.1",
    author="wmx",
    author_email="[email protected]",
    packages=find_packages(),
    ext_modules=cythonize(files),
    zip_safe=False,
    requires=[# 这里填写模块的所有依赖包
        'requests',
        'json',
        'typing'
    ]
)

4 编译生成 whl 安装文件

python setup.py bdist_wheel  

在my_module/dist 目录下生成 my_module-0.0.1-cp39-cp39-win_amd64.whl

5 安装

pip install ./my_module-0.0.1-cp39-cp39-win_amd64.whl

6 测试

新建 use_module.py

from my_module import test1
from my_module import test2
from my_module import test3

# print("help:")
# help(test1)
# help(test2)
# help(test3)

test1.fun1("sadf")
test2.fun2("xxx")
test2.fun3("jjj")
  • 参考命令 :
    python setup.py --help
PS D:\workspace\Python\test_freeotp\my_module> python setup.py --help   
  
Common commands: (see '--help-commands' for more)

  setup.py build      will build the package underneath 'build/'
  setup.py install    will install the package

Global options:
  --verbose (-v)      run verbosely (default)
  --quiet (-q)        run quietly (turns verbosity off)
  --dry-run (-n)      don't actually do anything
  --help (-h)         show detailed help message
  --no-user-cfg       ignore pydistutils.cfg in your home directory
  --command-packages  list of packages that provide distutils commands

Information display options (just display information, ignore any commands)
  --help-commands     list all available commands
  --name              print package name
  --version (-V)      print package version
  --fullname          print -
  --author            print the author's name
  --author-email      print the author's email address
  --maintainer        print the maintainer's name
  --maintainer-email  print the maintainer's email address
  --contact           print the maintainer's name if known, else the author's
  --contact-email     print the maintainer's email address if known, else the
                      author's
  --url               print the URL for this package
  --license           print the license of the package
  --licence           alias for --license
  --description       print the package description
  --long-description  print the long package description
  --platforms         print the list of platforms
  --classifiers       print the list of classifiers
  --keywords          print the list of keywords
  --provides          print the list of packages/modules provided
  --requires          print the list of packages/modules required
  --obsoletes         print the list of packages/modules made obsolete

usage: setup.py [global_opts] cmd1 [cmd1_opts] [cmd2 [cmd2_opts] ...]
   or: setup.py --help [cmd1 cmd2 ...]
   or: setup.py --help-commands
   or: setup.py cmd --help

python setup.py --help-commands


PS D:\workspace\Python\test_freeotp\my_module> python setup.py --help-commands
Standard commands:
  build             build everything needed to install
  build_py          "build" pure Python modules (copy to build directory)   
  build_ext         build C/C++ extensions (compile/link to build directory)
  build_clib        build C/C++ libraries used by Python extensions
  build_scripts     "build" scripts (copy and fixup #! line)
  clean             clean up temporary files from 'build' command
  install           install everything from build directory
  install_lib       install all Python modules (extensions and pure Python) 
  install_headers   install C/C++ header files
  install_scripts   install scripts (Python or otherwise)
  install_data      install data files
  sdist             create a source distribution (tarball, zip file, etc.)
  bdist_dumb        create a "dumb" built distribution
  bdist_rpm         create an RPM distribution
  bdist_wininst     create an executable installer for MS Windows
  check             perform some checks on the package
  upload            upload binary package to PyPI

Extra commands:
  bdist_wheel       create a wheel distribution
  alias             define a shortcut to invoke one or more commands
  bdist_egg         create an "egg" distribution
  develop           install package in 'development mode'
  dist_info         create a .dist-info directory
  easy_install      Find/get/install Python packages
  egg_info          create a distribution's .egg-info directory
  install_egg_info  Install an .egg-info directory for the package
  rotate            delete older distributions, keeping N newest files
  saveopts          save supplied options to setup.cfg or other config file
  setopt            set an option in setup.cfg or another config file
  test              run unit tests after in-place build (deprecated)
  upload_docs       Upload documentation to sites other than PyPi such as devpi

usage: setup.py [global_opts] cmd1 [cmd1_opts] [cmd2 [cmd2_opts] ...]
   or: setup.py --help [cmd1 cmd2 ...]
   or: setup.py --help-commands
   or: setup.py cmd --help

你可能感兴趣的:(Python,python)