python脚本打包成so或pyd文件
As everyone knows,Python is an interpreted language. If you don’t want to show your code to others, you can compile Python source code to generate a .pyd library file (for Windows) or a .so library file (for Linux).
Windows python to .pyd
Suppose you now have a test.py
1 | # test.py |
Now you want to compile the test.py into a pyd file. Create a new setup.py as follows:
1 | # setup.py |
1 | python setup.py build |
A build folder will be created with the generated .pyd file in it. Copy the pyd file to the current directory (personal habit) and call .pyd as follows:
1 | from test import add |
Or compile with python setup.py build_ext --inplace
, so that the .pyd file is generated directly in the current directory and then called as described above.
Linux python to .so
Similarly to windows generating .pyd file, also take test.py as an example,build setup.py file
1 | # setup.py |
1 | python3 setup.py build_ext --inplace |
call .so file
1 | import ctypes |
Linux C++ to .so
1 | gcc -shared -f PIC test.c -o test.so |
评论