使用setup.py打包python文件

setup.py

# coding: utf-8

from setuptools import setup

setup(
        name = "Test",
        py_modules = ['my_test', 'great_class'],
        entry_points = {
            'console_scripts': ['Test = my_test:fun']
        },
        data_files = ['_great_class.so'] # 指定其他的一些文件(如配置文件)
)

setup(
        name = "Test2",
        py_modules = ['my_test2'],
        entry_points = {
            'console_scripts': ['Test2 = my_test2:fun']
        }
)
       

great_class.h

#ifndef GREAT_CLASS
#define GREAT_CLASS

class Great {
        private:
                int s;
        public:
                void setWall(int _s) {
                        s = _s;
                }
                int getWall() {
                        return s;
                }
        };

#endif //GREAT_CLASS

great_class.i

%module great_class

%{
#include "great_class.h"
%}

%include "great_class.h"

通過swig生成_great_class.so文件

mytest.py

#!/usr/bin/env python
import great_class

def fun():
    c = great_class.Great()

    c.setWall(5)

    print c.getWall()

if __name__ == '__main__':

    fun()

mytest2.py

#!/usr/bin/env python

def fun0(a):
    print "wo jiu shi shi", a

def fun():
    a = "miao"
    fun0(a)

if __name__ == '__main__':
    fun()

執行命令: python setup.py install

運行結果

Python 庫打包分發(setup.py 編寫)簡易指南: http://blog.konghy.cn/2018/04/29/setup-dot-py/

Python腳本報錯AttributeError: ‘module’ object has no attribute’xxx’解決方法:http://lovesoo.org/python-script-error-attributeerror-module-object-has-no-attribute-solve-method.html

查看import庫的源文件,發現源文件存在且沒有錯誤,同時存在源文件的.pyc文件

問題解決方法:

1. 命名py腳本時,不要與python預留字,模塊名等相同

2. 刪除該庫的.pyc文件(因爲py腳本每次運行時均會生成.pyc文件;在已經生成.pyc文件的情況下,若代碼不更新,運行時依舊會走pyc,所以要刪除.pyc文件),重新運行代碼;或者找一個可以運行代碼的環境,拷貝替換當前機器的.pyc文件即可

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章