pipenv是個python中不錯的管理套件,可以快速的建立python虛擬環境,也可以在建立的同時選擇好python版本,但如果有更多的版本要選擇,像是3.7、3.8版,這樣要如何建立呢?
這時候python的安裝路徑就很重要
測試運作環境:
- OS:Mac OS Catalina 10.15.5
- iTerm:3.3.6
- Python: 3.7.4、3.8.1、3.9
前言:
從git server上面把專案抓下來後,想要安裝在自己的電腦,並且想要執行這個專案。在開心地執行pipenv --three
後,它跳出一個訊息:這是3.9版的套件,而你的是3.7.4,需要我換版,這時候就想,要如何更換版本呢?
Warning: Your Pipfile requires python_version 3.9, but you are using 3.7.4 (/目錄/.venv/bin/python).
$ pipenv --rm and rebuilding the virtual environment may resolve the issue.
$ pipenv check will surely fail.
步驟:
分成兩個步驟:安裝Python、執行 pipenv --python
{python_path}。(此會以mac為說明,其他作業系統則是看路徑)
步驟一:用homebrew
安裝不同版本的python
檢查一下安裝的項目:
ls -l /usr/local/opt/
看到底下有兩個版本的python,一個是3,一個是3.8,表示我們已經安裝好了。如果沒有安裝的話,可以使用brew install python
來安裝python。
進去資料夾裡面的 python3.8
資料會是這樣,要看到 python3.8
,這就是我們python3.8
版的執行環境:
步驟二:使用pipenv
建立虛擬環境
如果直接執行pipenv --three
,會執行成python3 (3.7.4)
,這是我的 Mac OS
中Python3
的預設運作版本。
$ pipenv --three
Creating a virtualenv for this project…
Pipfile: /Users/tonywu/Documents/Work/one account/business-base/Pipfile
Using /usr/local/bin/python3 (3.7.4) to create virtualenv…
⠏ Creating virtual environment...Already using interpreter /usr/local/opt/python/bin/python3.7
Using base prefix '/usr/local/Cellar/python/3.7.4_1/Frameworks/Python.framework/Versions/3.7'
New python executable in /現在執行目錄/.venv/bin/python3.7
Also creating executable in /現在執行目錄/.venv/bin/python
Installing setuptools, pip, wheel...
done.
Running virtualenv with interpreter /usr/local/bin/python3
✔ Successfully created virtual environment!
Virtualenv location: /現在執行目錄/.venv
如果執行pipenv --python 3.8 呢? 它會找不到
$ pipenv --python 3.8
Warning: Python 3.8 was not found on your system…
You can specify specific versions of Python with:
$ pipenv --python path/to/python
看到上面的錯誤訊息知道,我們要提供 python3.8
的路徑給 pipenv
,因此要輸入:
pipenv --python /usr/local/opt/python@3.8/bin/python3
這樣就會安裝成功囉~
$ pipenv --python /usr/local/opt/python@3.8/bin/python3
Creating a virtualenv for this project…
Pipfile: /Users/tonywu/Documents/Work/one account/business-base/Pipfile
Using /usr/local/opt/python@3.8/bin/python3 (3.8.1) to create virtualenv…
⠴ Creating virtual environment...Already using interpreter /usr/local/opt/python@3.8/bin/python3.8
Using base prefix '/usr/local/Cellar/python@3.8/3.8.1/Frameworks/Python.framework/Versions/3.8'
New python executable in /現在執行目錄/.venv/bin/python3.8
Also creating executable in /現在執行目錄/.venv/bin/python
Installing setuptools, pip, wheel...
done.
Running virtualenv with interpreter /usr/local/opt/python@3.8/bin/python3
✔ Successfully created virtual environment!
Virtualenv location: /現在執行目錄/.venv
雖然還是跳出警示(Warning: Your Pipfile requires python_version 3.9, but you are using 3.8.1
),但是只要安裝好3.9版,就不會有問題了!
~Copyright by Eyelash500~
IT技術文章:EY*研究院
iT邦幫忙:eyelash*睫毛
Blog:睫毛*Relax
Facebook:睫毛*Relax