今天在使用Python的時候,使用Anaconda創建了一個虛擬環境,在Jupyter Notebook中也激活了這個虛擬環境,而且在這個環境下也安裝好了相應的包
但怎么實驗,也無法在Jupyter Notebook中成功import新包,報錯顯示No module xxx found。
實驗了各種方法均不成功,后按照下文的方式重新配置一個新的虛擬環境后,再次安裝新包成功,特此記錄一下方法。
文章的鏈接和具體內容如下:
在Windows下,為Jupyter創建新的kernel
1.創建環境
- 打開Anaconda Prompt
- 輸入以下代碼創建環境
(D:\Users\seanhe\Anaconda3) C:\Users\seanhe>conda create -n KernelForJupyter python=3.6
- 得到以下結果
The following NEW packages will be INSTALLED: certifi: 2016.2.28-py36_0 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free pip: 9.0.1-py36_1 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free python: 3.6.2-0 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free setuptools: 36.4.0-py36_1 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free vc: 14-0 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free vs2015_runtime: 14.0.25420-0 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free wheel: 0.29.0-py36_0 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free wincertstore: 0.2-py36_0 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free Proceed ([y]/n)? y Linking packages ... [ COMPLETE ]|##################################################| 100% # # To activate this environment, use: # > activate KernelForJupyter # # To deactivate this environment, use: # > deactivate KernelForJupyter # # * for power-users using bash, you must source #
2.激活環境以及安裝必要的包
- 輸入以下代碼激活環境
(D:\Users\seanhe\Anaconda3) C:\Users\seanhe> activate KernelForJupyter
- 安裝 ipykernel
(KernelForJupyter) C:\Users\seanhe>conda install ipykernel
3.創建新的kernel
- 輸入以下代碼創建新的kernel
(KernelForJupyter) C:\Users\seanhe>python -m ipykernel install --name NewKernel Installed kernelspec NewKernel in C:\ProgramData\jupyter\kernels\newkernel
4.Jupyter其他操作
- 我們通過以下代碼查看juypter kernel的其他命令
(KernelForJupyter) C:\Users\seanhe>jupyter kernelspec --help Manage Jupyter kernel specifications. Subcommands ----------- Subcommands are launched as `jupyter kernelspec cmd [args]`. For information on using subcommand 'cmd', do: `jupyter kernelspec cmd -h`. list List installed kernel specifications. install Install a kernel specification directory. uninstall Alias for remove remove Remove one or more Jupyter kernelspecs by name. install-self [DEPRECATED] Install the IPython kernel spec directory for this Python
- 例如我們可以通過以下指令查看當前環境下的kernel
(KernelForJupyter) C:\Users\seanhe>jupyter kernelspec list
Available kernels:
python3 D:\Users\seanhe\Anaconda3\envs\KernelForJupyter\share\jupyter\kernels\python3
newkernel C:\ProgramData\jupyter\kernels\newkernel
5.作在Anaconda中刪除一個環境
?方法1:使用Anaconda Prompt(Windows)或終端(macOS/Linux)??
- ?打開Anaconda Prompt(Windows)或終端(macOS/Linux)??
- Windows:搜索并打開“Anaconda Prompt”(或在開始菜單中找到Anaconda文件夾下的Prompt)。
- macOS/Linux:直接打開終端。
- ?列出所有環境(可選)??
確認要刪除的環境名稱是否正確:conda env list
或
conda info --envs
輸出會顯示所有環境,當前環境前會標有
*
。 - ?刪除指定環境?
使用以下命令(將環境名
替換為你要刪除的環境名稱):conda env remove --name 環境名
或簡寫:
conda remove --name 環境名 --all
- ?驗證是否刪除成功?
再次運行conda env list
,確認目標環境已消失。
?方法2:通過Anaconda Navigator(圖形界面)??
- 打開 ?Anaconda Navigator?(在開始菜單或應用程序中搜索)。
- 在左側菜單中點擊 ??“Environments”?。
- 在環境列表中找到要刪除的環境,點擊其右側的 ?**▼? 下拉箭頭,選擇 ??“Remove”?**。
- 確認刪除操作。
?注意事項?
- ?刪除環境會徹底移除該環境下的所有包和依賴,包括Python解釋器,操作不可逆。
- 如果環境正在使用(如當前激活的環境),需先退出:
conda deactivate
- 如果遇到權限問題(如macOS/Linux下提示權限不足),可嘗試在命令前加
sudo
(不推薦常規使用,優先修復權限問題)。
?常見問題?
- ?環境名拼寫錯誤?:確保輸入的環境名與
conda env list
中的完全一致(區分大小寫)。 - ?刪除失敗?:如果環境損壞或存在沖突,可以嘗試手動刪除環境文件夾(路徑通常在
~/anaconda3/envs/環境名
或~/miniconda3/envs/環境名
),但建議優先使用命令行操作。
完成上述步驟后,目標環境即被徹底刪除。
6.在PyCharm 中使用 Anaconda 中新配置的環境
在 PyCharm 中使用 Anaconda 中新配置的環境,如果無法正常使用,通常是由于以下幾個原因導致的。以下是詳細的排查和解決步驟:
1. ?確認環境是否已正確創建?
- 打開 Anaconda Prompt 或終端,運行以下命令查看所有環境:
conda env list
或
conda info --envs
- 確保你創建的環境(如
my_new_env
)出現在列表中,并且路徑正確。
2. ?檢查 PyCharm 的 Python 解釋器設置?
- 在 PyCharm 中,打開項目后:
- 點擊頂部菜單欄的 ?File > Settings?(Windows/Linux)或 ?PyCharm > Preferences?(macOS)。
- 進入 ?Project: <你的項目名> > Python Interpreter。
- 點擊右上角的齒輪圖標,選擇 ?Add...??(添加解釋器)。
- 在彈出的窗口中:
- 選擇 ?Conda Environment?(Conda 環境)。
- 確保勾選了 ?Existing environment?(現有環境),然后在下拉菜單中查找你的環境。
- 如果環境未顯示,可以手動指定解釋器路徑:
- 點擊 ?**...?**? 按鈕,導航到 Anaconda 安裝目錄下的
envs/<你的環境名>/bin/python
(Linux/macOS)或envs\<你的環境名>\python.exe
(Windows)。
- 點擊 ?**...?**? 按鈕,導航到 Anaconda 安裝目錄下的
3. ?PyCharm 未正確識別 Conda 環境?
- PyCharm 可能未正確關聯到 Anaconda 的 Conda 可執行文件。嘗試以下操作:
- 在 ?Python Interpreter? 設置中,選擇 ?Add...?。
- 選擇 ?Conda Environment。
- 確保 ?Conda executable?(Conda 可執行文件)路徑正確:
- 默認路徑可能是:
- Windows:
C:\Users\<用戶名>\Anaconda3\Scripts\conda.exe
或C:\ProgramData\Anaconda3\Scripts\conda.exe
。 - macOS/Linux:
~/anaconda3/bin/conda
或~/miniconda3/bin/conda
。
- Windows:
- 如果路徑不正確,手動指定 Conda 的安裝路徑。
- 默認路徑可能是:
4. ?環境未激活或 PyCharm 緩存問題?
- 重啟 PyCharm:有時 PyCharm 的緩存可能導致環境未及時更新。
- 在 PyCharm 中嘗試 ?File > Invalidate Caches / Restart?(文件 > 使緩存失效并重啟),然后重新檢查解釋器列表。
5. ?環境創建在非默認路徑?
- 如果你在創建環境時指定了自定義路徑(如
conda create --prefix=/path/to/env
),PyCharm 可能無法自動檢測到它。 - 在 ?Python Interpreter? 設置中,手動選擇該路徑下的
python
可執行文件(如/path/to/env/bin/python
)。
6. ?PyCharm 版本問題?
- 確保你使用的是較新版本的 PyCharm(如 2020.3 或更高版本),舊版本可能對 Conda 支持不完善。
- 更新 PyCharm:?Help > Check for Updates?(幫助 > 檢查更新)。
7. ?驗證環境是否可用?
- 在終端中激活環境并測試 Python 是否正常工作:
conda activate my_new_env python --version
- 如果終端中環境正常,但 PyCharm 仍無法識別,可能是 PyCharm 的配置問題。
總結步驟:
- 確認環境已通過
conda env list
創建成功。 - 在 PyCharm 中手動添加解釋器,選擇 ?Conda Environment? 并指定路徑。
- 檢查 Conda 可執行文件路徑是否正確。
- 重啟 PyCharm 或清除緩存。
如果問題仍未解決,請提供以下信息:
- Anaconda 版本(
conda --version
)。 - PyCharm 版本(Help > About)。
- 操作系統類型(Windows/macOS/Linux)。
- 環境創建的具體命令(是否使用了
--prefix
)。
文章評論