步遥情感网
您的当前位置:首页Python 打包成 wheel

Python 打包成 wheel

来源:步遥情感网

`version`:版本号。

`install_requires`:需要安装的依赖。(如果没有代理下得太慢,不如写 `requirements.txt` 然后用国内镜像安装)


```py
from setuptools import find_packages, setup, Extension, Command
setup(name='center_net_obj_det',
      # 表示 CenterNet 文件夹中的都打包
      packages=[p for p in find_packages() if p.startswith('CenterNet')],   
      version='0.0.4'
      # ext_modules=[Extension('object_detection_tutorial', ['object_detection_tutorial.c'])],
      # install_requires=[
      #     'torch==0.4.1',
      #     'opencv-python',
      #     'Cython',
      #     'numba',
      #     'progress',
      #     'matplotlib',
      #     'easydict',
      #     'scipy',
      #     'numpy'
      # ]
      )
```

## 2. 生成 .whl 文件
先安装 `wheel`:`pip install wheel`

直接使用如下命令

```bash
python setup.py bdist_wheel
```

## 3. 安装发布
进入文件夹 `dist`,里面也有一个 `xxx.whl` 文件,输入命令 `pip install xxx.whl` 即可安装。如果有已安装的版本,输入 `pip install xxx.whl --force` 即可重新安装。

安装后通过文件的组织结构来调用库,如某项目的文件架构如下

```
CenterNet
|-src
    |-object_detection_tutorial.py
    |-...
|-...
```

如果想要调用`object_detection_tutorial.py`的`ObjectDetection`类,需要如下导入

```py
from CenterNet.src.object_detection_tutorial import ObjectDetection
```

## 4. Bug
### 4.1 nms.so
报错信息为

```py
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/cuicui/CenterNet/src/object_detection_tutorial.py", line 16, in <module>
    from detectors.detector_factory import detector_factory
  File "/home/cuicui/CenterNet/src/lib/detectors/detector_factory.py", line 5, in <module>
    from .exdet import ExdetDetector
  File "/home/cuicui/CenterNet/src/lib/detectors/exdet.py", line 15, in <module>
    from external.nms import soft_nms
ImportError: /home/cuicui/CenterNet/src/lib/external/nms.so: undefined symbol: _Py_ZeroStruct
```

解决方法为进入对应文件夹重新编译并安装,如下

```bash
$ cd /home/cuicui/CenterNet/src/lib/external/
$ sudo make
$ python setup.py build_ext install
```

### 4.2 .so 文件无法 import
python 打包后 .so 文件无法 import,见[问题](问题.md)第四条

```python
import os
import sys

import xxx      # 这样就可以引用 this_dir 文件夹下的库了
```

因篇幅问题不能全部显示,请点此查看更多更全内容