简体   繁体   中英

Package found in Jupyter Notebook but not command line

Specs

  • Ubuntu 18.x
  • conda 4.8.3

I created a new conda environment in the command line. After installing some packages, I ran a kernel in jupyterlabs using the packages with no problem. I later wrote a.py program, but it gave errors saying that I had not installed the packages. 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 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. 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'

The Culprit?

Inside the jupyter notebook (with the new_env environment), sys.path is

['/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:

['', 
'/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. This gives me 2 questions:

1. How can I get the command line python with the new_env environment pointed to the correct path?

2. How can I get the command line python with the new_env environment to run the installed packages?

conda create -n new_env pip ipykernel python=3.7

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

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