I have a base conda environment, from which I have run jupyter lab:
(base) $ jupyter lab
Then, from another virtual environment, I have done
(venv) $ pip install ipywidgets
(venv) $ pip install ipykernel
(venv) $ python -m ipykernel install --user --name my-kernel
So, then, in Jupyter Lab (which was started from my base environment), I can open a notebook and select my-kernel as the kernel.
From within such a notebook(which is running my-kernel), how can I detect whether JupyterLab (which was started from my base environment) has ipywidgets installed?
I cannot just do import ipywidgets and see if I get ModuleNotFoundError because that would only detect whether ipywidgets was installed in my-kernel - however, I'm trying to find out if it is installed in my base environment.
It partially depends on how you are creating your virtual environments and which OS you are using. I.e. for conda users, this will be different. You can make a subprocess call to pip in the base environment and return a list of installed packages.
import sys
import subprocess
from pathlib import Path
def is_installed_in_base(pkg_name):
pip = Path(sys.base_prefix).joinpath('bin', 'pip') # Linux
# pip = Path(sys.base_prefix).joinpath('Scripts', 'pip.exe') # Windows
proc = subprocess.Popen(
[pip.as_posix(), 'list'],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
)
out, err = proc.communicate()
packages = out.decode().lower().split('\n')[2:]
packages = [pkg.split()[0].strip() for pkg in packages if pkg]
return pkg_name.lower() in packages
is_installed_in_base('ipywidgets')
# returns:
True
I guess maybe
import ipywidgets
print(ipywidgets.__file__)
Related
I'm facing weird issue in my Jupyter-notebook.
In my first cell:
import sys
!{sys.executable} -m pip install numpy
!{sys.executable} -m pip install Pillow
In the second cell:
import numpy as np
from PIL import Image
But it says : ModuleNotFoundError: No module named 'numpy'
I have used this command to install Jupyter notebook :
sudo apt install python3-notebook jupyter jupyter-core python-ipykernel
Additional information :
pip --version
pip 20.2.2 from /home/maifee/.local/lib/python3.7/site-packages/pip (python 3.7)
python --version
Python 3.7.5
Thanks to #suuuehgi.
When Jupyter Notebook isn't opened as root:
import sys
!{sys.executable} -m pip install --user numpy
I've had occasional weird install issues with Jupyter Notebooks as well when I'm running a particular virtual environment. Generally, installing with pip directly in the notebook in this form:
!pip install numpy
fixes it. Let me know how it goes.
Restarting the kernal from the Jupyter Notebook solved this issue for me
I had a similar issue. Turns out I renamed an upstream path. And I hadn't deactivated my conda env first. When I deactivated the env.
conda deactivate
Then when I activated it again, everything was as it should have been.
conda activate sample
Now I am seeing other issues with jupyter themes... but its not impacting my numpy code. So, at least I fixed the "ModuleNotFoundError: No module named 'numpy'" error
I have the same problem. My numpy is installed, I am using the same folder as usual.
If I try 'conda deactivate', I get the message:
ValueError: The python kernel does not appear to be a conda environment. Please use %pip install instead.
I added a print of the 'pip install numpy' result and the 'Module not found error' after
Here is a solution which worked for me:
lib_path="c:\\users\\user\\python_39\\lib\\site-packages\\"
MODULE_NAME = "module_to_import"
MODULE_PATH = lib_path+MODULE_NAME+"\\__init__.py"
import importlib
import sys
spec = importlib.util.spec_from_file_location(MODULE_NAME, MODULE_PATH)
module = importlib.util.module_from_spec(spec)
sys.modules[spec.name] = module
spec.loader.exec_module(module)
import module_to_import
When i create a project by Pycharm in Anconda environment, use : conda info --envs i get this :
base C:\Users\Admin\Anaconda3
env_dlib C:\Users\Admin\Anaconda3\envs\env_dlib
opencv-env C:\Users\Admin\Anaconda3\envs\opencv-env
then use command : conda activate env_dilb i can import dlib easily by using this command :python then import dlib, but i can not import cv2 by import cv2, it get notice : "ModuleNotFoundError: No module named 'cv2'".
And conversely when i use conda activate opencv-env, I can only do
import cv2.
How can i import two of both, and some other libraries too.
In the environment "dlib" package is available but "opencv" is not available.
Please follow the below steps to install opencv in the environment:
conda activate env_dlib
pip install opencv-python
After that try importing opencv using below command:
import cv2
I cannot import pandas_datareader on my jupyter notebook(via anaconda, python3) on my windows 10 laptop. It has been installed and I can see the file but It's having errors with importing into the jupyter notebook file. Any help?
I have tried :
pip install pandas-datareader ,
pip3 install pandas-datareader ,
conda install -c anaconda pandas-datareader
I expect the the code to run smoothly however I get
ModuleNotFoundError Traceback (most recent call last)
in ()
----> 1 import pandas_datareader
ModuleNotFoundError: No module named 'pandas_datareader'
Most obvious reason can be that you are using differnt enviroments(kernels) for jupyter notebook and others. Try running this code inside jupyter notebook empty project:
try:
from pip._internal.operations import freeze
except ImportError: # if your pip version is bigger then 10
from pip.operations import freeze
requirements = freeze.freeze()
for i in requirements:
print(i)
and check if there are your's missing imports
You have to run CMD Prompt from Anaconda navigator or find it from Start menu. Then use this command:
conda install pandas_datareader
I underwent similar difficulties and successfully fixed thanks to this post - Trouble installing some libraries in python (oauth2client and gspread) Conda is installing packagies to environment for Anaconda which is obviously different than pip uses.
I am trying to install plotnine for a notebook I am using. I have done the following:
Created a conda environment using python 3.6, and adding plotnine
Launching jupyter lab with the above environment activated
In the notebook, I added the following line:
!conda install -c conda-forge --yes plotnine
However, my output makes no sense. First it says that all requested packages have been installed, and then it says it cannot find the module
!conda install -c conda-forge --yes plotnine
from plotnine import *
Solving environment: done
# All requested packages already installed.
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-1-386ef81e08ff> in <module>()
11 get_ipython().system('conda install -c conda-forge --yes plotnine')
12 ######
---> 13 from plotnine import * # python clone of ggplot2
14 matplotlib.rcParams['figure.figsize'] = [12, 8]
15 matplotlib.rcParams['lines.linewidth'] = 2
ImportError: No module named 'plotnine'
In case there is a known conflict, here is the entire import statement:
import gsc # proprietary module
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
import pandas as pd
from ipywidgets import interact, FloatSlider
from util_demo import *
# adding installation of plotnine, which is not included by default
# import sys
!conda install -c conda-forge --yes plotnine
######
from plotnine import * # python clone of ggplot2
matplotlib.rcParams['figure.figsize'] = [12, 8]
matplotlib.rcParams['lines.linewidth'] = 2
matplotlib.rcParams['xtick.labelsize'] = 24
matplotlib.rcParams['ytick.labelsize'] = 24
matplotlib.rcParams['legend.fontsize'] = 24
matplotlib.rcParams['axes.labelsize'] = 24
EDIT: I also checked sys.path within the jupyter notebook and get the following. I do not see anything about conda here. Should I update either PATH or PYTHONPATH?
['',
'/usr/local/Cellar/python3/3.5.1/Frameworks/Python.framework/Versions/3.5/lib/python35.zip',
'/usr/local/Cellar/python3/3.5.1/Frameworks/Python.framework/Versions/3.5/lib/python3.5',
'/usr/local/Cellar/python3/3.5.1/Frameworks/Python.framework/Versions/3.5/lib/python3.5/plat-darwin',
'/usr/local/Cellar/python3/3.5.1/Frameworks/Python.framework/Versions/3.5/lib/python3.5/lib-dynload',
'/usr/local/Cellar/python3/3.5.1/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages',
'/usr/local/Cellar/python3/3.5.1/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/IPython/extensions',
'/Users/adamg/.ipython']
I had the same problem. It looks like for me, my notebook in Jupyter Lab was running the base kernel and not the kernel of the virtual environment. Type
import sys
sys.executable
into your notebook. For me, I got the result
'/anaconda3/bin/python'
instead of the desired
'/anaconda3/envs/myenv/bin/python'
I solved it by following the instructions in the iPython documentation. In summary, you need to install a new iPython kernel for your new environment. Run:
conda install -n myenv ipython
conda activate myenv
python -m ipykernel install --user --name myenv --display-name "Python (myenv)"
Then, to run Jupyter Lab in the new environment:
conda activate myenv
jupyter lab
And you should be able to select the kernel "Python (myenv)" when you open a new notebook (also in the top right of an existing notebook).
#try pip install first and rerun; the code line below should solve it:
pip install plotnine
This one is best shown through what I'm trying to do and what is going on.
In my python file, I want to import seaborn.
It's available to install via conda.
(me_dev)[me#ip-***]$ conda install seaborn
Fetching package metadata: ....
Solving package specifications: ............................
# All requested packages already installed.
# packages in environment at /home/me/miniconda/envs/me_dev:
#
seaborn 0.7.0 py27_0
(me_dev)[me#ip-***** ****]$ which python
~/miniconda/bin/python
(me_dev)[me#ip-****]$ which ipython
~/miniconda/bin/ipython
Now if I start iPython and import seaborn:
In [1]: import seaborn
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-1-085c0287ecb5> in <module>()
----> 1 import seaborn
ImportError: No module named seaborn
Also, pip is not working either. It's not reading packages from pip.
(me_dev)[me#ip-*****]$ pip install jellyfish
Requirement already satisfied (use --upgrade to upgrade): jellyfish in /home/me/miniconda/lib/python2.7/site-packages
Yet:
from jellyfish import jaro_winkler ImportError: No module named jellyfish
Edit:
sys.path looks like so in iPython
['',
'/home/me/miniconda/bin',
'/home/me/miniconda/lib/python27.zip',
'/home/me/miniconda/lib/python2.7',
'/home/me/miniconda/lib/python2.7/plat-linux2',
'/home/me/miniconda/lib/python2.7/lib-tk',
'/home/me/miniconda/lib/python2.7/lib-old',
'/home/me/miniconda/lib/python2.7/lib-dynload',
'/home/me/miniconda/lib/python2.7/site-packages/setuptools-19.6.2-py2.7.egg',
'/home/me/miniconda/lib/python2.7/site-packages',
'/home/me/miniconda/lib/python2.7/site-packages/cryptography-1.0.2-py2.7-linux-x86_64.egg',
'/home/me/miniconda/lib/python2.7/site-packages/IPython/extensions',
'/home/me/.ipython']
Here's my .bashsrc
(me_dev)[me#ip-**** ~]$ cat .bashrc
# .bashrc
# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
# added by Miniconda 3.16.0 installer
export PATH="/home/me/miniconda/bin:$PATH"
# User specific aliases and functions
source activate me_dev
# added by Miniconda2 3.19.0 installer
export PATH="/home/me/miniconda/bin:$PATH"
I think which python (and which ipython) should be pointing at ~/miniconda/envs/me_dev/bin/, not at ~/miniconda/bin/ directory. When you start an python session with those binaries, they can't see your seaborn library, which is probably only installed in the environment site packages.
I'm not sure exactly how you ended up in this state, but I would recommend trying to make a new environment and see if you end up pointed at the correct binaries.
The organization your .bashrc is the problem. You're activating your virtual environment, but then giving the main miniconda bin directory precedence in your $PATH, which has the effect of partially inactivating the virtual environment.