python-setup

python-setup

前言

之前在安包的时候经常会碰到setup.py这个文件,也没有在意,但是后来就越来越不懂了,不明白它到底做了什么。所以这次要clear这一块儿。 在demo下建立了一个文件如下图所示

.
└── setup.py


在里面写入

from setuptools import setup, find_packages

setup(
    name="demo",    
    version="0.1",
    packages = find_packages(),
    )   

其中name是包名, version是版本,packages是所包含的其它的包。 然后 执行

python setup.py bdist_egg

然后结构变成这样了

.
├── build
│   └── bdist.linux-x86_64
├── demo.egg-info
│   ├── dependency_links.txt
│   ├── PKG-INFO
│   ├── SOURCES.txt
│   └── top_level.txt
├── dist
│   └── demo-0.1-py3.6.egg
└── setup.py


其中在dist中生成的是.egg包,其实是个zip包,解开看看里面是

.
├── build
│   └── bdist.linux-x86_64
├── demo.egg-info
│   ├── dependency_links.txt
│   ├── PKG-INFO
│   ├── SOURCES.txt
│   └── top_level.txt
├── dist
│   ├── demo-0.1-py3.6.egg
│   └── EGG-INFO
│       ├── dependency_links.txt
│       ├── PKG-INFO
│       ├── SOURCES.txt
│       ├── top_level.txt
│       └── zip-safe
└── setup.py


现在给包加一些内容,文件目录现在变成这样

.
├── build
│   └── bdist.linux-x86_64
├── demo
│   └── __init__.py
├── demo.egg-info
│   ├── dependency_links.txt
│   ├── PKG-INFO
│   ├── SOURCES.txt
│   └── top_level.txt
├── dist
│   ├── demo-0.1-py3.6.egg
│   └── EGG-INFO
│       ├── dependency_links.txt
│       ├── PKG-INFO
│       ├── SOURCES.txt
│       ├── top_level.txt
│       └── zip-safe
└── setup.py

即又加了一个demo, 然后 里面有个__init__.py文件,然后 里面的内容 是

def test():
    print("HHHHHHHHHHHHHHH")

if __name__ == "__main__":
    test()


为了冲突先把之前的删除掉,目录结构变成

.
├── demo
│   └── __init__.py
└── setup.py

再次执行 python setup.py bdist_egg

是这样的结构

.
├── build
│   ├── bdist.linux-x86_64
│   └── lib
│       └── demo
│           └── __init__.py
├── demo
│   └── __init__.py
├── demo.egg-info
│   ├── dependency_links.txt
│   ├── PKG-INFO
│   ├── SOURCES.txt
│   └── top_level.txt
├── dist
│   └── demo-0.1-py3.6.egg
└── setup.py

解压demo*.egg之后是这样的

.
├── build
│   ├── bdist.linux-x86_64
│   └── lib
│       └── demo
│           └── __init__.py
├── demo
│   └── __init__.py
├── demo.egg-info
│   ├── dependency_links.txt
│   ├── PKG-INFO
│   ├── SOURCES.txt
│   └── top_level.txt
├── dist
│   ├── demo
│   │   ├── __init__.py
│   │   └── __pycache__
│   │       └── __init__.cpython-36.pyc
│   ├── demo-0.1-py3.6.egg
│   └── EGG-INFO
│       ├── dependency_links.txt
│       ├── PKG-INFO
│       ├── SOURCES.txt
│       ├── top_level.txt
│       └── zip-safe
└── setup.py

然后 运行 python setup.py install,出现的是这样的

running install
running bdist_egg
running egg_info
writing demo.egg-info/PKG-INFO
writing dependency_links to demo.egg-info/dependency_links.txt
writing top-level names to demo.egg-info/top_level.txt
reading manifest file 'demo.egg-info/SOURCES.txt'
writing manifest file 'demo.egg-info/SOURCES.txt'
installing library code to build/bdist.linux-x86_64/egg
running install_lib
running build_py
creating build/bdist.linux-x86_64/egg
creating build/bdist.linux-x86_64/egg/demo
copying build/lib/demo/__init__.py -> build/bdist.linux-x86_64/egg/demo
byte-compiling build/bdist.linux-x86_64/egg/demo/__init__.py to __init__.cpython-36.pyc
creating build/bdist.linux-x86_64/egg/EGG-INFO
copying demo.egg-info/PKG-INFO -> build/bdist.linux-x86_64/egg/EGG-INFO
copying demo.egg-info/SOURCES.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
copying demo.egg-info/dependency_links.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
copying demo.egg-info/top_level.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
zip_safe flag not set; analyzing archive contents...
creating 'dist/demo-0.1-py3.6.egg' and adding 'build/bdist.linux-x86_64/egg' to it
removing 'build/bdist.linux-x86_64/egg' (and everything under it)
Processing demo-0.1-py3.6.egg
Copying demo-0.1-py3.6.egg to /home/pengkun/anaconda3/lib/python3.6/site-packages
Adding demo 0.1 to easy-install.pth file

Installed /home/pengkun/anaconda3/lib/python3.6/site-packages/demo-0.1-py3.6.egg
Processing dependencies for demo==0.1
Finished processing dependencies for demo==0.1


注意到其中的copying… 和Copying…

Copying demo-0.1-py3.6.egg to /home/pengkun/anaconda3/lib/python3.6/site-packages

这时候在python终端可以看一下

>>> import demo
>>> demo.test()
HHHHHHHHHHHHHHH

2分钟体会一下这里面的妙处。

下面还有更妙的,上面还要import一下,下面再改一下可以做到不用import,直接就像python一样 直接python就可以运行起来。

from setuptools import setup, find_packages

setup(
    name="demo",    
    version="0.1",
    packages = find_packages(),
    

    entry_points={
        'console_scripts':['pengkun = demo:test','bar = demo:test',],

        'gui_scripts':['baz = demo:test',]
        }
    )   


然后再安装 python setup.py install

这时候会看到

Installing bar script to /home/pengkun/anaconda3/bin
Installing pengkun script to /home/pengkun/anaconda3/bin
Installing baz script to /home/pengkun/anaconda3/bin

如果不用anaconda的话,会在

/usr/local/bin 下面

接下来就是妙处了

pengkun@ubuntu:~$ pengkun
HHHHHHHHHHHHHHH
pengkun@ubuntu:~$ bar
HHHHHHHHHHHHHHH
pengkun@ubuntu:~$ baz
HHHHHHHHHHHHHHH
pengkun@ubuntu:~$ 

一打名字就直接运行了。

这样如果写一个大型的包的话,每次调的时候都太麻烦,这样弄了之后就可以了,而且不容易看到其内部是怎么回事儿,还有一定的保密性呢。

打赏,谢谢~~

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,多谢支持~

打开微信扫一扫,即可进行扫码打赏哦