I am new to jupyter notebooks and virtualenvironment. I think that I am incurring in a really trivial problem.
I am trying to import matplotlib in a Jupyter notebook and the import works fine. However, it uses the wrong version of the library (installed also systemwide) and I don't know how to force it to use the one in the virtualenvironment.
In my virtualenvironment I want to use a most recent version of matplotlib and therefore I did
pip install --upgrade matplotlib in my virtualenvironment. The upgrade worked fine.
Now if I do pip show matplotlib in my virtualenvironment I get:
Name: matplotlib
Version: 2.2.5
...
Instead, if I do the same command in my home, I get:
Name: matplotlib
Version: 1.5.1
However, if in my jupyter notebook I do
import matplotlib
print ("matplotlib version:",matplotlib.__version__)
I get:
('matplotlib version:', '1.5.1')
Could you please help me understanding what I do wrong?
I found a solution to my problem. First of all, I read this long post that I suggest to everyone using conda or pip.
Then I understood that the shell environment is determined when the Jupyter notebook is launched, while the Python executable is determined by the kernel, and the two do not necessarily match.
I understood this by putting at the beginning of my jupyter notebook the following:
paths = !type -a python
for path in set(paths):
path = path.split()[-1]
print(path)
!{path} -c "import sys; print(sys.path)"
print()
!type python
import sys
sys.executable
!pip show matplotlib
It showed me that I was still using an older version of the matplotlib library.
Therefore I added
!{sys.executable} -m pip install --upgrade matplotlib
Restarted my jupyter kernel and this solved my problem.
I hope this solution will help someone else in the same situation.
Related
I have been using Jupyter for some time now and it has worked just fine. I have Jupyter and Python installed via Homebrew. I am running on MacOS.
Yesterday, I ran the command brew upgrade and now my Jupyter notebook is unable to find any of the installed python packages. I will use Numpy as the example.
When inside of a Jupyter notebook, I try to do
import numpy
I get the message:
ModuleNotFoundError: No module named 'numpy'
If, however, I launch python in a terminal window, then I can import Numpy without issue.
I first checked that the package was installed correctly by re-issuing the install command
brew install numpy
which outputs:
Warning: numpy 1.18.4 is already installed and up-to-date
To reinstall 1.18.4, run `brew reinstall numpy`
I also ran
pip install numpy
and got:
Requirement already satisfied: numpy in /usr/local/lib/python3.7/site-packages (1.18.4)
Now, this is where I got confused because I expected the path to point to something like /usr/local/Cellar/, so I checked the path inside of the Jupyter notebook:
import sys
sys.path
which outputs:
['/Users/kseuro/Dropbox/Dev/',
'/usr/local/Cellar/jupyterlab/2.1.2/libexec/lib/python38.zip',
'/usr/local/Cellar/jupyterlab/2.1.2/libexec/lib/python3.8',
'/usr/local/Cellar/jupyterlab/2.1.2/libexec/lib/python3.8/lib-dynload',
'/usr/local/opt/python#3.8/Frameworks/Python.framework/Versions/3.8/lib/python3.8',
'',
'/usr/local/Cellar/jupyterlab/2.1.2/libexec/lib/python3.8/site-packages',
'/usr/local/Cellar/jupyterlab/2.1.2/libexec/lib/python3.8/site-packages/IPython/extensions',
'/Users/kseuro/.ipython']
Ok, so Homebrew wants Jupyter to use Python3.8? So I tried brew switch python 3.8 and got:
Error: python does not have a version "3.8" in the Cellar.
python's installed versions: 3.7.7
I feel like I'm out of my depth now and need help figuring out what to do next. I don't want to start by just changing paths around.
Suggestions? Thanks so much.
I figured out what to do — posting the solution for my future self and others who may stumble upon this.
Since Jupyerlab is in its own Cellar, the Python packages need to end up in the
/usr/local/Cellar/jupyterlab/x.y.z/libexec/lib/python3.x/site-packages
directory, where x, y, z are integers, so that the Jupyter kernel can find them.
You can do this by calling:
import sys
!{sys.executable} -m pip install 'package-name'
inside of the Jupyer notebook.
All is well, again.
So I am trying to import zipline with anaconda. Though due to the fact that zipline requires python 3.5 I created an environment with python 3.5 in it. I did conda install zipline in the environment's terminal and it says it has successfully installed, then when I open python within the environment, and I import zipline no errors come up. I also have jupyter notebook installed in the env, when I import zipline in ipython, it causes no errors. But then when I open Jupiter notebook through the environment, then open a new kernel and try to import zipline, it says the module does not exist (I have even imported numpy and other things that are also downloaded and they work fine in the same jupyter notebook tab, and I also I am opening JN through the 3.5 env terminal not the base terminal). Please help, thank you.
EDIT: An example of the code could be
import numpy as np
np.sqrt(16)
This will return:
4.0
Then if I import zipline:
!conda install -c quantopian zipline
import zipline as zp
import numpy as np
np.sqrt(16)
There is no output, it just skips to the next cell without an error popping up, and also the I[1] part of the cell changes to I[*] instead of I[2]
(I am new at python so probably I am doing something wrong)
what I get from pip show zipline in the terminal
Name: zipline
Version: 1.3.0
Summary: A backtester for financial algorithms.
Home-page: http://zipline.io
Author: Quantopian Inc.
Author-email: opensource#quantopian.com
License: Apache 2.0
Location: c:\users\benito\anaconda3\envs\env_zipline\lib\site-packages
Requires: pip, setuptools, Logbook, pytz, numpy, requests-file, scipy, pandas, pandas-datareader, patsy, statsmodels, python-dateutil, six, requests, Cython, cyordereddict, bottleneck, contextlib2, decorator, networkx, numexpr, bcolz, click, toolz, multipledispatch, MarkupSafe, Mako, sqlalchemy, alembic, sortedcontainers, intervaltree, lru-dict, empyrical, tables, trading-calendars
The first thing is it is required to install jupyter notebook for every environment. So at first make sure that you have installed jupyter notebook correctly on the required environment. If you have installed it correctly then open jupyter notebook and in a code cell write any one of the following commands and execute the cell.
!conda install -c quantopian zipline -y
or
!conda install -c quantopian/label/ci zipline -y
After this execution import the library again.
It checks the lib folder where my seaborn stuff is, but still error._.
Hi,
I have looked at other posts, but most seemed to be dealing with Jupyter notebooks, which I'm not. I was wondering how to get to use Seaborn in the basic Python IDE or in PyCharm. I read about filepath collisions stuff, but not too clear on that front.
I'm using Python 3.6 right now.
Thanks for any help!
When dealing with version ambiguity, remember that pip is a python module. Once you're confident that python is the python installation that your IDE is running, run
python --version
python -m pip install seaborn
>pip3 may be pointing to an old or different python installation.
import pip
pip.main(['install','seaborn'])
From: https://stackoverflow.com/a/49391839
If you're doing in jupyter notebook Try doing this:
!conda install -c anaconda seaborn -y
Try running it in the terminal, it will work. But while running the command your pwd should be in the virtual environment in activated form
$ sudo apt-get install -y python3-seaborn
Try running this in a command line 'pip install seaborn'
https://seaborn.pydata.org/installing.html#installing
In PyCharm IDE, we can import the downloaded libraries, and that's what I did. Still, I have no clue on how to resolve this issue on Python IDE, but as of now, it's working on PyCharm for me.
Conda install seaborn
worked in my case.
If you are using jupyter notebook following command will solve the issue
!pip install seaborn
Jupyter Code-Cell:
%%bash
pip install seaborn
Since you are using python 3, try to open with idle3 from terminal
Tried importing seaborn in Pycharm with the proper configuration thing, and it works. I still don't know why the regular Python IDE doesn't work even though one of the sys.path folder it checks contains the seaborn folder, but oh well.
Thanks for all the replies!
Maybe you should try “python —version “to check if you’re using the right version of python in cmd. Sometimes it happens that there are multiple versions installed and it sometimes picks the wrong one.
Also it can happens that python hasn’t the rights to use the module. Then you should create your file and run it with “python [path of file]”
Just found a new method to install all important libraries.
Open command prompt:
Pip install pyforest
All the most important libraries got installed.
My fix (Same import error but on Jupyter Notebook). The import sequence:
import numpy as np
import seaborn as sns
%matplotlib inline
import matplotlib.pyplot as plt
Importing Matplotlib before seaborn can lead to an import error (I have no idea why).
The Seaborn official document also shows this:
https://seaborn.pydata.org/installing.html
If you're using Jupyter notebook try to run this_ _
** pip install seaborn --user**
I'm trying to parse the following line of code in an iPython notebook.
from Ipython.display import display, Image
I get the following error,
ModuleNotFoundError: No module named 'Ipython'
When I run pip3 install Ipython
Here's what I get.
Requirement already satisfied: Ipython in ./.envs/dl/lib/python3.6/site-packages
I'm running the code in the same virtual env in which ipython is installed. What am I missing. My python version is 3.6.
Its from IPython.display import display, Image
'P' also caps in IPython
I had this problem too. You have to pay attention to which python version you are using, which pip version, and which environment too. I made a stupid mistake at first and installed TensorFlow on a virtualenv, then tried to call IPython which means my function and installation had different paths.
I recommend using "python -m pip install"
I have recently started using Python 3.5 and Anaconda on my Windows pc. I am trying to plot a map. However, When I am in my Jupyter notebook and i type the command
import mpl_toolkits.basemap
I get an error message saying 'no module name' 'mpl_toolkits.basemap'
However, I have the module downloaded and in the same C:\Users\Geena file as my .matplotlib, .ipython, .jupyter files, etc.
Anyone know how I can fix this?
I've had this issue with anaconda on my windows 7.
I found the way to fix it with python 3.5:
You need to run with administrator rights "Anaconda Prompt" and in "Anaconda Prompt" run following command:
conda install -c conda-forge basemap-data-hires=1.0.8.dev0
, it will show new packages that you need to install and will ask you to install it - say 'Yes'.
After that new packages will be installed and the issue "import mpl_toolkits.basemap" will be fixed.
Thank you.
Currently, basemap is not compatible with python 3 for windows users. So, if you try conda install basemap and you have python 3 installed in windows, you'll see a message pointing out that a conflict was found with python 3.
I solved this by installing a python 2.7 environment. Try this:
http://conda.pydata.org/docs/py2or3.html
Then you just activate the python 2 environment. For example: activate py27 (py27 is the identifier of my python 2.7 environment).
After that, you can run conda install basemap with no conflict.
I'm using python 3.6.4 on Windows 7 Family Premium (32bit).
Because I was a bit frustrated by the message "no module named 'mpl_toolkits.basemap'", I searched for and tried a dozen of solutions without success : various versions, building from source, problems with VS version, nmake, ... You all know what I mean ;-)
I finally found a quite simple solution that works perfectly well for me :-) Here it is !
from here I downloaded basemap‑1.1.0‑cp36‑cp36m‑win32.whl
I changed the current dir to my download dir
I installed the wheel with python -m pip basemap‑1.1.0‑cp36‑cp36m‑win32.whl
I did the same for matplotlib‑2.2.3‑cp36‑cp36m‑win32.whl
You DO read the versions correctly : matplotlib 2.2.3 and basemap 1.1.0
Everything works fine for me and I finally can plot OSM POI's on a map of Belgium, without any 'trickery' at import :
import requests # to fetch OSM data
import json # to get the response
from mpl_toolkits.basemap import Basemap # ... Belgium is there !
import numpy as np # for arrays
import matplotlib.pyplot as plt # to build the populated map
Big big thanks to Christoph Gohlke (Danke Dir Christoph !) who did all the wonderful job !
When you have Anaconda, you don't download modules anywhere. In your command prompt, you type
conda install basemap
and it is installed with all its dependencies.
Anaconda requires an unusual install command for basemap 1.0.7.
https://anaconda.org/anaconda/basemap
To install this package with conda run:
conda install -c https://conda.anaconda.org/anaconda basemap
I just had this issue as well. All you need to do is update matplotlib by doing the following:
pip install --upgrade matplotlib
mpl_toolkits is part of matplotlib and just needs to be updated.
If you're using anaconda, the easiest thing to do is described here: in the conda prompt (as admin), type conda install -c anaconda basemap.
For people of the future : "Basemap is deprecated in favor of the Cartopy project."
https://matplotlib.org/basemap/users/intro.html#cartopy-new-management-and-eol-announcement