conda虚拟环境使用以及在jupyter notebook/pycharm上的使用

conda 相关操作

创建虚拟环境

  1. 在终端里输入
conda create -n your_env_name python=x.x
  1. 启动 anaconda navigator 进行创建,如下图
    anaconda
  2. 在 jupyter notebook 中创建(需要先安装 nb_conda,如何安装如下),如下图jupyter

查看已经创建的环境

conda env list

启动创建的环境(环境名:xxx)

conda activate xxx

退出当前的环境

conda deactivate

删除环境

  1. 在终端里输入
conda remove -n xxx --all
  1. 在 anaconda navigator 中删除环境

  2. 安装 nb_conda 后,在 jupyter notebook 中删除环境

如何“复制”环境

具体而言就是,我们已经有一个环境 bq_env,现在需要在 bq_env 中进行新的开发,新开发需要安装新的 python 包,所以我们需要新建一个新环境 py_tf,其中的 python 包和 bq_env 完全相同。

在电脑里找到 anaconda 文件夹下的 env 文件夹,其中就能看到我们已经创建的虚拟环境。满足上面的需求,我们只需要复制 bq_env 文件夹到 env,改名为 py_tf,如下图。
在这里插入图片描述

jupyter notebook 中使用 conda 虚拟环境

使用虚拟环境

  1. 安装 nb_conda
conda install nb_conda
  1. 在终端里打开需要使用的新环境,然后安装 ipykernel
conda activate xxx
conda install ipykernel

或者

conda install -n xxx ipykernel
  1. 启动 jupyter notebook(哪个环境下启动均可),然后在新建的时候选择使用哪个 conda 环境。或者在新建了 notebook 后,选择“改变服务”。
    新建
    在这里插入图片描述

查看虚拟环境

命令行操作,也可以安装 conda_nb 后,直接在 jupyter notebook 页面的 conda 中查看。

jupyter kernelspec list

删除虚拟环境

删除步骤同上。

但是有时候删除了 conda 虚拟环境 xxx 后,jupyter notebook 中仍然有 xxx 环境,好像没有删干净一样。这可能是因为我们之前在 xxx 环境中安装了 ipykernel,所以需要删除指定的 kernel。

jupyter kernelspec uninstall xxx

安装 conda_nb 后 jupyter notebook 警告问题

这个警告应该是 conda_nb 的问题,不影响 jupyter notebook 的使用。如果想要删掉可以参考下面的链接。

https://github.com/Anaconda-Platform/nb_conda/issues/66

pycharm 中使用 conda 虚拟环境

添加 conda 虚拟环境

  1. 在 pycharm 中点击 preference(有些版本的 pycharm 是 setting),找到 Python Interpreter,点击 Show All,如下图。
    pycharm
  2. 打开后可以看到目前已经有的 conda 环境,如果我们有新建的环境没有在这里面,那么可以点击下面的“+”进行添加。
    py2
  3. 点击“+”后,就能看到虚拟环境(Virtualenv Environment)。右侧同样可以新建虚拟环境(New environment)以及添加现有的环境(Existing environment)。
    在这里插入图片描述
  4. Existing environment 中我们就可以选择已经创建好,但还没有被 pycharm 使用的 conda 环境。
PyCharm是一款强大的Python集成开发环境(IDE),它支持配置Jupyter Notebook环境以便于交互式数据分析和科学计算。以下是设置步骤: 1. **安装Jupyter Notebook**:首先确保你的系统已经安装了Jupyter Notebook。如果没有,可以在命令行或终端通过`pip install jupyter notebook`进行安装。 2. **启动Jupyter Server**:打开命令行并导航到含有Python项目的目录,运行`jupyter notebook`命令来启动服务器。 3. **添加Python Interpreter to PyCharm**: - 打开PyCharm,点击菜单栏的"File" -> "Settings" (Windows/Linux) 或者 "Preferences" (Mac OS)。 - 在左侧选择"Project", 然后选择"Python Interpreters"。 - 点击"+"按钮,创建新的虚拟环境,如果使用的是venv, 输入路径选择对应的python.exe文件;如果使用Conda,选择Conda环境。 4. **配置Jupyter Notebook Support**: - 再次回到"Settings"或"Preferences",找到"Editor" -> "Interpreter Console"。 - 在"Interpreter Console Type"下拉菜单中选择"Jupyter Notebook"。 - 在"Path to Jupyter executable"输入Jupyter Notebook的完整路径,通常是`path/to/jupyter-notebook`. 5. **创建Notebook**: - 在PyCharm中,点击菜单栏的"File" -> "New" -> "Jupyter Notebook",然后选择项目中的相应目录,即可创建一个新的Jupyter Notebook。 6. **连接到远程Jupyter Server**: - 如果需要连接到远程Jupyter Server,需要配置SSH隧道或直接填写Jupyter Notebook服务器地址。 现在,你已经在PyCharm中成功配置了Jupyter Notebook环境,可以直接在PyCharm内打开、编辑和运行Notebook文件。
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值