I've been using Cython quite a bit, but sometimes I find it annoying (for example, I need setup.py on a large scale), and I found out about Numba, which makes it easier to speed up Python. Isn't it nice to JIT with llvm like Julia? I also tried it with reference to the following article. If you are new to Numba itself, please see the link first.
-Introduction to Python Acceleration Numba -Python: Acceleration with Numba -[python: Speeding up matrix operations](http://kzky.hatenablog.com/entry/2015/04/11/python%3A_Speeding up matrix operations)
For now, it's only about how to implement it.
It worked below.
sudo easy_install funcsigs enum34 numba
git clone https://github.com/numba/llvmlite
cd llvmlite/
sudo python setup.py install
Please note the following.
--llvmlite can be entered with easy_install, but dylib is not enough and a runtime error (why?) --In addition to enum34, enum can be easily_installed, but it causes an error. --When I tried it on Mac OS 10.9 which is different from the machine I tried first, I got an error with import numba, but it was solved by re-installing numpy. I just built it from github with the following command without thinking too much ...
```
git clone https://github.com/numpy/numpy.git numpy_temp
cd numpy_temp
python setupegg.py bdist_egg
sudo easy_install dist/*egg
```
~~ I will try Windows7 with WinPython2.7 soon, but I think that llvm is necessary, C ++ 11 support is probably necessary, 64bit seems to be difficult, and there are many walls ... ~~ Anaconda I put it in and it passed. I used Anaconda for the first time, but if you have the possibility of gnawing numerical calculations or want to speed up your Python code, it seems good to use it positively.
I will add this later, but I will dare to try it with a process other than numerical calculation. The reason is that the effect on numerical calculation has already been evaluated by the above-mentioned predecessors, and I happen to be making a complicated binary data reading / writing program instead of numerical calculation.
If you use PyInstaller, you can make the Python script executable and it will be easier to distribute it to others, but if you use numba, you will get the following error at runtime. This is because PyInstaller is failing to collect files that Python dynamically dlopens.
OSError: dlopen(/var/folders/qk/z4x58g2962q21q5570g2hj0c0000gn/T/_MEIRXXAjg/llvmlite/binding/libllvmlite.dylib, 6): image not found
So let's include this dylib in the build result. You can work around the problem by following the steps below. PyInstaller uses github tag v2.1.
python pyinstaller / pyinstaller.py --onefile test.py. At this time, test.spec is created and at the same time an exe is created based on this test.spec, but this exe is in the above error state.
Add the following line to test.spec and instruct it to embed libllvmlite.dylib in onefiled executable.
# -*- mode: python -*-
a = Analysis(['test.py'],
pathex=['/path/to/main/script'],
hiddenimports=[],
hookspath=None,
runtime_hooks=None)
#One line below this is added
a.binaries += [("llvmlite/binding/libllvmlite.dylib", "/Library/Python/2.7/site-packages/llvmlite/binding/libllvmlite.dylib", 'BINARY')]
pyz = PYZ(a.pure)
exe = EXE(pyz,
a.scripts,
a.binaries,
a.zipfiles,
a.datas,
name='test',
debug=False,
strip=None,
upx=True,
console=True )
Use python pyinstaller / pyinstaller.py --clean test.spec and rebuild with the modified test.spec.
(Assuming you have installed the environment in C: \ Anaconda) First, when you try to use the Anaconda environment with PyInstaller, you will encounter an error that pywintypes cannot be found regardless of Numba. However, I was able to solve it by placing the dll in the location found in PyInstaller in the method described here.
cd C:\Anaconda\Lib\site-packages\win32\
copy pywintypes27.dll lib\
copy pythoncom27.dll lib\
After that, just write the procurement method of llvmlite.dll in .spec as in the case of Mac. If you installed it in C: \ Anaconda, the line you should add should look like this:
a.binaries += [("llvmlite/binding/llvmlite.dll", "C:\\Anaconda\\Lib\\site-packages\\llvmlite\\binding\\llvmlite.dll", 'BINARY')]
Recommended Posts