Skip to content

Commit

Permalink
wsl2+python+conda+jupyterlab:配置记录
Browse files Browse the repository at this point in the history
  • Loading branch information
ixxmu committed Jan 17, 2025
1 parent 1cf57dd commit c595fb2
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions docs/2025-01/wsl2_python_conda_jupyterlab_配置记录.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
title: "wsl2+python+conda+jupyterlab:配置记录"
date: 2025-01-17T02:12:39Z
draft: ["false"]
tags: [
"fetched",
"医学和生信笔记"
]
categories: ["Acdemic"]
---
wsl2+python+conda+jupyterlab:配置记录 by 医学和生信笔记
------
<div><section><span>关注公众号,发送</span><strong>R语言</strong><span>或</span><strong>python</strong><span>,可获取资料</span><span></span></section><section><mp-common-profile data-pluginname="mpprofile" data-id="MzUzOTQzNzU0NA==" data-headimg="http://mmbiz.qpic.cn/mmbiz_png/tpAC6lR84R9YDc8IDhqWAHTrZsMuhDpFlw4scqOl1ZVWpeY77cdibaSzPeGALfkEhdVpwHzVibHCRSYZg4csB43g/0?wx_fmt=png" data-nickname="医学和生信笔记" data-alias="yxhsxbj" data-signature="外科医生👨‍⚕️的R语言和生信学习🔖" data-from="2" data-weuitheme="light"></mp-common-profile></section><section data-role="outer" label="edit by 135editor"><section data-role="paragraph"><section data-role="outer" label="edit by 135editor"><section data-role="paragraph"><section data-role="outer" label="edit by 135editor"><section data-role="paragraph"><section data-role="outer"><section data-role="outer" label="edit by 135editor"><section data-tools="135编辑器" data-id="28"><p data-brushtype="text" hm_fix="440:185"><span>💡专注R语言在🩺生物医学中的使用</span></p></section></section></section></section></section><hr><section data-tool="mdnice编辑器" data-website="https://www.mdnice.com"><p data-tool="mdnice编辑器"><span><span>设为“</span><strong><span>星标</span></strong><span>”,精彩不错过</span></span><br></p><hr><p data-tool="mdnice编辑器"><span><strong></strong></span></p><section data-tool="mdnice编辑器" data-website="https://www.mdnice.com"><p data-tool="mdnice编辑器">最近要学习Python处理单细胞,跟着<code>scanpy</code>的tutorial跑流程,发现这句代码一直报错:<code>sc.tl.leiden(adata, flavor="igraph", n_iterations=2)</code>,网上搜了一下发现是在Windows中的一个小bug,详情见:sc.tl.leiden with <code>flavor='igraph'</code> raises an (ignored by igraph) “ValueError: high is out of bounds for int32” · Issue #3028 · scverse/scanpy。</p><p data-tool="mdnice编辑器">所以我安装了wsl,试试在wsl中能不能跑通,简单记录下wsl+python+conda+jupyterlab的安装。</p><p data-tool="mdnice编辑器"><br></p><ul><li><p>安装wsl</p></li><ul><li><p>开启功能</p></li><li><p>安装内核更新包</p></li><li><p>安装Ubuntu</p></li></ul><li><p>修改apt镜像</p></li><li><p>修改pip镜像</p></li><li><p>安装conda</p></li><li><p>安装jupyterlab</p></li></ul><p data-tool="mdnice编辑器"><br></p><h2 data-tool="mdnice编辑器"><span>unset</span><span></span><span><span>unset</span>安装wsl<span>unset</span></span><span></span><span>unset</span></h2><h3 data-tool="mdnice编辑器"><span></span><span>开启功能</span><span></span></h3><p data-tool="mdnice编辑器">我的系统版本是Windows 11专业版 23H2:</p><p><img data-galleryid="" data-imgfileid="100020819" data-ratio="0.49480968858131485" data-s="300,640" data-type="png" data-w="578" data-src="https://mmbiz.qpic.cn/mmbiz_png/tpAC6lR84R8cjPhede0jziaf7d6MYRcic2LORickP4RUicSvt6XHMdEbYMO8VPH0Fv7EQ95FDibflkYpeDVoy7VC6gw/640?wx_fmt=png&amp;from=appmsg" src="https://mmbiz.qpic.cn/mmbiz_png/tpAC6lR84R8cjPhede0jziaf7d6MYRcic2LORickP4RUicSvt6XHMdEbYMO8VPH0Fv7EQ95FDibflkYpeDVoy7VC6gw/640?wx_fmt=png&amp;from=appmsg"></p><p data-tool="mdnice编辑器">按下win+r,输入<code>control</code>打开控制面板,打开<code>程序和功能</code>:</p><p><img data-galleryid="" data-imgfileid="100020820" data-ratio="0.49834619625137816" data-s="300,640" data-type="png" data-w="907" data-src="https://mmbiz.qpic.cn/mmbiz_png/tpAC6lR84R8cjPhede0jziaf7d6MYRcic2EVIsiaibhp7HOX80uib8iboBkdo1RwInLNRjFD6ojrfGZRx6DgvuWNGwibQ/640?wx_fmt=png&amp;from=appmsg" src="https://mmbiz.qpic.cn/mmbiz_png/tpAC6lR84R8cjPhede0jziaf7d6MYRcic2EVIsiaibhp7HOX80uib8iboBkdo1RwInLNRjFD6ojrfGZRx6DgvuWNGwibQ/640?wx_fmt=png&amp;from=appmsg"></p><p data-tool="mdnice编辑器">点击<code>启用或关闭Windows功能</code></p><p><img data-galleryid="" data-imgfileid="100020821" data-ratio="0.4828693790149893" data-s="300,640" data-type="png" data-w="934" data-src="https://mmbiz.qpic.cn/mmbiz_png/tpAC6lR84R8cjPhede0jziaf7d6MYRcic2Bp7vx85aahPEVSDyxpmiaTIgKWcrPkoBhUa2D5YIVtPCy4slU9ico3fQ/640?wx_fmt=png&amp;from=appmsg" src="https://mmbiz.qpic.cn/mmbiz_png/tpAC6lR84R8cjPhede0jziaf7d6MYRcic2Bp7vx85aahPEVSDyxpmiaTIgKWcrPkoBhUa2D5YIVtPCy4slU9ico3fQ/640?wx_fmt=png&amp;from=appmsg"></p><p data-tool="mdnice编辑器">把以下3项勾上,然后重启电脑:</p><p><img data-galleryid="" data-imgfileid="100020822" data-ratio="2.3671171171171173" data-s="300,640" data-type="png" data-w="444" data-src="https://mmbiz.qpic.cn/mmbiz_png/tpAC6lR84R8cjPhede0jziaf7d6MYRcic2ofYLarfPgjuH32C0u29iaGfW4U5TgWMNoiboraIjZ0qqQDbRGgEPicGOg/640?wx_fmt=png&amp;from=appmsg" src="https://mmbiz.qpic.cn/mmbiz_png/tpAC6lR84R8cjPhede0jziaf7d6MYRcic2ofYLarfPgjuH32C0u29iaGfW4U5TgWMNoiboraIjZ0qqQDbRGgEPicGOg/640?wx_fmt=png&amp;from=appmsg"></p><h3 data-tool="mdnice编辑器"><span></span><span>安装内核更新包</span><span></span></h3><p data-tool="mdnice编辑器">power shell 以管理员方式运行(如果不升级可能会遇到报错,这个命令需要科学上网):</p><pre data-tool="mdnice编辑器"><span></span><code>wsl --update<br></code></pre><p data-tool="mdnice编辑器">将 WSL 默认版本设置为 WSL 2:</p><pre data-tool="mdnice编辑器"><span></span><code>wsl --<span>set</span>-default-version 2<br></code></pre><h3 data-tool="mdnice编辑器"><span></span><span>安装Ubuntu</span><span></span></h3><p data-tool="mdnice编辑器">打开Microsoft Store,安装Ubuntu即可,我选的22.04</p><p><img data-galleryid="" data-imgfileid="100020823" data-ratio="0.7611111111111111" data-s="300,640" data-type="png" data-w="1080" data-src="https://mmbiz.qpic.cn/mmbiz_png/tpAC6lR84R8cjPhede0jziaf7d6MYRcic2yVQlk8xfxV1icSyjT8XmgicVDKKDL19XJtDo4AyHV9yO7VibtZ8JUMYkA/640?wx_fmt=png&amp;from=appmsg" src="https://mmbiz.qpic.cn/mmbiz_png/tpAC6lR84R8cjPhede0jziaf7d6MYRcic2yVQlk8xfxV1icSyjT8XmgicVDKKDL19XJtDo4AyHV9yO7VibtZ8JUMYkA/640?wx_fmt=png&amp;from=appmsg"></p><p data-tool="mdnice编辑器">安装完成后就可以在开始菜单看到了,点击即可运行,第一次需要创建账号和密码,要牢记!</p><p data-tool="mdnice编辑器">win11现在默认的终端非常美观,直接用即可,各种设置都是可以直接点点点修改的,比如界面、字体、图标、配色等:</p><p><img data-galleryid="" data-imgfileid="100020824" data-ratio="0.4527777777777778" data-s="300,640" data-type="png" data-w="1080" data-src="https://mmbiz.qpic.cn/mmbiz_png/tpAC6lR84R8cjPhede0jziaf7d6MYRcic2R4k0BGruHicbBXfudspIv5jzaCX3TibbJtQncChBqD9tBWH2PeVBmyTQ/640?wx_fmt=png&amp;from=appmsg" src="https://mmbiz.qpic.cn/mmbiz_png/tpAC6lR84R8cjPhede0jziaf7d6MYRcic2R4k0BGruHicbBXfudspIv5jzaCX3TibbJtQncChBqD9tBWH2PeVBmyTQ/640?wx_fmt=png&amp;from=appmsg"></p><h2 data-tool="mdnice编辑器"><span>unset</span><span></span><span><span>unset</span>修改apt镜像<span>unset</span></span><span></span><span>unset</span></h2><ul data-tool="mdnice编辑器"><li><section>查看默认apt源:<code>cat /etc/apt/sources.list</code></section></li><li><section>更新apt镜像源,安装软件更快</section></li><li><section><br></section></li><ul><li><section>先备份源文件:<code>sudo cp /etc/apt/sources.list /etc/apt/sources.list.backup</code></section></li><li><section>编辑源文件:<code>sudo vim /etc/apt/sources.list</code></section></li><li><section>进入vim编辑器,在普通模式下(输入上面的代码后默认就是普通模式),使用<code>dd</code>(<code>dd</code>是vim中的命令,表示删除整行内容),删除所有内容,然后去搜索apt清华源(或者阿里源:阿里巴巴开源镜像站-OPSX镜像站-阿里云开发者社区),复制并粘贴(直接鼠标右键)到vim,然后输入<code>:wq</code>。然后输入<code>sudo apt update</code>(会输出很多信息),然后输入<code>sudo apt upgrade</code>(会输出更多信息)</section></li></ul></ul><p data-tool="mdnice编辑器">ubuntu22.04默认已经安装Python3.10,输入<code>python3 --version</code>可查看版本;但是没有安装<code>pip</code>,可以使用<code>sudo apt install python3-pip</code>安装</p><h2 data-tool="mdnice编辑器"><span>unset</span><span></span><span><span>unset</span>修改pip镜像<span>unset</span></span><span></span><span>unset</span></h2><p data-tool="mdnice编辑器">接下来修改<code>pip</code>的镜像:</p><pre data-tool="mdnice编辑器"><span></span><code><span># 清华</span><br>pip config <span>set</span> global.index-url https://pypi.tuna.tsinghua.edu.cn/simple<br><span># 或者阿里</span><br>pip config <span>set</span> global.index-url https://mirrors.aliyun.com/pypi/simple/<br></code></pre><p data-tool="mdnice编辑器">查看设置的结果:</p><pre data-tool="mdnice编辑器"><span></span><code>pip config list<br></code></pre><h2 data-tool="mdnice编辑器"><span>unset</span><span></span><span><span>unset</span>安装conda<span>unset</span></span><span></span><span>unset</span></h2><p data-tool="mdnice编辑器">安装conda,方便管理软件</p><ul data-tool="mdnice编辑器"><li><section><p>首先到清华conda镜像(Index of /anaconda/miniconda/ | 清华大学开源软件镜像站 | Tsinghua Open Source Mirror)下载miniconda linux版安装包,当然也可以直接wget官网的包,但是很慢;</p><pre><span></span><code><span># 这个</span><br>wget https://mirrors.tuna.tsinghua.edu.cn/anaconda/miniconda/Miniconda3-py310_24<span>.11</span><span>.1</span><span>-0</span>-Linux-x86_64.sh<br><br><span># 或者这个,我选择最新的</span><br>wget https://mirrors.tuna.tsinghua.edu.cn/anaconda/miniconda/Miniconda3-latest-Linux-x86_64.sh<br></code></pre></section></li><li><section><p>然后用mv命令把安装包移动到<code>/home/liyue/</code>的某一个文件夹下,最好自己建一个新文件夹,用来放各种安装包,方便管理;</p></section></li><li><section><p>运行安装命令:<code>bash Miniconda3-latest-Linux-x86_64.sh</code>,安装过程中注意<code>同意</code>或者<code>回车</code>(卖萌哥的教程:conda的安装与使用 2.0版(2022-08-12更新) - 简书)</p></section></li><li><section><p>安装完成后,会告诉你如果不想每次启动Linux时默认启动conda的base环境,可以使用命令<code>conda config --set auto_activate_base false</code></p></section></li><li><section><p>更改conda镜像为清华源(anaconda | 镜像站使用帮助 | 清华大学开源软件镜像站 | Tsinghua Open Source Mirror)或者其他源(Anaconda 软件仓库镜像使用帮助 - MirrorZ Help):</p><pre><span></span><code><span># 我选的上科大的镜像</span><br>conda config --add channels https://mirrors.shanghaitech.edu.cn/anaconda/pkgs/main<br>conda config --add channels https://mirrors.shanghaitech.edu.cn/anaconda/pkgs/r<br>conda config --add channels https://mirrors.shanghaitech.edu.cn/anaconda/pkgs/msys2<br>conda config --<span>set</span> custom_channels.conda-forge https://mirrors.shanghaitech.edu.cn/anaconda/cloud/<br>conda config --<span>set</span> custom_channels.bioconda https://mirrors.shanghaitech.edu.cn/anaconda/cloud/<br><br><span># 显示安装的频道</span><br>conda config --<span>set</span> show_channel_urls yes <br><span># 查看已经添加的频道</span><br>conda config --get channels<br></code></pre></section></li><li><section><p>创建新环境,彼此之间不会污染</p></section></li></ul><h2 data-tool="mdnice编辑器"><span>unset</span><span></span><span><span>unset</span>安装jupyterlab<span>unset</span></span><span></span><span>unset</span></h2><p data-tool="mdnice编辑器">直接使用<code>pip</code>安装即可:</p><pre data-tool="mdnice编辑器"><span></span><code>pip install jupyterlab<br></code></pre><p data-tool="mdnice编辑器">安装完成后,输入<code>jupyter lab</code>,发现运行不了,然后我关闭<code>wsl</code>,再重新打开,发现就可以顺利运行了。。。</p><p data-tool="mdnice编辑器">我使用conda创建了一个叫<code>sc</code>的小环境,然后安装了<code>scanpy</code>这个包,激活这个小环境后,再运行<code>jupyter lab</code>,发现在jupyter中<code>import scanpy as sc</code>总是报错找不到这个包,搜索后发现是因为内核不在同一个位置导致的。</p><p data-tool="mdnice编辑器"><code>pip</code>安装的这个内核在:</p><pre data-tool="mdnice编辑器"><span></span><code><span>import</span> sys<br>print(sys.path)<br><br>[<span>'/usr/lib/python310.zip'</span>, <span>'/usr/lib/python3.10'</span>, <span>'/usr/lib/python3.10/lib-dynload'</span>, <span>''</span>, <span>'/home/ayue/.local/lib/python3.10/site-packages'</span>, <span>'/usr/local/lib/python3.10/dist-packages'</span>, <span>'/usr/lib/python3/dist-packages'</span>]<br></code></pre><p data-tool="mdnice编辑器">但是<code>sc</code>这个小环境的包位于conda的目录下,所以导致<code>import</code>失败:</p><p><img data-galleryid="" data-imgfileid="100020825" data-ratio="0.2388888888888889" data-s="300,640" data-type="png" data-w="1080" data-src="https://mmbiz.qpic.cn/mmbiz_png/tpAC6lR84R8cjPhede0jziaf7d6MYRcic2G2os4VSMjmHsRUtNMnWDgmHrsSU8FkQJcAWwzK24gJtnNLFfkqPXHw/640?wx_fmt=png&amp;from=appmsg" src="https://mmbiz.qpic.cn/mmbiz_png/tpAC6lR84R8cjPhede0jziaf7d6MYRcic2G2os4VSMjmHsRUtNMnWDgmHrsSU8FkQJcAWwzK24gJtnNLFfkqPXHw/640?wx_fmt=png&amp;from=appmsg"></p><p data-tool="mdnice编辑器">于是根据这篇教程(Jupyter Lab 使用 conda 虚拟环境_jupyterlab 怎么读出conda虚拟环境-CSDN博客)修改,给已经创建好的<code>sc</code>这个小环境添加<code>ipykernel</code>:</p><pre data-tool="mdnice编辑器"><span></span><code><span># 给小环境sc添加ipykernel</span><br>conda install -n sc ipykernel</code><code># 激活小环境</code><code>conda activate sc<br><span># 将小环境写入jupyter lab的kernel中</span><br>python -m ipykernel install --user --name sc --display-name <span>"sc"</span><br></code></pre><p data-tool="mdnice编辑器">这样就可以切换内核了:</p><p><img data-galleryid="" data-imgfileid="100020826" data-ratio="0.42314814814814816" data-s="300,640" data-type="png" data-w="1080" data-src="https://mmbiz.qpic.cn/mmbiz_png/tpAC6lR84R8cjPhede0jziaf7d6MYRcic2pVG3xq0ULl08PibDezTNb0luMJuZzJsQ87B4fvhfic4mxWaOPV0otBHg/640?wx_fmt=png&amp;from=appmsg" src="https://mmbiz.qpic.cn/mmbiz_png/tpAC6lR84R8cjPhede0jziaf7d6MYRcic2pVG3xq0ULl08PibDezTNb0luMJuZzJsQ87B4fvhfic4mxWaOPV0otBHg/640?wx_fmt=png&amp;from=appmsg"></p><p><img data-galleryid="" data-imgfileid="100020827" data-ratio="1.0628140703517588" data-s="300,640" data-type="png" data-w="398" data-src="https://mmbiz.qpic.cn/mmbiz_png/tpAC6lR84R8cjPhede0jziaf7d6MYRcic2Q5Lic6ysYGJJf57BTjvyGQFWwTRuQkicrLVhN96VOmzwbdajwm6FOUibw/640?wx_fmt=png&amp;from=appmsg" src="https://mmbiz.qpic.cn/mmbiz_png/tpAC6lR84R8cjPhede0jziaf7d6MYRcic2Q5Lic6ysYGJJf57BTjvyGQFWwTRuQkicrLVhN96VOmzwbdajwm6FOUibw/640?wx_fmt=png&amp;from=appmsg"></p><p data-tool="mdnice编辑器">切换内核后,再<code>import</code>就不会报错了,就可以顺利使用这个小环境的包了。</p></section><p><span><br></span></p><hr><blockquote><p><span><strong>联系我们,关注我们</strong></span></p><ol><li><section>免费QQ交流群1:613637742</section></li><li><section>免费QQ交流群2:608720452</section></li><li><section>公众号消息界面关于作者获取联系方式</section></li><li><section>知乎、CSDN、简书同名账号</section></li><li><section>哔哩哔哩:阿越就是我</section></li></ol></blockquote></section></section></section></section></section><p><mp-style-type data-value="3"></mp-style-type></p></div>
<hr>
<a href="https://mp.weixin.qq.com/s/qAM1AU8u5cUIaRHQOV_pxg",target="_blank" rel="noopener noreferrer">原文链接</a>

0 comments on commit c595fb2

Please sign in to comment.