pyenv 是一套管理同時使用不同版本 Python 的工具,也可以搭配 virtualenv 使用。
首先依照 Common build problems 安裝一些套件,由於等一下也會使用到 git 所以一併安裝。
$ sudo apt-get install -y make build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev git
然後執行官方建議的安裝方式。
$ wget https://raw.githubusercontent.com/yyuu/pyenv-installer/master/bin/pyenv-installer -O - | bash
然後我不是很喜歡每次啟動 shell 就會自動載入使用 pyenv,所以我自己寫了一個 ~/bin/pyenv 的腳本程式來使用。
#!/bin/bash export PATH="$HOME/.pyenv/bin:$PATH" eval "$(pyenv init -)" eval "$(pyenv virtualenv-init -)" export PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h(python)\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ ' bash --noprofile -l
每次當我要使用 pyenv 時,就要先執行一次 pyenv,接下來我就可以看到 (python) 的提示出現,代表我正在使用 pyenv。
fourdollars@ubuntu(python):~$
接下來來找找看,有哪些 Python 版本可以安裝使用。
$ pyenv install -l Available versions: ... 2.7.10 ... 3.4.3 ...
我選擇安裝最新(寫這篇文章時)的 Python 2 的穩定發行版本。
$ pyenv install 2.7.10 Downloading Python-2.7.10.tgz... -> https://yyuu.github.io/pythons/eda8ce6eec03e74991abb5384170e7c65fcd7522e409b8e83d7e6372add0f12a Installing Python-2.7.10... patching file ./Lib/site.py Installed Python-2.7.10 to /home/fourdollars/.pyenv/versions/2.7.10
然後先切進 2.7.10 的環境,更新一下套件。
$ pyenv global 2.7.10 $ pip list --outdated | awk '{print $1}' | xargs pip install --upgrade You are using pip version 6.1.1, however version 7.1.2 is available. You should consider upgrading via the 'pip install --upgrade pip' command. You are using pip version 6.1.1, however version 7.1.2 is available. You should consider upgrading via the 'pip install --upgrade pip' command. Collecting setuptools Downloading setuptools-18.3.1-py2.py3-none-any.whl (462kB) 100% |████████████████████████████████| 462kB 666kB/s Collecting pip Using cached pip-7.1.2-py2.py3-none-any.whl Installing collected packages: setuptools, pip Found existing installation: setuptools 15.2 Uninstalling setuptools-15.2: Successfully uninstalled setuptools-15.2 Found existing installation: pip 6.1.1 Uninstalling pip-6.1.1: Successfully uninstalled pip-6.1.1 Successfully installed pip-7.1.2 setuptools-18.3.1
接下來要搭配使用 virtualenv 建立一個獨立的 Python 環境。
$ pyenv virtualenv 2.7.10 watchdog Collecting virtualenv Downloading virtualenv-13.1.2-py2.py3-none-any.whl (1.7MB) 100% |████████████████████████████████| 1.7MB 345kB/s Installing collected packages: virtualenv Successfully installed virtualenv-13.1.2 New python executable in /home/fourdollars/.pyenv/versions/watchdog/bin/python2.7 Also creating executable in /home/fourdollars/.pyenv/versions/watchdog/bin/python Installing setuptools, pip, wheel...done. Ignoring indexes: https://pypi.python.org/simple Requirement already satisfied (use --upgrade to upgrade): setuptools in /home/fourdollars/.pyenv/versions/watchdog/lib/python2.7/site-packages Requirement already satisfied (use --upgrade to upgrade): pip in /home/fourdollars/.pyenv/versions/watchdog/lib/python2.7/site-packages
接下來要建立一個 watchdog 專案目錄,並且在裡面設定使用這個獨立的 Python 環境。
$ mkdir ~/watchdog && cd ~/watchdog $ pyenv local watchdog $ pyenv local watchdog
也就是說在這個目錄底下,pyenv 會自動切進跟專案目錄名稱相同的 virtualenv 環境。
接下來總算可以安裝 Django 來開發了。
$ pip install Django Collecting Django Downloading Django-1.8.4-py2.py3-none-any.whl (6.2MB) 100% |████████████████████████████████| 6.2MB 110kB/s Installing collected packages: Django Successfully installed Django-1.8.4
安裝完 Django 後要重新登入一下,才可以使用 django-admin 的指令來建立這個新專案。
fourdollars@ubuntu(python):~/watchdog$ django-admin startproject watchdog .
然後就是 Django 的初始化跟啟動開發用的伺服器。
$ python manage.py migrate Operations to perform: Synchronize unmigrated apps: staticfiles, messages Apply all migrations: admin, contenttypes, auth, sessions Synchronizing apps without migrations: Creating tables... Running deferred SQL... Installing custom SQL... Running migrations: Rendering model states... DONE Applying contenttypes.0001_initial... OK Applying auth.0001_initial... OK Applying admin.0001_initial... OK Applying contenttypes.0002_remove_content_type_name... OK Applying auth.0002_alter_permission_name_max_length... OK Applying auth.0003_alter_user_email_max_length... OK Applying auth.0004_alter_user_username_opts... OK Applying auth.0005_alter_user_last_login_null... OK Applying auth.0006_require_contenttypes_0002... OK Applying sessions.0001_initial... OK fourdollars@ubuntu(python):~/watchdog$ python manage.py runserver Performing system checks... System check identified no issues (0 silenced). September 08, 2015 - 09:50:59 Django version 1.8.4, using settings 'watchdog.settings' Starting development server at http://127.0.0.1:8000/ Quit the server with CONTROL-C.
沒有留言:
張貼留言