這樣的開發環境沒問題嗎?

 這樣的開發環境沒問題嗎?

如果把軟體開發比喻成音樂,開發環境就是開發者的樂器。好的開發環境會讓開發過程更順利,減少使用者需要煩惱的外務;當你專注在需要研究的課題上,便能完成更好的作品。最好的開發環境,應該讓你成為更好的開發者。

由於 Python 的根基來自 Unix 文化,它周圍的工具鏈(toolchain)也深受其「do one thing, and do it well」哲學影響,著重於創建單一功能,可與其他工具互動的元件。搭配開源社群常見的「bring your own tools」理念,Python 傳統上並沒有一個「標準」的工具搭配,而是讓大家發揮創意自由組合。這同時是優點,也是詛咒——不論你的背景為何,都可以在 Python 找到習慣的配置,但是經驗較少的人就容易犯錯,用了不適合自己,甚至根本錯誤的開發環境。

講者在這個議程將根據自己參與 Python 工具鏈專案、以及觀察、建議 Python 開發者建構環境的意見,描述常見的開發狀況、常見的組合、容易混淆的工具用法,並根據這些問題提出建議,希望讓大家能夠找到有用的資訊,建構讓自己更得心應手的開發環境。

9dafad54b5b4f360b7aae5f482bc1c91?s=128

Tzu-ping Chung

June 01, 2018
Tweet

Transcript

  1. Ṝᒬత։ᚙ؀ڥ ᔒ໰୊䆩ʁ

  2. None
  3. https://conda.io/docs/user-guide/

  4. None
  5. None
  6. None
  7. Me • Call me TP • Follow @uranusjr • https://uranusjr.com

  8. None
  9. None
  10. None
  11. http://macdown.uranusjr.com

  12. https://docs.pipenv.org

  13. None
  14. https://xkcd.com/1987/

  15. *NOT FINE

  16. Application Dependencies Tools Interpreters

  17. None
  18. Application Dependencies

  19. $ python3 -m venv --prompt="project" .venv $ . .venv/bin/activate (project)

    $ pip install -r requirements.txt
  20. cffi==1.5.2 cryptography==1.2.2 enum34==1.1.2 Flask==0.10.1 gunicorn==19.4.5 idna==2.0 ipaddress==1.0.16 itsdangerous==0.24 Jinja2==2.8 MarkupSafe==0.23

    ndg-httpsclient==0.4.0 pyasn1==0.1.9 pycparser==2.14 pyOpenSSL==0.15.1 requests==2.9.1 six==1.10.0 Werkzeug==0.11.4 requests[security] flask gunicorn==19.4.5
  21. https://gist.github.com/datagrok/2199506

  22. None
  23. Python? Meh.

  24. https://docs.pipenv.org

  25. Pipenv • No more bin/activate • Subshell • Direct binary

    call • Declared dependencies • Locking into actual requirements
  26. None
  27. Application Dependencies Tools

  28. SUDO PIP INSTALL PIPENV

  29. I TOLD YOU TO USE VIRTUAL ENVIRONMENTS

  30. None
  31. $ mkdir -p ~/.local/bin ~/.local/venvs $ python3 -m venv ~/.local/venvs/pipenv

    $ ~/.local/venvs/pipenv/bin/pip install pipenv $ ln -s ~/.local/venvs/pipenv/bin/pipenv ~/.local/bin $ pipenv --version pipenv, version 11.10.3
  32. https://github.com/mitsuhiko/pipsi

  33. Rules of Thumb • Do not use sudo. Ever. •

    Always use virtual environments • Use --user when desperate
  34. $ python3 -m ~/.local/venvs/pipenv

  35. Application Dependencies Tools Interpreters

  36. I MEAN IT COULD BE WORSE

  37. None
  38. Linux • Ubuntu: use “deadsnakes” PPA • Compile your own

    otherwise • Discussed later • Be careful with the package manager
  39. macOS • Do not use System Python • Do not

    use Homebrew Python • Must compile your own
  40. Compile Your Python • Install some build dependencies • pyenv

    install <version> • You can use shims, but I hate them
  41. $ python-build 3.6.5 ~/.local/pythons/3.6 $ python-build 3.5.4 ~/.local/pythons/3.5 $ ln

    -s ~/.local/pythons/3.6/python3.6 ~/.local/bin $ ln -s ~/.local/pythons/3.5/python3.5 ~/.local/bin $ ln -s ~/.local/bin/python3.6 ~/.local/bin/python3
  42. $ python3.5 -V Python 3.5.4 $ python3 -V Python 3.6.5

    $ python -V nope
  43. https://github.com/uranusjr/pythonup-macos

  44. $ pythonup install 3.6 # Build 3.6. $ pythonup upgrade

    3.6 # Re-install if newer. $ pythonup use 3.6 # Link 3.6 scripts.
  45. python() { local PYTHON="$(which python)" if [[ "$PYTHON" == /usr/*

    ]]; then echo "nope" >&2 | echo >/dev/null else "$PYTHON" "$@" fi } Hide the Default Python
  46. Windows • No global Python to worry about • No

    support from pyenv • Building is drastically more difficult
  47. https://vstinner.readthedocs.io/cpython_windows.html

  48. ! Just use installers from python.org?

  49. " At least download them automatically?

  50. https://github.com/uranusjr/pythonup-windows

  51. $ pythonup install 3.6 # Download and install 3.6. $

    pythonup upgrade 3.6 # Re-install if newer. $ pythonup use 3.6 # Link 3.6 scripts.
  52. THE CIRCLE IS NOW COMPLETE

  53. Summary (1/3) • Don’t use Homebrew Python • Prefer self-compiling

    if possible • Don’t add things to PATH; link* them
  54. Summary (2/3) • Always use virtual environments • You can

    call scripts in venv directly • Link* them in PATH!
  55. Summary (3/3) • Always use virtual environments (reprise) • Avoid

    activate scripts • Pipfile and Pipenv abstracts away warts
  56. Future Work • A lot of unfinished stuff already •

    Differences when developing a package • pyproject.toml and build system declaration
  57. JOIN!