setuptools使用

一、安装

activate python_test
pip install --upgrade pip setuptools==45.2.0

二、代码结构

新建一个项目(SetupToolsTest)

图片.jpg

代码

MANIFEST.in
# 打包其余的文件(include_package_data = True)
include demo/files/*.exe
include demo/files/*.txt
setup.py
from setuptools import setup,find_packages

setup(
    name="demo",
    version="1.0",
    author="cnh",
    author_email="129185@qq.com",
    url = 'www.katexiaohao.xyz',
    packages = find_packages(),
    include_package_data = True,  
    install_requires=[
    'numpy',
    ]
)
hello.py
import numpy
def hello_func():
    print("HelloWorld")
# files文件下的文件只是为了测试MANIFEST.in 和 include_package_data = True 无实际作用
def read_hello():
    with open(r'E:\PycharmProjectTest\SetupToolsTest\demo\files\HelloWorld.txt','r',encoding='utf-8') as f:
        print(f.read())
myapp.py
def myapp_func():
    print("嘿嘿嘿")

三、打包安装

①打包测试

# 先进入setup.py再执行操作
python .\setup.py check
# out:running check 正常输出

②正式打包

# 这句命令直接安装到当前环境site-packages
# 如:d:\install\anaconda\envs\python_test\lib\site-packages
python .\setup.py install

输出:

running install
running bdist_egg
running egg_info
creating demo.egg-info
writing demo.egg-info\PKG-INFO
writing dependency_links to demo.egg-info\dependency_links.txt
writing requirements to demo.egg-info\requires.txt
writing top-level names to demo.egg-info\top_level.txt
writing manifest file 'demo.egg-info\SOURCES.txt'
reading manifest file 'demo.egg-info\SOURCES.txt'
reading manifest template 'MANIFEST.in'
writing manifest file 'demo.egg-info\SOURCES.txt'
installing library code to build\bdist.win-amd64\egg
running install_lib
running build_py
creating build
creating build\lib
creating build\lib\demo
copying demo\hello.py -> build\lib\demo
copying demo\myapp.py -> build\lib\demo
copying demo\__init__.py -> build\lib\demo
creating build\lib\demo\files
copying demo\files\ChromeSetup.exe -> build\lib\demo\files
copying demo\files\HelloWorld.txt -> build\lib\demo\files
copying demo\files\dingtalk_downloader.exe -> build\lib\demo\files
copying demo\files\python-3.7.4-amd64.exe -> build\lib\demo\files
creating build\bdist.win-amd64
creating build\bdist.win-amd64\egg
creating build\bdist.win-amd64\egg\demo
creating build\bdist.win-amd64\egg\demo\files
copying build\lib\demo\files\ChromeSetup.exe -> build\bdist.win-amd64\egg\demo\files
copying build\lib\demo\files\dingtalk_downloader.exe -> build\bdist.win-amd64\egg\demo\files
copying build\lib\demo\files\HelloWorld.txt -> build\bdist.win-amd64\egg\demo\files
copying build\lib\demo\files\python-3.7.4-amd64.exe -> build\bdist.win-amd64\egg\demo\files
copying build\lib\demo\hello.py -> build\bdist.win-amd64\egg\demo
copying build\lib\demo\myapp.py -> build\bdist.win-amd64\egg\demo
copying build\lib\demo\__init__.py -> build\bdist.win-amd64\egg\demo
byte-compiling build\bdist.win-amd64\egg\demo\hello.py to hello.cpython-37.pyc
byte-compiling build\bdist.win-amd64\egg\demo\myapp.py to myapp.cpython-37.pyc
byte-compiling build\bdist.win-amd64\egg\demo\__init__.py to __init__.cpython-37.pyc
creating build\bdist.win-amd64\egg\EGG-INFO
copying demo.egg-info\PKG-INFO -> build\bdist.win-amd64\egg\EGG-INFO
copying demo.egg-info\SOURCES.txt -> build\bdist.win-amd64\egg\EGG-INFO
copying demo.egg-info\dependency_links.txt -> build\bdist.win-amd64\egg\EGG-INFO
copying demo.egg-info\requires.txt -> build\bdist.win-amd64\egg\EGG-INFO
copying demo.egg-info\top_level.txt -> build\bdist.win-amd64\egg\EGG-INFO
zip_safe flag not set; analyzing archive contents...
creating dist
creating 'dist\demo-1.0-py3.7.egg' and adding 'build\bdist.win-amd64\egg' to it
removing 'build\bdist.win-amd64\egg' (and everything under it)
Processing demo-1.0-py3.7.egg
Removing d:\install\anaconda\envs\python_test\lib\site-packages\demo-1.0-py3.7.egg
Copying demo-1.0-py3.7.egg to d:\install\anaconda\envs\python_test\lib\site-packages
demo 1.0 is already the active version in easy-install.pth

Installed d:\install\anaconda\envs\python_test\lib\site-packages\demo-1.0-py3.7.egg
Processing dependencies for demo==1.0
Searching for numpy==1.21.2
Best match: numpy 1.21.2
Adding numpy 1.21.2 to easy-install.pth file
Installing f2py-script.py script to D:\install\Anaconda\envs\python_test\Scripts
Installing f2py.exe script to D:\install\Anaconda\envs\python_test\Scripts

Using d:\install\anaconda\envs\python_test\lib\site-packages
Finished processing dependencies for demo==1.0

③文件目录:

图片.jpg

四、验证使用

①新建测试文件

from demo import hello,myapp
hello.hello_func()
hello.read_hello()
myapp.myapp_func()
运行后输出:
HelloWorld
我是一段用于测试打包环境的文字
嘿嘿嘿

五、结语

入门使用就这些,更高深的用法在实践中去发现吧。。。
THE END
分享
二维码
打赏
< <上一篇
下一篇>>