Upgrade to Pro — share decks privately, control downloads, hide ads and more …

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

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

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

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

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

Tzu-ping Chung

June 01, 2018
Tweet

More Decks by Tzu-ping Chung

Other Decks in Programming

Transcript

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

    View full-size slide

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

    View full-size slide

  3. Me
    • Call me TP
    • Follow @uranusjr
    • https://uranusjr.com

    View full-size slide

  4. http://macdown.uranusjr.com

    View full-size slide

  5. https://docs.pipenv.org

    View full-size slide

  6. https://xkcd.com/1987/

    View full-size slide

  7. Application
    Dependencies
    Tools
    Interpreters

    View full-size slide

  8. Application
    Dependencies

    View full-size slide

  9. $ python3 -m venv --prompt="project" .venv
    $ . .venv/bin/activate
    (project) $ pip install -r requirements.txt

    View full-size slide

  10. 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

    View full-size slide

  11. https://gist.github.com/datagrok/2199506

    View full-size slide

  12. Python? Meh.

    View full-size slide

  13. https://docs.pipenv.org

    View full-size slide

  14. Pipenv
    • No more bin/activate
    • Subshell
    • Direct binary call
    • Declared dependencies
    • Locking into actual requirements

    View full-size slide

  15. Application
    Dependencies
    Tools

    View full-size slide

  16. SUDO PIP INSTALL PIPENV

    View full-size slide

  17. I TOLD YOU TO USE
    VIRTUAL ENVIRONMENTS

    View full-size slide

  18. $ 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

    View full-size slide

  19. https://github.com/mitsuhiko/pipsi

    View full-size slide

  20. Rules of Thumb
    • Do not use sudo. Ever.
    • Always use virtual environments
    • Use --user when desperate

    View full-size slide

  21. $ python3 -m ~/.local/venvs/pipenv

    View full-size slide

  22. Application
    Dependencies
    Tools
    Interpreters

    View full-size slide

  23. I MEAN
    IT COULD BE WORSE

    View full-size slide

  24. Linux
    • Ubuntu: use “deadsnakes” PPA
    • Compile your own otherwise
    • Discussed later
    • Be careful with the package manager

    View full-size slide

  25. macOS
    • Do not use System Python
    • Do not use Homebrew Python
    • Must compile your own

    View full-size slide

  26. Compile Your Python
    • Install some build dependencies
    • pyenv install
    • You can use shims, but I hate them

    View full-size slide

  27. $ 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

    View full-size slide

  28. $ python3.5 -V
    Python 3.5.4
    $ python3 -V
    Python 3.6.5
    $ python -V
    nope

    View full-size slide

  29. https://github.com/uranusjr/pythonup-macos

    View full-size slide

  30. $ pythonup install 3.6 # Build 3.6.
    $ pythonup upgrade 3.6 # Re-install if newer.
    $ pythonup use 3.6 # Link 3.6 scripts.

    View full-size slide

  31. python() {
    local PYTHON="$(which python)"
    if [[ "$PYTHON" == /usr/* ]]; then
    echo "nope" >&2 | echo >/dev/null
    else
    "$PYTHON" "$@"
    fi
    }
    Hide the Default Python

    View full-size slide

  32. Windows
    • No global Python to worry about
    • No support from pyenv
    • Building is drastically more difficult

    View full-size slide

  33. https://vstinner.readthedocs.io/cpython_windows.html

    View full-size slide

  34. ! Just use installers from python.org?

    View full-size slide

  35. " At least download them automatically?

    View full-size slide

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

    View full-size slide

  37. $ 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.

    View full-size slide

  38. THE CIRCLE
    IS NOW COMPLETE

    View full-size slide

  39. Summary (1/3)
    • Don’t use Homebrew Python
    • Prefer self-compiling if possible
    • Don’t add things to PATH; link* them

    View full-size slide

  40. Summary (2/3)
    • Always use virtual environments
    • You can call scripts in venv directly
    • Link* them in PATH!

    View full-size slide

  41. Summary (3/3)
    • Always use virtual environments (reprise)
    • Avoid activate scripts
    • Pipfile and Pipenv abstracts away warts

    View full-size slide

  42. Future Work
    • A lot of unfinished stuff already
    • Differences when developing a package
    • pyproject.toml and build system declaration

    View full-size slide