手札笔记- condon和python+jupyter环境搭建

一个数据分析师或者算法分析师在进入一个新的部门或者接手一台新的机器时,往往需要为后续工作展开进行一定的环境搭建工作,下面主要梳理了在linux服务器上搭建工作环境和部署python编辑服务,以及一些服务器操作常用命令。

一、conda环境构建

为了隔离个人服务和其他环境冲突,通常需要构建单独的虚拟环境来进行测试和服务挂载,因此通过conda来构建虚拟环境就显得尤为重要,在此罗列一下整个conda的安装流程。

  1. 若系统未安装wget,需先执行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查看系统版本信息
  • 2、 chmod +x Anaconda3-2025.06-1-Linux-x86_64.sh
  • 3、./Anaconda3-2025.06-1-Linux-x86_64.sh如果最后选择yes,就不需要2.4的更新环境配置内容
  • 4、 更新环境配置:
    **echo 'export PATH="$HOME/anaconda3/bin:$PATH"' >> ~/.bashrc
    ** source ~/.bashrc #激活环境变量,有些情况情况下需要每次都触发激活变量环境
  • 5、 验证conda安装完成 conda —version

二、创建python虚拟环境

conda create -n jupyter_env python=3.11

修改python包镜像源,考虑到默认python包安装源速度过慢,有时候需要替换为国内的一些镜像源

  • 创建或编辑配置文件:mkdir -p ~/.pip && nano ~/.pip/pip.conf
  • 写入以下内容
    (以清华源为例):[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包环境:

  • 读取环境文件安装:pip install -r requirements.txt
  • 对当前环境进行记录:pip freeze > requirements.txt

三、 python工作IDE-jupyter

  1. 激活虚拟环境:
conda activate jupyter_env 
  1. 虚拟环境下安装jupyter交互界面
conda install jupyter
  1. jupyter的配置与启动
  • 生成配置文件:jupyter notebook --generate-config
  • 设置密码:通过Python执行from jupyter_server.auth import passwd print(passwd())
  • 修改配置文件(如~/.jupyter/jupyter_notebook_config.py),指定IP、端口、工作目录等参数
    **jupyter服务器配置 vim ~/.jupyter/jupyter_notebook_config.py
c.NotebookApp.allow_remote_access = True
c.NotebookApp.allow_root = True #允许root账户启动服务
c.NotebookApp.notebook_dir = '/home/workspace/jpy’ #设置jupyter的主目录
c.NotebookApp.open_browser = False #是否浏览器自动打开

c.NotebookApp.ip = ’0.0.0.0' #地址ip设置
c.NotebookApp.password_required = True #密码是否需要
c.NotebookApp.port = 8888 #端口,默认8888
  • Jupyter提供了专门的密码设置命令,可以避免手动配置错误
jupyter notebook password
  • 启动服务:nohup jupyter notebook —allow-root & 可后台挂载运行
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容