简体   繁体   English

Package 在 Jupyter Notebook 中找到但不是命令行

[英]Package found in Jupyter Notebook but not command line

Specs眼镜

  • Ubuntu 18.x Ubuntu 18.x
  • conda 4.8.3康达 4.8.3

I created a new conda environment in the command line.我在命令行中创建了一个新的 conda 环境。 After installing some packages, I ran a kernel in jupyterlabs using the packages with no problem.在安装了一些包之后,我在 jupyterlabs 中运行了一个 kernel,使用这些包没有问题。 I later wrote a.py program, but it gave errors saying that I had not installed the packages.我后来写了一个 .py 程序,但它给出了错误,说我没有安装软件包。 Here is a minimal example to reproduce the error:这是重现错误的最小示例:

conda create -n new_env pip ipykernel python=3.7
conda activate new_env
whereis pip

/opt/conda/envs/new_env/pip /opt/conda/envs/new_env/pip

/opt/conda/envs/new_env/pip install pandas pandas
ipython kernel install --user --name=new_env

Inside a jupyterlab notebook (with the new_env environment), import pandas as pd works perfectly.在 jupyterlab 笔记本(使用 new_env 环境)中, import pandas as pd完美运行。 Inside the command line this yields an error:在命令行内部,这会产生一个错误:

conda activate new_env
python
>>>import pandas

Traceback (most recent call last): File "", line 1, in ModuleNotFoundError: No module named 'pandas' Traceback(最近一次调用最后一次):ModuleNotFoundError 中的文件“”,第 1 行:没有名为“pandas”的模块

The Culprit?罪魁祸首?

Inside the jupyter notebook (with the new_env environment), sys.path is在 jupyter notebook 内部(使用 new_env 环境), sys.path

['/rapids/notebooks/my_code/TF_Models/DAE',
 '/opt/conda/envs/new_env/lib/python37.zip',
 '/opt/conda/envs/new_env/lib/python3.7',
 '/opt/conda/envs/new_env/lib/python3.7/lib-dynload',
 '',
 '/opt/conda/envs/new_env/lib/python3.7/site-packages',
 '/opt/conda/envs/new_env/lib/python3.7/site-packages/IPython/extensions',
 '/root/.ipython']

inside the command line (with new_env conda activated), the sys.path is:在命令行内(激活了new_env ), sys.path是:

['', 
'/opt/conda/lib/python38.zip', 
'/opt/conda/lib/python3.8', 
'/opt/conda/lib/python3.8/lib-dynload', 
'/opt/conda/lib/python3.8/site-packages']

If not the actual issue, this is clearly tied to the issue.如果不是实际问题,这显然与问题有关。 The jupyterlab is pointed to the correct paths but the python in the conda environment is not. jupyterlab 指向正确的路径,但 conda 环境中的 python 不是。 This gives me 2 questions:这给了我两个问题:

1. How can I get the command line python with the new_env environment pointed to the correct path? 1.如何在new_env环境指向正确路径的情况下获取命令行python?

2. How can I get the command line python with the new_env environment to run the installed packages? 2、如何在new_env环境下获取命令行python来运行安装的包?

conda create -n new_env pip ipykernel python=3.7 conda create -n new_env pip ipykernel python=3.7

try with... conda create -n new_env pip ipykernel python==3.7尝试... conda create -n new_env pip ipykernel python==3.7

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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