简体   繁体   中英

Conda virtual environment for IPython

I'm fairly new to the Python scene. My problem is that when I launch a jupyter notebook from an Anaconda Powershell with my DataScience virtual environment activated, the notebook does not have my virtual environment in it's PATH, and therefore cannot find some packages (like plotly and progress). The same is true when I launch VS Code from Anaconda Navigator with DataScience activated. When I run import plotly in an interactive window, I get ModuleNotFoundError: No module named 'plotly' . But when I run this line in the terminal within VS Code, it runs without error.

So I have run the following commands in various shell/terminal sessions:

import sys print(sys.path)

In a VS Code terminal I get:

['', 'C:\\\\Users\\\\adiad\\\\Anaconda3\\\\envs\\\\DataScience\\\\python37.zip', 'C:\\\\Users\\\\adiad\\\\Anaconda3\\\\envs\\\\DataScience\\\\DLLs', 'C:\\\\Users\\\\adiad\\\\Anaconda3\\\\envs\\\\DataScience\\\\lib', 'C:\\\\Users\\\\adiad\\\\Anaconda3\\\\envs\\\\DataScience', 'C:\\\\Users\\\\adiad\\\\Anaconda3\\\\envs\\\\DataScience\\\\lib\\\\site-packages']

In an interactive window in VS Code I get:

['C:\\\\Users\\\\adiad\\\\AppData\\\\Local\\\\Temp\\\\04e2b30c-4fc3-4aa9-9567-3aba17081a73', 'C:\\\\Users\\\\adiad\\\\Anaconda3\\\\python37.zip', 'C:\\\\Users\\\\adiad\\\\Anaconda3\\\\DLLs', 'C:\\\\Users\\\\adiad\\\\Anaconda3\\\\lib', 'C:\\\\Users\\\\adiad\\\\Anaconda3', '', 'C:\\\\Users\\\\adiad\\\\Anaconda3\\\\lib\\\\site-packages', 'C:\\\\Users\\\\adiad\\\\Anaconda3\\\\lib\\\\site-packages\\\\win32', 'C:\\\\Users\\\\adiad\\\\Anaconda3\\\\lib\\\\site-packages\\\\win32\\\\lib', 'C:\\\\Users\\\\adiad\\\\Anaconda3\\\\lib\\\\site-packages\\\\Pythonwin', 'C:\\\\Users\\\\adiad\\\\Anaconda3\\\\lib\\\\site-packages\\\\IPython\\\\extensions', 'C:\\\\Users\\\\adiad\\\\.ipython']

In a jupyter notebook running in my browser I get:

['C:\\\\Users\\\\adiad\\\\Anaconda3\\\\envs\\\\test', 'C:\\\\Users\\\\adiad\\\\Anaconda3\\\\python37.zip', 'C:\\\\Users\\\\adiad\\\\Anaconda3\\\\DLLs', 'C:\\\\Users\\\\adiad\\\\Anaconda3\\\\lib', 'C:\\\\Users\\\\adiad\\\\Anaconda3', '', 'C:\\\\Users\\\\adiad\\\\Anaconda3\\\\lib\\\\site-packages', 'C:\\\\Users\\\\adiad\\\\Anaconda3\\\\lib\\\\site-packages\\\\win32', 'C:\\\\Users\\\\adiad\\\\Anaconda3\\\\lib\\\\site-packages\\\\win32\\\\lib', 'C:\\\\Users\\\\adiad\\\\Anaconda3\\\\lib\\\\site-packages\\\\Pythonwin', 'C:\\\\Users\\\\adiad\\\\Anaconda3\\\\lib\\\\site-packages\\\\IPython\\\\extensions', 'C:\\\\Users\\\\adiad\\\\.ipython']

The IPython session don't appear to reference my virtual environment. So my question is: what do I need to do make IPython run with same environment as my terminal?

I found the following SO question which seems to answer my question, but I find it hard to believe that everyone is following this practice. How to start an ipython shell(not notebook) within a conda or virtualenv

Here's my configuration:

conda version : 4.7.12 conda-build version : 3.18.8 python version : 3.7.3.final.0 virtual packages : base environment : C:\\Users\\adiad\\Anaconda3 (writable) channel URLs : https://conda.anaconda.org/conda-forge/win-64 https://conda.anaconda.org/conda-forge/noarch https://repo.anaconda.com/pkgs/main/win-64 https://repo.anaconda.com/pkgs/main/noarch https://repo.anaconda.com/pkgs/r/win-64 https://repo.anaconda.com/pkgs/r/noarch https://repo.anaconda.com/pkgs/msys2/win-64 https://repo.anaconda.com/pkgs/msys2/noarch package cache : C:\\Users\\adiad\\Anaconda3\\pkgs C:\\Users\\adiad\\.conda\\pkgs C:\\Users\\adiad\\AppData\\Local\\conda\\conda\\pkgs envs directories : C:\\Users\\adiad\\Anaconda3\\envs C:\\Users\\adiad\\.conda\\envs C:\\Users\\adiad\\AppData\\Local\\conda\\conda\\envs platform : win-64 user-agent : conda/4.7.12 requests/2.22.0 CPython/3.7.3 Windows/10 Windows/10.0.18362

After doing further digging, my problem ought to be filed under, "knowing enough to be dangerous." My problem was ultimately caused by the fact that the jupyter package hadn't yet been installed in my new environment. So whenever I attempted to launch an IPython session of some kind, either in VS Code or in a browser, the application would look in my environment and see that the IPython packages weren't installed. It would then look to other conda environments and use the "nearest" equivalent, which was the base environment. Hence, most of the packages would load, but not all.

The fix to my problem was: conda install jupyter

另一个简单的修复:在 Anaconda Navigator 上启动 CMD.exe 提示安装: conda install jupyter And Conda install plotly

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM