1. 程式人生 > >mac上通過brew包管理器安裝python3.6

mac上通過brew包管理器安裝python3.6

mac上通過brew包管理器安裝python3.6

緣由:

最近在學習pythonTDD(測試驅動開發時)遇到了一個問題,根據指導需要使用Django1.12版本,但是在跑服務啟動命令

python manage.py runserver

的時候,出現瞭如下的報錯:

 File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 728, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/Users/robot/.virtualenvs/superlists/lib/python3.7/site-packages/django/contrib/admin/__init__.py", line 4, in <module>
    from django.contrib.admin.filters import (
  File "/Users/robot/.virtualenvs/superlists/lib/python3.7/site-packages/django/contrib/admin/filters.py", line 10, in <module>
    from django.contrib.admin.options import IncorrectLookupParameters
  File "/Users/robot/.virtualenvs/superlists/lib/python3.7/site-packages/django/contrib/admin/options.py", line 13, in <module>
    from django.contrib.admin import helpers, widgets
  File "/Users/robot/.virtualenvs/superlists/lib/python3.7/site-packages/django/contrib/admin/widgets.py", line 152
    '%s=%s' % (k, v) for k, v in params.items(),
    ^
SyntaxError: Generator expression must be parenthesized

後來搜尋後發現了時python(3.7)和Django(1.12)版本之間的相容問題,網路上給出的解決方案是安裝python3.6。
由於之前使用brew進行包管理和軟體安裝的,所以想偷個懶。繼續使用brew安裝python3.6,通過搜尋網路獲得如下解決方案,原解決方案是:Homebrew: Rollback from Python 3.7 to Python 3.6.5.x? - Ask Different

解決方案

具體的操作步驟是:

brew unlink python
brew install https://raw.githubusercontent.com/Homebrew/homebrew-core/f2a764ef944b1080be64bd88dca9a1d80130c558/Formula/python.rb

感悟:

因為之前沒有安裝python3.6所以在使用一些help的brew switch時並不生效,
根據觀察,使用 brew switch命令時,brew回去cell目錄中去找,但是由於之前就沒安裝過,所以自然會失敗。