因為想寫一些Python程序,所以今天在家中的Windows 10安裝Python。也因為習慣及相容性考慮,目前首選使用Linux,再利用pyenv 做Version管理及Python虛疑環境。
安裝Ubuntu (on Windows 10)
除了Windows 10自帶的Linux子系統WSL(也是Ubuntu),Canonical Group Limited也在MS Store 發佈了Ubuntu。目前我是選擇MS Store 的Ubuntu。只要到MS Store找出Ubuntu並安裝即可。詳細安裝可參考 WSL-Windows 10 Installation Guide
在Linux子系統上安裝Python
安裝好Linux子系統後,就算不帶Python的,也可直接安裝pyenv再安裝Python [不確定,待驗証],但我發現某些情況也要在系統上安裝同版本的Python。其中例子是npyscreen所依賴的curses/ncurses,因為WSL下的Python3並未帶curses,所以就算成功安裝npyscreen可會報錯,不能使用。經多次試驗發現如想在pyenv下的Python能正常使用curses/ncurses就必需在Linux子系統上也安裝Python+ncurses。
安裝pyenv
以住用VirtualEnv來做Python Version管理,但用過pyenv之後就愛上她。Ubuntu 本自帶Python3,但在pyenv環境也可另行安裝Python3。
首先,打開「Windows上Ubuntu的Bash」,輸入以下指令安裝pyenv所需的軟件套裝:
# Required libraries $ sudo apt-get install git gcc make openssl libssl-dev libbz2-dev libreadline-dev libsqlite3-dev zlib1g-dev # For convenience, install the development tools $ sudo apt-get install build-essential
利用pyenv-installer安裝pyenv及pyenv-virtualenv:
$ curl -L https://github.com/pyenv/pyenv-installer/raw/master/bin/pyenv-installer | bash $ pyenv update
修改 ~/.bashrc ,把以下的pyenv所需的指令加到最後:
# Update ~/.bashrc and add pyenv required commands: $ vi ~/.bashrc
export PATH="~/.pyenv/bin:$PATH" eval "$(pyenv init -)" eval "$(pyenv virtualenv-init -)"
# Restart shell $ exec $SHELL
安裝Python:
# Show available versions: $ pyenv install --list # Install 3.6.7 $ pyenv install 3.6.7
(注意,當前Python3 最新版為3.7.1,但經pyenv安裝時報錯,所以轉用3.6.7)
Downloading Python-3.7.1.tar.xz... -> https://www.python.org/ftp/python/3.7.1/Python-3.7.1.tar.xz Installing Python-3.7.1... BUILD FAILED (Ubuntu 18.04 using python-build 1.2.8) Inspect or clean up the working tree at /tmp/python-build.20181030235256.31820 Results logged to /tmp/python-build.20181030235256.31820.log Last 10 log lines: File "/tmp/tmpgw81w9h4/pip-10.0.1-py2.py3-none-any.whl/pip/_internal/__init__.py", line 42, in <module> File "/tmp/tmpgw81w9h4/pip-10.0.1-py2.py3-none-any.whl/pip/_internal/cmdoptions.py", line 16, in <module> File "/tmp/tmpgw81w9h4/pip-10.0.1-py2.py3-none-any.whl/pip/_internal/index.py", line 25, in <module> File "/tmp/tmpgw81w9h4/pip-10.0.1-py2.py3-none-any.whl/pip/_internal/download.py", line 39, in <module> File "/tmp/tmpgw81w9h4/pip-10.0.1-py2.py3-none-any.whl/pip/_internal/utils/glibc.py", line 3, in <module> File "/tmp/python-build.20181030235256.31820/Python-3.7.1/Lib/ctypes/__init__.py", line 7, in <module> from _ctypes import Union, Structure, Array ModuleNotFoundError: No module named '_ctypes' Makefile:1122: recipe for target 'install' failed make: *** [install] Error 1
Create first virtualenv for practice
$ pyenv virtualenv 3.6.7 my-exec-001 # Show current installed versions $ pyenv versions 3.6.7 3.6.7/envs/my-exec-001 my-exec-001 # Enter to the shell: $ pyenv shell my-exec-001 # Also update the pip $ pip install --upgrade pip