Jupyter kernel problem caused by pyenv+pipx - python

I installed two versions of python in pyenv, python3.7.5 and 3.8.6.
I usually use python3.8.6, so it is globally activated (pyenv global 3.8.6).
I also installed pipx by pip install pipx within python3.8.6 to install jupyter lab globally (pipx install jupyterlab), which is located in ~/.local/pipx/venvs/jupyterlab.
It seems that there is one jupyter kernel pre-installed.
$ jupyter kernelspec list
Available kernels:
python3 ~/.pyenv/versions/3.8.6/share/jupyter/kernels/python3
When I execute jupyter lab, this kernel is automatically selected.
However, in jupyter lab, I cannot import numpy, which is installed in python3.8.6.
[1]: import numpy as np
[1]: ---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-1-0aa0b027fcb6> in <module>
----> 1 import numpy as np
ModuleNotFoundError: No module named 'numpy'
This may be due to a PATH setting.
[2]: import sys
sys.path
[2]: ['~/',
'~/.pyenv/versions/3.8.6/lib/python38.zip',
'~/.pyenv/versions/3.8.6/lib/python3.8',
'~/.pyenv/versions/3.8.6/lib/python3.8/lib-dynload',
'',
'~/.local/pipx/venvs/jupyterlab/lib/python3.8/site-packages',
'~/.local/pipx/shared/lib/python3.8/site-packages',
'~/.local/pipx/venvs/jupyterlab/lib/python3.8/site-packages/IPython/extensions',
'~/.ipython']
~/.pyenv/versions/3.8.6/lib/python3.8/site-packages should be in sys.path. Why is not this path included?

You need to use pipx. Type "pipx inject jupyterlab numpy scipy matplotlib etc etc etc."
This will install those modules into the jupyterlab virtual environment.

Related

Unable to import 'pandas_profiling' module

I have installed 'pandas_profiling' through conda install -c conda-forge pandas-profiling in the base environment. I could see through the conda list that pandas_profiling has been installed correctly (snapshot attached),
When I try to import pandas_profiling I receive ModuleNotFoundError
import pandas_profiling
Traceback (most recent call last):
File "<ipython-input-4-60d2bac64bfc>", line 1, in <module>
import pandas_profiling
ModuleNotFoundError: No module named 'pandas_profiling'
Update: output of import sys; print(sys.path); print(sys.prefix)
['/home/user1/miniconda3/lib/python38.zip', '/home/user1/miniconda3/lib/python3.8', '/home/user1/miniconda3/lib/python3.8/lib-dynload', '', '/home/user1/miniconda3/lib/python3.8/site-packages', '/home/user1/miniconda3/lib/python3.8/site-packages/IPython/extensions', '/home/user1/.ipython']
/home/user1/miniconda3
This is a frequent issue, check out this entry in the FAQ.
Occasionally you will encounter this error if you import a package from the current notebook. It is important to ensure that the pip version is associated with the current Python kernel. That way, the installed packages can be used in the current notebook.
As detailed here, the shell environment and the Python executable are disconnected.
This should work for you
import sys
!{sys.executable} -m pip install pandas-profiling

ModuleNotFoundError: No module named 'pymc3'

I'm trying to import PyMC3 library in Jupyter Notebook's Python 3 kernel. I've already installed pymc3 initials in Anaconda Prompt (Miniconda3) using:
conda install theano
conda install pygpu
pip install pymc3
And when I try to import it in Jupyter Notebook:
import pymc3 as pm
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-1-1f817cf6472a> in <module>
1 # PyMC3 for Bayesian Inference
----> 2 import pymc3 as pm
ModuleNotFoundError: No module named 'pymc3'
Any solutions to this problem guys...
You have installed the pymc3 to other python environment that you are using to import it.
Check your python executable location
Inside your script, use
import sys
print(sys.executable)
To get the path to the python executable you are using. Lets say it is C:\python\python.exe.
Install the package
Then, install the package using
<path_to_python.exe> -m pip install pymc3
This is the bulletproof way installing packages with pip, when you have multiple python installations or virtual environments on your system.

GeoPandas not getting imported

I am trying to import the GeoPandas module (which I had recently installed on my laptop) in a Jupyter notebook but it returning this error:
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-1-a62d01c1d62e> in <module>
----> 1 import geopandas as gpd
ModuleNotFoundError: No module named 'geopandas'
I am certain that geopandas installed correctly. Can someone please suggest a fix?
It may not be installed in the right kernel for your jupyter notebook. One way to fix this is to run %pip install geopandas in a cell in your notebook. Then you can restart the kernel and you should be able to import it.
edit
just tried this out again and I had to add a --user to the magic command. The final command should look like %pip install geopandas --user
For me, I had to manually install the libraries as i could not import them although they were present in pip list of installed packages.
I went to https://www.lfd.uci.edu/~gohlke/pythonlibs/, where I downloaded the compatible version for GDAL, Pyproj, Fiona, Shapely and Geopandas.
Then I manually installed them using:
pip install the file path

pyfits and mayvi installed package in astroconda but not visible by spyder

I have installed astroconda. After activating astroconda by command source activate astroconda I typed spyder. Then in Spyder I imported some package import pyfits and from mayavi import mlab
I see this error:
Traceback (most recent call last): File "", line 1, in
ImportError: No module named pyfits
I checked with conda list and pyfits 3.3 is in installed package list. How tell Spyder to use this package?
Spyder it is not included by default in astroconda. So, when you load it, it uses the version in the Ananconda environment.
To solve this, you only have to install it in astroconda:
source activate astroconda
conda install spyder

Python - Can't import Seaborn

I'm running iPhyton Notebooks and I'm trying to import the Seaborn package. When I try to import it from the Terminal, it loads up fine, but when I import it through iPython Notebooks, it give me the following error.
I have even tried to re-install Seaborn using both Conda and Pip inside iPython notebooks and still it wont work.
Any idea why?
Thanks.
ImportError Traceback (most recent call last)
<ipython-input-1-417274a1ae6c> in <module>()
1 get_ipython().system(u'conda install seaborn')
2 get_ipython().system(u'pip install seaborn')
----> 3 import seaborn as sb
4
ImportError: No module named seaborn
Try this
import sys
print sys.path
sys.path.append('<path to package in your syste>')
import seaborn
In my Ubuntu 14.4LTS the packages get installed in the following folder
/usr/local/lib/python2.7/dist-packages
So I simply add the package path at run time
Donit install Ipython on all your system. Install it only in the environments you want it. Otherwise Ipython will look for modules in the default path instead of the environment's path.
This is probably where your ipython is looking:
/home/user/anaconda2/lib/python2.7/
It should be looking for modules here:
/home/user/anaconda2/envs/name-of-env/lib/python3.4/
To check the path you type:
import sys
sys.path
Try entering the following in your terminal:
conda install seaborn
It will install seaborn and make it available for you to import into your notebook
Open anaconda prompt and Type
pip install seaborn

Categories