[英]How to correctly install python3 and virtualenv on MacOS Mojave?
我開始學習 Django 框架,所以我需要在我的 mac 上安裝最新的 python、pip、virtualenv 和 django 數據包。 我嘗試用 brew 來做,但我有一些奇怪的行為。
起初,python3 沒有安裝在 /usr/bin/ 中,而是安裝在 /Library/Frameworks/Python.framework 目錄中:
$ which python
/usr/bin/python
$ which python3
/Library/Frameworks/Python.framework/Versions/3.7/bin/python3
這對我來說很奇怪,因為每個教程都講述了 /usr/bin/python37 而沒有講述 /Library/Frameworks/Python.framework 這樣可以嗎?
之后我做了sudo pip3 install virtualenv
並得到了這個答案:
The directory '/Users/user/Library/Caches/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
The directory '/Users/user/Library/Caches/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
好的,我使用 -H sudo 標志進行了卸載和安裝:
Installing collected packages: virtualenv
Successfully installed virtualenv-16.4.3
但是當我嘗試創建一個虛擬環境時,我得到了
$ virtualenv venv
-bash: /usr/local/bin/virtualenv: No such file or directory
檢查 virtualenv 位置:
$ which virtualenv
/Library/Frameworks/Python.framework/Versions/3.7/bin/virtualenv
為什么是 /Library/Frameworks/Python.framework/? 為什么它會在 /usr/local/bin/virtualenv 中搜索 virtualenv? 在 Mac 上寫代碼總是那么痛苦?
您可以簡單地使用“venv”,而不是使用 brew。
要創建一個虛擬環境,您可以運行 -->
python3 -m venv environment_name
示例:如果你想為 django 創建一個名為 django_env 的虛擬環境
python3 -m venv django_env
“-m”標志檢查 sys.path 並執行主模塊。
虛擬環境的激活:
source django_env/bin/activate
停用:
deactivate
要求:
$ brew install python3 #upgrade
Pip3 與 Python3 一起安裝
安裝
要通過 pip 運行安裝 virtualenv:
$ pip3 install virtualenv
用法
創建 virtualenv:
$ virtualenv -p python3 <desired-path>
激活虛擬環境:
$ source <desired-path>/bin/activate
停用 virtualenv:
$ deactivate
你可以在官方頁面上看到更多關於Homebrew
的信息。
只需按照以下步驟操作:
$ pip 安裝 virtualenv 安裝后,您可以創建一個虛擬環境:
$ virtualenv [directory] 在 MacOS 上,我們使用 source 命令激活我們的虛擬環境。 如果您在 myvenv 目錄中創建了 venv,則命令為
$ 源 myvenv/bin/激活
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.