Emacs for Python


emacs for python

  • python mode
    • flycheck support flake8, pylint and pycompile
    • pip install flake8, jedi, elpy
    • interpreter in emacs
  • autopep8: auto format code
    • pip install autopep8
  • company-jedi: code completion
    • include jedi-core, do not install jedi if you prefer company-mode
      • c-c , jedi:goto-definition-pop-marker
      • c-c . jedi:goto-definition
      • c-c ? jedi:show-doc
  • elpy
    • support virtualenv

Youtube 影片

emacs configuration

(use-package python
  :ensure t
  :mode ("\\.py\\'" . python-mode)
  :interpreter ("python" . python-mode)
  (setq-default indent-tabs-mode nil)
  (setq-default python-indent-offset 4)
  (setq flycheck-flake8rc "~/.config/flake8")
  (use-package py-autopep8
    :ensure t
    :hook ((python-mode . py-autopep8-enable-on-save))
(use-package company-jedi
  :ensure t
  (add-hook 'python-mode-hook 'jedi:setup)
  (add-hook 'python-mode-hook (lambda ()
                                (add-to-list (make-local-variable 'company-backends)
(use-package elpy
  :ensure t
  :commands (elpy-enable)
  (setq elpy-rpc-backend "jedi"))

Python project

  • for Linux (CentOS 7) to install python36
$sudo yum install -y centos-release-scl-rh
$sudo yum install -y rh-python36
$sudo yum install -y devtoolset-7
  • add following files to /etc/profile.d enabledevtools.sh
source scl_source enable devtoolset-7


source scl_source enable rh-python36

Install opencv for linux (CentOS 7)

$yum groupinstall "Development Tools" -y
$yum install cmake3 gtk2-devel numpy pkgconfig -y
$yum install python36-numpy python36-devel libdc1394-devel libv4l-devel gstreamer-plugins-base-devel
$git clone https://github.com/opencv/opencv.git
$cd opencv
$git checkout 3.5
$mkdir build
$cd build
$cmake -D WITH_OPENCL=OFF -D WITH_CUDA=OFF -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D BUILD_opencv_python3=yes -D PYTHON3_EXECUTABLE=/opt/rh/rh-python36/root/usr/bin/python -D PYTHON3_INCLUDE_DIR=/opt/rh/rh-python36/root/usr/include/python3.6m -D PYTHON3_LIBRARY=/opt/rh/rh-python36/root/usr/lib64/libpython3.6m.so.rh-python36-1.0 -d PYTHON3_NUMPY_INCLUDE_DIRS=/opt/rh/rh-python36/root/usr/lib64/python3.6/site-packages/numpy/core/include -DWITH_FFMPEG=OFF ..
$make -j 2
$sudo make install
$cd /opt/rh/rh-python36/root/usr/lib64/python3.6/site-packages
$sudo ln -s /usr/local/lib/python3.6/site-packages/cv2.cpython-36-x86_64-linux-gnu.so cv2.so

Install opencv for Mac OS

$git clone https://github.com/opencv/opencv
$cd opencv
$mkdir build
$cd build
$cmake -DPYTHON_DEFAULT_EXECUTABLE=python3 -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/usr/local -DBUILD_opencv_python2=False -DBUILD_opencv_python3=True -DPYTHON3_EXECUTABLE=python3 ..
$make j2
$sudo make install
# for virtualenv
$echo /usr/local/lib/python3.6/site-packages >> /Users/jerryhsieh/Developer/Web/Python/ENV/lib/python3.6/site-packages/opencv3.pth

Python opencv face detect

import cv2
import sys
# Get user supplied values
imagePath = sys.argv[1]
cascPath = "haarcascade_frontalface_default.xml"
# Create the haar cascade
faceCascade = cv2.CascadeClassifier(cascPath)
# Read the image
image = cv2.imread(imagePath)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Detect faces in the image
faces = faceCascade.detectMultiScale(
    minSize=(30, 30)
    #flags = cv2.CV_HAAR_SCALE_IMAGE
print("Found {0} faces!".format(len(faces)))
# Draw a rectangle around the faces
for (x, y, w, h) in faces:
    cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2)
cv2.imshow("Faces found", image)