Jupyter lab installing/importing packages - python

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

Related

Networkx & Jupyter Notebook using pip AttributeError

Hi everyone, I'm trying to create a network visualization with python using the networkx package. Unfortunately I'm getting a "AttributeError: module 'scipy.sparse' has no attribute 'coo_array'" at the end of my code. I've tried to fix it but every help in the internet points towards upgrading pip or conda which I've done and still get the same error. I'm using jupyter notebooks - any help would be greatly appreciated!
#Prerequisites
import sys
!{sys.executable} -m pip install --user networkx
!{sys.executable} -m pip install --user numpy
!{sys.executable} -m pip install --user pandas
pip install notebook --upgrade
conda upgrade notebook
import networkx as nx
import numpy as np
import pandas as pd
from pathlib import Path
from pandas import DataFrame
#Read in Source File - NB this must match the schema requirements
df_InputData = pd.read_excel("/Users/paulkruse/Desktop/FR/Fr1.xlsx")
Src_Column = 'Source ID'
Tgt_Column = 'Target ID'
print(df_InputData);
#Nodes are positioned using the Fruchterman-Reingold force-directed algorithm.
Q = nx.Graph()
arr_SrcTgt= np.array(df_InputData[[Src_Column, Tgt_Column]])
print(arr_SrcTgt);
Q.add_edges_from(arr_SrcTgt)
dict_Coords = nx.spring_layout(Q)
It seems that your scipy version is out of date. The most recent version 1.8.1 certainly has coo_array function. One way to resolve this should be to upgrade scipy. If you're doing this from inside jupyter notebook, then use
!pip install --user scipy==1.8.1
Make sure to restart kernel after installation.

Check if module is installed in Jupyter, rather than in kernel

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__)

Why does installing Tensorflow with conda ruin matplotlib?

Starting from a new anaconda environment, it looks like this (summarized, not a full paste):
[myenv]$ python
Python 3.8.8 ...
>> import matplotlib.pyplot as plt
>>
[myenv]$ conda install tensorflow-gpu
...
[myenv]$ python
>> import tensorflow
>> tensorflow.__version__
'2.4.1'
>> import matplotlib.pyplot as plt
ModuleNotFoundError: No module named 'matplotlib'
>>
[myenv]$ conda install matplotlib
...
[myenv]% python
>> import tensorflow
>> import matplotlib.pyplot as plt
<tons of traceback>
RunTimeError: the sip module implements API v12.0 to v12.4 but the PyQt5.core module requires API v12.5
How can I have tensorflow and matplotlib at the same time?
Do you have the complete traceback? Based on the Error you posted, it seems like its a dependency issue on the sip module. I'll recommend using you own virtualenv instead of anacondas.
FOR CREATING YOUR OWN VIRTUALENV:
open terminal
go to the directory you want your project to be
pip install virtualenv
virtualenv venv
source venv/bin/activate (for linux or mac)
pip install tensorflow
pip install matplotlib
pip install <whatever library you want>

How to import solutions in jupyter lab notebook

moduleNotFoundError Traceback (most recent call last)
<ipython-input-3-a733e16188a8> in <module>
1 import os
----> 2 import cv2
3 import time
4 import numpy as np
how to resolve this problem i have installed anaconda software
cv2 Is opencv
Since you are using conda (Anaconda) you can install it using conda install -c conda-forge opencv (More here)
(or !conda install -c conda-forge opencv if you want to install it from within a Jupiter Notebook)
(For others not using Anaconda) If you are not using Conda you should check the installation instructions for your operating system here

Error loading IPython notebook

Once I opened a notebook file with Jupyter (it asks me to convert the file) I never can open it in the standard IPython notebook anymore. I get the following error:
Error loading notebook
Bad Request
2014-12-21 04:13:03.203 [NotebookApp] WARNING | Unreadable Notebook: /FunIT experiment.ipynb global name 'NBFormatError' is not defined
WARNING:tornado.access:400 GET /api/notebooks/FunIT%20experiment.ipynb?_=1419153182928 (::1) 3.11ms referer=http://localhost:8888/notebooks/FunIT%20experiment.ipynb
An example of such corrupted files is this one:
https://www.dropbox.com/s/7io99wpoivxmc8f/FunIT%20experiment.ipynb?dl=0
How can I revert this process? I need to open these files with the standard IPython notebook (v2.2.0).
This problem has to do with incompatibility of the notebook and your IPython version. In my current version of IPython:
ipython --version
2.3.1
When I try to open the file (FunIT\ experiment.ipynb):
ipython notebook FunIT\ experiment.ipynb
I get the following error message
Error loading notebook
Unreadable Notebook: FunIT experiment.ipynb Unsupported nbformat
version 4
The error message indicates that the notebook format is not supported. Let's install the development version, https://github.com/ipython/ipython. I used virtual Environment, http://docs.python-guide.org/en/latest/dev/virtualenvs/, but it's not necessary.
Install virtual environment
pip install virtualenv
mkdir test
cd test
virtualenv venv
source venv/bin/activate
Ipython Notebook development installation
git clone --recursive https://github.com/ipython/ipython.git
cd ipython
pip install -e ".[notebook]" --user
Now I have the current development version.
ipython --version
3.0.0-dev
And I can open the file with ipython notebook
ipython notebook FunIT\ experiment.ipynb
Here is a snippet of the code:
import pandas as pd
import numpy as np
from pandas.tools.pivot import pivot_table
#from sklearn.metrics import roc_auc_score
import matplotlib.pyplot as plt
%pylab inline
#from sklearn.neighbors.kde import KernelDensity
import seaborn as sns
import scipy.stats as st
sns.set()
Upgrading IPython fixed it for me:
pip install ipython --upgrade
This works for me perfectly:
pip install jupyter

Categories

Resources