python项目打包相关知识

我们两清 提交于 2020-02-27 06:48:14

一、编译为so:

编译除__init__.py之外的py文件,并且除__init__.py以外,不拷贝其他.py文件


from setuptools import setup, find_packages
from distutils.command.build_py import build_py as build_py_orig
from Cython.Build import cythonize

class CustBbuildPy(build_py_orig):
    def find_modules(self):
        modules = super().find_modules()
        modules = [module for module in modules if module[1] == "__init__"]  # 只拷贝__init__.py
        return modules

    def find_package_modules(self, package, package_dir):
        modules = super().find_package_modules(package, package_dir)
        modules = [module for module in modules if module[1] == "__init__"]  # 只拷贝__init__.py
        return modules

packages = find_packages()
name = packages[0]
setup(
    name=name,
    version='0.0.1',
    packages=packages,
    ext_modules=cythonize("app_installer/**/*.py", exclude="**/__init__.py", build_dir="./build/temp.c_sources/"),
    cmdclass={'build_py': CustBbuildPy},
    classifiers=[
        "Programming Language :: Python :: 3",
        "License :: OSI Approved :: MIT License",
        "Operating System :: POSIX :: Linux",
        "Operating System :: Microsoft :: Windows :: Windows 10",
    ],
    python_requires='>=3.7',
    setup_requires=["wheel", "Cython"]
      )

二、编译为pyd:

在window下编译为pyd,默认使用Microsoft Visual C++ 14.0,没有安装时会报错,可以使用mingw32来编译

error: Microsoft Visual C++ 14.0 is required. Get it with "Microsoft Visual C++ Build Tools": https://visualstudio.microsoft.com/downloads/

from distutils.command.build_ext import build_ext as build_ext_orig
class CustomBuildExt(build_ext_orig):
    def __init__(self, dist):
        super().__init__(dist)
        if platform.system() == "Windows":
            self.compiler = "mingw32"
            self.define = "MS_WIN64"

    def initialize_options(self):
        super().initialize_options()
        if platform.system() == "Windows":
            self.compiler = "mingw32"
            self.define = "MS_WIN64"

setup(
    .......,
    ext_modules=cythonize("app_installer/**/*.py", exclude="**/__init__.py", build_dir="./build/temp.c_sources/"),
    cmdclass={"build_ext": CustomBuildExt},
    .......
)

或者使用命令行指定

python setup.py clean --all build_ext --compiler=mingw32 --inplace -DMS_WIN64 build_wheel

clean all: 清除临时文件

build_ext构建扩展模块

build_wheel 打包为wheel

三、打包发布为wheel:

先清理临时文件,再打包: python3 setup.py clean -all bdist_wheel


from setuptools import setup, find_packages


packages = find_packages()
name = packages[0]
setup(
    name=name,
    version='0.0.1',
    packages=packages
    classifiers=[
        "Programming Language :: Python :: 3",
        "License :: OSI Approved :: MIT License",
        "Operating System :: POSIX :: Linux",
        "Operating System :: Microsoft :: Windows :: Windows 10",
    ],
    python_requires='>=3.7',
    setup_requires=["wheel"]
      )

四、打包为可执行文件:

pip install pyinstaller
pyinstall -F Hello.py

五、资料参考:

1、官网:

https://packaging.python.org/

https://packaging.python.org/tutorials/packaging-projects/

https://packaging.python.org/guides/packaging-binary-extensions/

2、其他:

https://www.cnblogs.com/yangwm/p/11243346.html

https://stackoverflow.com/questions/39499453/package-only-binary-compiled-so-files-of-a-python-library-compiled-with-cython
https://www.cnblogs.com/xueweihan/p/12030457.html
https://zhuanlan.zhihu.com/p/57967281
https://zhuanlan.zhihu.com/p/25308951
https://blog.csdn.net/daniel_ustc/article/details/77622895

解释下相关参数:

‘cython_evaluate’ 是我们要生成的动态链接库的名字
sources 里面可以包含 .pyx 文件,以及后面如果我们要调用 C/C++ 程序的话,还可以往里面加 .c / .cpp 文件
language 其实默认就是 c,如果要用 C++,改成 c++
include_dirs 这个就是传给 gcc 的 -I 参数(numpy.get_include()其实这个只是示例,本程序不需要)
library_dirs 这个就是传给 gcc 的 -L 参数
libraries 这个就是传给 gcc 的 -l 参数
extra_compile_args 就是传给 gcc 的额外的编译参数,比方说你可以传一个 -std=c++11
extra_link_args 就是传给 gcc 的额外的链接参数(也就是生成动态链接库的时候用的)

Linux使用ssh超时断开连接的真正原因
http://bluebiu.com/blog/linux-ssh-session-alive.html#fn:note_3

3、修改编译选项

https://www.codenong.com/724664/

from distutils.core import setup, Extension
from distutils.command.build_ext import build_ext
copt =  {'msvc': ['/openmp', '/Ox', '/fp:fast','/favor:INTEL64','/Og']  ,
     'mingw32' : ['-fopenmp','-O3','-ffast-math','-march=native']       }
lopt =  {'mingw32' : ['-fopenmp'] }

class build_ext_subclass( build_ext ):
    def build_extensions(self):
        c = self.compiler.compiler_type
        if copt.has_key(c):
           for e in self.extensions:
               e.extra_compile_args = copt[ c ]
        if lopt.has_key(c):
            for e in self.extensions:
                e.extra_link_args = lopt[ c ]
        build_ext.build_extensions(self)

mod = Extension('_wripaca',
            sources=['../wripaca_wrap.c',
                     '../../src/wripaca.c'],
            include_dirs=['../../include']
            )

setup (name = 'wripaca',
   ext_modules = [mod],
   py_modules = ["wripaca"],
   cmdclass = {'build_ext': build_ext_subclass } )

4、pip安装包时缺少Microsoft Visual C++编译错误时,可以采用以下的方法试试(本人没有亲自测试过)

https://github.com/pypa/pip/issues/18

错误:

error: Unable to find vcvarsall.bat

# 方法1
setup.py install build --compiler=mingw32

# 方法2
pip install --build-option="--compiler=mingw32" psycopg2

# 方法3
pip install --no-install SomePackage
cd <venv>/build/SomePackage
python setup.py build --compiler=special-compiler
pip install --no-download SomePackage

# 方法4
pip install --global-option build_ext --global-option --compiler=mingw32 foo

# 方法5
python setup.py build_ext --compiler=mingw32 install


5、cython: 使用mingw编译器

https://www.jianshu.com/p/50105307dea5

6、setup相关参考资料

https://www.cnblogs.com/cposture/p/9029023.html

https://blog.51cto.com/9291927/2450914

https://www.codenong.com/50938128/

https://blog.gmem.cc/python-study-note https://xbuba.com/questions/39499453

https://blog.csdn.net/qq_39622065/article/details/84205289 https://zhuanlan.zhihu.com/p/57967281

https://bucharjan.cz/blog/using-cython-to-protect-a-python-codebase.html

https://blog.csdn.net/feijiges/article/details/77932382

7、pyinstaller相关参考资料

https://blog.csdn.net/m0_37477175/article/details/82146996
https://blog.csdn.net/qiqiyingse/article/details/84950497
https://www.jb51.net/article/177030.htm
https://zhuanlan.zhihu.com/p/40716095
https://pythonhosted.org/PyInstaller/man/pyinstaller.html
https://juejin.im/entry/5b18c4a2e51d4506ae71a347

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!