手札筆記- condon和python+jupyter環(huán)境搭建

一個(gè)數(shù)據(jù)分析師或者算法分析師在進(jìn)入一個(gè)新的部門或者接手一臺(tái)新的機(jī)器時(shí),往往需要為后續(xù)工作展開進(jìn)行一定的環(huán)境搭建工作,下面主要梳理了在linux服務(wù)器上搭建工作環(huán)境和部署python編輯服務(wù),以及一些服務(wù)器操作常用命令。

一、conda環(huán)境構(gòu)建

為了隔離個(gè)人服務(wù)和其他環(huán)境沖突,通常需要構(gòu)建單獨(dú)的虛擬環(huán)境來進(jìn)行測(cè)試和服務(wù)掛載,因此通過conda來構(gòu)建虛擬環(huán)境就顯得尤為重要,在此羅列一下整個(gè)conda的安裝流程。

  1. 若系統(tǒng)未安裝wget,需先執(zhí)行yum install wget -y(CentOS)或apt-get install -y wget(Ubuntu/Debian)
  2. 安裝conda:
  • 1、 wget https://repo.anaconda.com/archive/Anaconda3-2025.06-1-Linux-x86_64.sh,需要uname -a查看系統(tǒng)版本信息
  • 2、 chmod +x Anaconda3-2025.06-1-Linux-x86_64.sh
  • 3、./Anaconda3-2025.06-1-Linux-x86_64.sh如果最后選擇yes,就不需要2.4的更新環(huán)境配置內(nèi)容
  • 4、 更新環(huán)境配置:
    **echo 'export PATH="$HOME/anaconda3/bin:$PATH"' >> ~/.bashrc
    ** source ~/.bashrc #激活環(huán)境變量,有些情況情況下需要每次都觸發(fā)激活變量環(huán)境
  • 5、 驗(yàn)證conda安裝完成 conda —version

二、創(chuàng)建python虛擬環(huán)境

conda create -n jupyter_env python=3.11

修改python包鏡像源,考慮到默認(rèn)python包安裝源速度過慢,有時(shí)候需要替換為國內(nèi)的一些鏡像源

  • 創(chuàng)建或編輯配置文件:mkdir -p ~/.pip && nano ~/.pip/pip.conf
  • 寫入以下內(nèi)容
    (以清華源為例):[global] index-url = https://pypi.tuna.tsinghua.edu.cn/simple trusted-host = pypi.tuna.tsinghua.edu.cn
    阿里云源: [global] index-url = http://mirrors.aliyun.com/pypi/simple trusted-host = mirrors.aliyun.com

安裝python包環(huán)境:

  • 讀取環(huán)境文件安裝:pip install -r requirements.txt
  • 對(duì)當(dāng)前環(huán)境進(jìn)行記錄:pip freeze > requirements.txt

三、 python工作IDE-jupyter

  1. 激活虛擬環(huán)境:
conda activate jupyter_env 
  1. 虛擬環(huán)境下安裝jupyter交互界面
conda install jupyter
  1. jupyter的配置與啟動(dòng)
  • 生成配置文件:jupyter notebook --generate-config
  • 設(shè)置密碼:通過Python執(zhí)行from jupyter_server.auth import passwd print(passwd())
  • 修改配置文件(如~/.jupyter/jupyter_notebook_config.py),指定IP、端口、工作目錄等參數(shù)
    **jupyter服務(wù)器配置 vim ~/.jupyter/jupyter_notebook_config.py
c.NotebookApp.allow_remote_access = True
c.NotebookApp.allow_root = True #允許root賬戶啟動(dòng)服務(wù)
c.NotebookApp.notebook_dir = '/home/workspace/jpy’ #設(shè)置jupyter的主目錄
c.NotebookApp.open_browser = False #是否瀏覽器自動(dòng)打開

c.NotebookApp.ip = ’0.0.0.0' #地址ip設(shè)置
c.NotebookApp.password_required = True #密碼是否需要
c.NotebookApp.port = 8888 #端口,默認(rèn)8888
  • Jupyter提供了專門的密碼設(shè)置命令,可以避免手動(dòng)配置錯(cuò)誤
jupyter notebook password
  • 啟動(dòng)服務(wù):nohup jupyter notebook —allow-root & 可后臺(tái)掛載運(yùn)行
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容