论坛
潜水/灌水快乐,沉淀知识,认识更多同行。
ToB圈子
加入IT圈,遇到更多同好之人。
朋友圈
看朋友圈动态,了解ToB世界。
ToB门户
了解全球最新的ToB事件
博客
Blog
排行榜
Ranklist
文库
业界最专业的IT文库,上传资料也可以赚钱
下载
分享
Share
导读
Guide
相册
Album
记录
Doing
应用中心
搜索
本版
文章
帖子
ToB圈子
用户
免费入驻
产品入驻
解决方案入驻
公司入驻
案例入驻
登录
·
注册
只需一步,快速开始
账号登录
立即注册
找回密码
用户名
Email
自动登录
找回密码
密码
登录
立即注册
首页
找靠谱产品
找解决方案
找靠谱公司
找案例
找对的人
专家智库
悬赏任务
圈子
SAAS
IT评测·应用市场-qidao123.com技术社区
»
论坛
›
数据库
›
SQL-Server
›
Python 虚拟环境安装使用(Anaconda 完备实操版) ...
Python 虚拟环境安装使用(Anaconda 完备实操版)
风雨同行
论坛元老
|
2024-9-22 16:53:25
|
显示全部楼层
|
阅读模式
楼主
主题
1594
|
帖子
1594
|
积分
4782
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要
登录
才可以下载或查看,没有账号?
立即注册
x
1. 安装
安装 anaconda(包罗 python 和 pip 等,支持创建及管理多个 python 虚拟环境)
注:miniconda 大概也可以,但是没用过,优先 anaconda
1.1 linux
1.1.1 ubuntu
Mac、Windows 及其他 Linux 体系类似
注:一样寻常不使用 root 用户,使用其他非 root 用户(方便使用 homebrew 等)
Anaconda3-2024.06-1-Linux-x86_64(example)
# 下载安装包
# 最新版官网: https://www.anaconda.com/download/success
# 清华源下载:https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/?C=M&O=A
# 如果官方下载速度不给力,可以从清华源下载(另外记得做好安装包的管理/归档,或者安装完成之后及时删除)
wget https://repo.anaconda.com/archive/Anaconda3-2024.06-1-Linux-x86_64.sh
# 安装
bash Anaconda3-2024.06-1-Linux-x86_64.sh
# 注: 最后有一个是否 conda init,优先输入`yes`,这样后面 conda 的使用更方便(开始安装后不要回车,不然就默认`no`了)
# 更新系统环境变量
# ~/.bashrc 在不同系统下,可能在 ~/.zshrc、~/.profile、~/.bash_profile、~/.bash_login、~/.profile 等文件中
source ~/.bashrc
# 确认是否安装成功,打印"conda xx.x.x"就成功了(或者看前面是否出现了"(base)",没有的话重启/新开终端)
conda -V
复制代码
# 手动 conda init(如果安装的时候没有 conda init,可以手动 conda init)
conda init
source ~/.bashrc
复制代码
2. 使用
配置 conda 和 pip 的国内镜像源后,通过 conda 来管理 python 虚拟环境,通过 pip 来安装第三方 python 库(也可以通过 conda 来安装)
注:python 虚拟环境的管理也可以通过 virtualenvwrapper 等其他工具
2.1 set mirror
2.1.1 conda
set
# Windows下执行(其他系统跳过)
conda config --set show_channel_urls yes
# 新建/更新conda配置文件
vim ~/.condarc
# 内容如下
复制代码
channels:
- defaults
show_channel_urls: true
default_channels:
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/msys2
custom_channels:
conda-forge: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
msys2: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
bioconda: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
menpo: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
pytorch: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
pytorch-lts: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
simpleitk: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
复制代码
2.1.2 pip
list
# pip源列表,此处只是记录人工整理镜像源,不涉及任何操作(下面配置镜像源的时候,从这里手动拷贝一个/多个过去)
官方:https://pypi.org/simple
清华:https://pypi.tuna.tsinghua.edu.cn/simple
百度:https://mirror.baidu.com/pypi/simple/
阿里:https://mirrors.aliyun.com/pypi/simple/
豆瓣:https://pypi.douban.com/simple/
中科大:https://pypi.mirrors.ustc.edu.cn/simple/
...
复制代码
set
# 临时使用
# schema
pip install [package] -i [url]
# example
pip install numpy -i https://pypi.tuna.tsinghua.edu.cn/simple
# 长期设置(推荐)
# schema
pip config set global.index-url [url]
# example
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
注:也可以通过 pip config set global.extra-index-url "<url1> <url2> ..." 配置多个镜像源
复制代码
2.2 create env
# 创建虚拟环境
# schema
conda create -n env_name python=xxx
# example
conda create -n test python=3.10
复制代码
2.3 activate env
# 激活虚拟环境
# schema
conda activate env_name
# example
conda activate test
复制代码
2.4 install package
实行完这一步,基本python环境已经搭建好了
# 通过pip安装第三方python库
# 直接安装指定包(一个/多个)
# schema
pip install xxx1 xxx2
# example
pip install numpy pandas
# 通过requirements.txt安装多个包
pip install -r requirements.txt
复制代码
2.5 remove package
这里开始,按需使用
# 删除某个第三方python库(应该同理可以批量删除)
# schema
pip uninstall xxx
# example
pip uninstall numpy
复制代码
2.6 freeze package
# 生成当前python环境的requirements.txt(一般手动维护requirements.txt)
pip freeze > requirements.txt
复制代码
2.7 list env
# 查看当前所有虚拟环境
conda env list
复制代码
2.8 remove env
# 删除错误/弃用的虚拟环境
# schema
conda remove -n env_name --all
# example
conda remove -n test --all
复制代码
2.9 deactivate env
# 退出虚拟环境(回到base环境)
conda deactivate
# 注:root用户在切换到其他用户前,先退出虚拟环境,不然可能会影响其他用户的conda环境的激活
复制代码
3. 资源
3.1 anaconda
download
https://www.anaconda.com/download/success
docs
https://docs.anaconda.com/
3.2 miniconda
官网
https://docs.anaconda.com/miniconda/
3.3 pypi
官网
https://pypi.org/
3.4 mirrors
3.4.1 tsinghua
3.4.1.1 anaconda
download
https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/?C=M&O=A
官网
https://mirrors.tuna.tsinghua.edu.cn/help/anaconda/
3.4.1.2 pypi
官网
https://mirrors.tuna.tsinghua.edu.cn/help/pypi/
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复
使用道具
举报
0 个回复
正序浏览
返回列表
快速回复
高级模式
B
Color
Image
Link
Quote
Code
Smilies
您需要登录后才可以回帖
登录
or
立即注册
本版积分规则
发表回复
回帖并转播
发新帖
回复
风雨同行
论坛元老
这个人很懒什么都没写!
楼主热帖
深入Python网络编程:从基础到实践 ...
MySQL锁(乐观锁、悲观锁、多粒度锁) ...
中职网络安全技能大赛SSH弱口令渗透测 ...
阿里云体验有奖:如何将 PolarDB-X 与 ...
四、MySQL之数据查询语言(二) ...
HTML+CSS+JS——动漫风二次元论坛(2页) ...
超融合和传统 “VMware + FC SAN& ...
转载自ChatGPT:Python关键字 asynico ...
01.初识Python
损失函数-pytorch
标签云
集成商
AI
运维
CIO
存储
服务器
浏览过的版块
网络安全
数据仓库与分析
SAP
登录参与点评抽奖加入IT实名职场社区
下次自动登录
忘记密码?点此找回!
登陆
新用户注册
用其它账号登录:
关闭
快速回复
返回顶部
返回列表