Python notes

Cài đặt Python trên môi trường windows

Môi trường

  • Windows 8.1
  • Python 2.7.6

Thực hiện

  • Truy cập trang chủ của Python
  • Vào mục downloads để tải phiên bản phù hợp (phiên bản 2.7.6)
  • Chạy tập tin Python-….msi và làm theo hướng dẫn (chọn thư mục cài đặt C:\abc\Python2.7.6)
  • Thêm đường dẫn “C:\abc\Python2.7.6” vào biến môi trường (Environment variables) “Path”

Cài đặt pip

Tham khảo

Môi trường windows

  • Download từ link nguồn: https://pip.pypa.io/en/latest/installing.html
  • Thực thi lệnh
1
python get-pip.py
  • Gán biến môi trường cho thư mục Scripts trong thư mục cài đặt Python

Môi trường Mac

  • Thực thi lệnh sau
1
sudo easy_install pip
  • Hoặc download và di chuyển tới thư mục chứa tập tin get-pip.py thực thi lệnh
1
sudo python get-pip.py

Cài đặt iPython (sau khi đã có pip)

  • Thực thi lệnh sau để cài đặt:
1
pip install ipython

Virtualenv

### Cài đặt virtualenv

  • Trên windows: sử dụng lệnh: “easy_install virtualenv” trong cmd
  • Trên Mac: sử dụng lệnh: “sudo easy_install virtualenv” trong terminal

Sử dụng

  • Tạo môi trường tên abc
    • virtualenv –no-site-packages abc: tạo môi trường không sử dụng các packages
    • virtualenv abc
  • Kích hoạt môi trường bằng cách chạy scripts của nó
    • “abc/bin/activate”: trên windows
    • “source working/bin/activate”: trên linux hay MAC
  • Chạy lệnh “deactive” để thoát

Python trên emacs

Cài đặt và cấu hình

  • Cài thêm gói role/flake8/jedi cho python: thực thi lệnh
1
2
3
pip install flake8
pip install rope
pip install jedi
  • Cài đặt Python-mode package, iPython package. Cấu hình
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
; python-mode
(setq py-install-directory "~/.emacs.d/elpa/python-mode-6.1.3")
(add-to-list 'load-path py-install-directory)
(require 'python-mode)
; use IPython
(setq-default py-shell-name "ipython")
(setq-default py-which-bufname "IPython")
; use the wx backend, for both mayavi and matplotlib
(setq py-python-command-args
  '("--gui=wx" "--pylab=wx" "-colors" "Linux"))
(setq py-force-py-shell-name-p t)
; switch to the interpreter after executing code
(setq py-shell-switch-buffers-on-execute-p t)
(setq py-switch-buffers-on-execute-p t)
; don't split windows
;(setq py-split-windows-on-execute-p nil)
; try to automagically figure out indentation
(setq py-smart-indentation t)
  • Cài đặt elpy package
  • Cấu hình trong emacs. Thêm vào file cấu hình .emacs:
1
2
(package-initialize)
(elpy-enable)

Sử dụng

  • Mở tập tin python, nhấn “Ctrl-c !” => cửa sổ thực thi của iPython sẽ hiện ra => không sử dụng vì dùng command.

Python Tools/Utilities

TODO TPL - dis module: disassembler - pdb module: debug - profile module - tabnanny module

Một số lưu ý

  • Tên biến:
    • Bắt đầu bởi A -> Z hoặc a -> z hoặc underscore(_)
    • Không cho phép ký tự @, $, %
    • Phân biệt viết hoa, viết thường
  • Code blocks: Không được bao bởi các dấu ( { [. Được nhận biết bởi dòng và canh lề
1
2
3
4
5
6
if True:
    print "Answer"
    print "True"
else:
    print "Answer"
  print "False"
  • 1 dòng lệnh trên nhiều dòng sẽ dùng dấu \
1
2
3
total = item_one + \
        item_two + \
        item_three
  • Dấu ‘, “, ‘’’ hoặc “””
1
2
3
4
word = 'word'
sentence = "This is a sentence."
paragraph = """This is a paragraph. It is
made up of multiple lines and sentences."""
  • Ghi chú: sử dụng #
  • Xử lý

Subprocess

  • import subprocess
  • subprocess.call([some_command, some_argument, another_argument_or_path])