1. Python 安裝
Mac上較好的做法是基于Homebrew來安裝管理我們的應(yīng)用
- 可以提前搜索指定的包是否存在
brew search python@3.9
- 安裝
brew install python@3.9
2. pip
pip 是 Python 包管理工具雏掠,提供了對Python 包的查找、下載佳鳖、安裝亿胸、卸載的功能蒿赢。
- 注意:Python 2.7.9 + 或 Python 3.4+ 以上版本都自帶 pip 工具。
在采用默認(rèn) pip3 安裝第三方庫的時候,經(jīng)常會出現(xiàn)超時的情況等浊。
pip._vendor.urllib3.exceptions.ReadTimeoutError: HTTPSConnectionPool(host='files.pythonhosted.org', port=443): Read timed out.
這時候就需要使用國內(nèi)的鏡像源了欲诺,常見的pip國內(nèi)鏡像源如下抄谐, 可以任意選擇一個
- 阿里云:https://mirrors.aliyun.com/pypi/simple/
- 清華:https://pypi.tuna.tsinghua.edu.cn/simple
- 中國科技大學(xué): https://pypi.mirrors.ustc.edu.cn/simple/
- 豆瓣:http://pypi.douban.com/simple/
創(chuàng)建配置文件
創(chuàng)建配置文件 ~/.pip/pip.conf
mkdir -p ~/.pip
cat > ~/.pip/pip.conf<<eof
[global]
timeout = 6000
index-url = https://mirrors.aliyun.com/pypi/simple/
trusted-host = mirrors.aliyun.com
eof
執(zhí)行安裝時,如果有如下提示扰法,則說明鏡像源已被替換
Looking in indexes: https://mirrors.aliyun.com/pypi/simple/
3. 安裝虛擬環(huán)境
1.為什么要安裝虛擬機(jī)?
單獨(dú)的虛擬環(huán)境可以讓每一個Python項(xiàng)目單獨(dú)使用一個環(huán)境蛹含,而不會影響Python系統(tǒng)環(huán)境,也不會影響其他項(xiàng)目的環(huán)境塞颁。示意圖如下
2. virtualenv
virtualenv是官方推薦的浦箱,隔離第三個庫的依賴關(guān)系,安裝方式如下:
pip3 install virtualenv
查看版本
virtualenv -V
3. 安裝virtualenvwrapper
virtualenvwrapper是virtualenv的擴(kuò)展包祠锣,可以更方便的新增酷窥、刪除、復(fù)制伴网、切換虛擬環(huán)境蓬推,并將所有虛擬環(huán)境整合在一個目錄下。
安裝
pip3 install virtualenvwrapper
配置
# 01 建目錄用來存放虛擬環(huán)境
mkdir ~/.virtualenvs
# 02 在.bashrc/.zshrc 中添加澡腾, 取決于使用的shell
export WORKON_HOME=~/.virtualenvs
source /usr/local/bin/virtualenvwrapper.sh
# 03 讓配置生效
source ~/.zshrc
常用的命令
- workon:列出虛擬環(huán)境列表
- lsvirtualenv:同上
- mkvirtualenv :新建虛擬環(huán)境
- workon [虛擬環(huán)境名稱]:切換虛擬環(huán)境
- rmvirtualenv :刪除虛擬環(huán)境
- deactivate: 離開虛擬環(huán)境
# 01 創(chuàng)建虛擬環(huán)境tf
mkvirtualenv tf
# 02 查看當(dāng)前的虛擬環(huán)境
lsvirtualenv
# 03 使用tf 虛擬環(huán)境
workon tf
# 04 退出虛擬環(huán)境
deactivate
4. Jupyter Lab
Jupyter Lab是Jupyter Notebook的全面升級(Jupyter’s Next-Generation Notebook Interface)沸伏。
Jupyter Lab是最新的基于網(wǎng)頁的交互式開發(fā)環(huán)境糕珊,用于文檔編寫、開發(fā)和運(yùn)行代碼毅糟、展示結(jié)果红选。靈活的接口允許用戶配置和編排工作流程,廣泛適用于數(shù)據(jù)分析姆另、科學(xué)計(jì)算和機(jī)器學(xué)習(xí)中喇肋。模塊化設(shè)計(jì),可以在同一個窗口以標(biāo)簽的形式同時打開好幾個文檔迹辐,同時插件管理非常強(qiáng)大苟蹈。
- 相對的,Pycharm也是一款非常優(yōu)秀的python IDE工具右核,但是更適合開發(fā)復(fù)雜慧脱、大型的項(xiàng)目,例如web后端項(xiàng)目贺喝。
4.1 安裝
# 01 安裝jupyterlab
pip3 install jupyterlab
# 02 啟動jupyterlab菱鸥,會自動彈出web界面,如果沒有彈出可以訪問http://localhost:8889/lab
jupyter lab
4.2 配合virtualenv使用
# 01 列出所有存在的kernal
# jupyter kernelspec list
Available kernels:
python3 /usr/local/share/jupyter/kernels/python3
# 02 創(chuàng)建虛擬環(huán)境tf
mkvirtualenv tf
# 03 使用tf虛擬環(huán)境
# workon tf
# 04 在tf虛擬環(huán)境中安裝ipykernel
pip3 install ipykernel
# 05 將虛擬換進(jìn)tf加入IPykernel裡(需要在tf生效情況下)
python3 -m ipykernel install --user --name=tf
# 06 重新打開jupyter lab 可以發(fā)現(xiàn)tf類型的Notebook
jupyter lab
# 07 如果想卸載某個虛擬環(huán)境的kernel
jupyter kernelspec uninstall your-virtualenv
4.3 插件安裝
1. 安裝自動格式化插件
# 01 安裝jupyterlab_code_formatter
pip3 install jupyterlab_code_formatter
# 02 安裝python代碼格式化包躏鱼,這里使用autopep8
pip install autopep8
安裝好后重啟jupyter lab氮采, 點(diǎn)擊Jupyter Lab左側(cè)欄插件圖標(biāo),在INSTALLED列表下染苛,可以觀察到j(luò)upyterlab_code_formatter已安裝鹊漠。(務(wù)必啟用插件功能)
配置插件方法如下:依次點(diǎn)擊Settings > Advanced Settings Editor > →Jupyterlab Code Formatter, 點(diǎn)中右上角的JSON Settings Editor茶行。
在右側(cè)的User Preference中輸入自定義配置并保存即可覆蓋默認(rèn)配置躯概。
{
"preferences": {
"default_formatter": {
"python": "autopep8",
"R": "styler"
}
}
}
配置好之后,可以通過如下方式格式化代碼:
- Edit > Apply Autopep8 Formatter
- 右鍵 > Format cell
- 點(diǎn)擊Format notebook按鈕