ipython cannot search matplotlib while using tensorflow and jupyter also has import error - matplotlib

I am using python 2.7 in Ubuntu and recently updated tensorflow 0.12.1.
I installed jupyter today for my sample code of tf and I need to use matplotlib. It does not find module name matplotlib and ipython in tensorflow has same error.
1. How can I set path in virtualenv or ipython or jupyter?
After activate tensorflow, I need to use jupyter notebook.
This below in the script for error does not work.
import sys
sys.path.append('my/path/to/module/folder')
import module-of-interest
2. other information: My environments are below.
mickyefromsd#DEKSTOP~$source ~/tensorflow/bin/activate
When I find matplotlib by python script under TF condition and before TF activation, it has below;
/usr/lib/python2.7/dist-packages/matplotlib/
When I type 'which ipython', it has below (not by /usr/bin/ipython) ;
/home/mickeyfromd/tensorflow/bin/ipython
Btw, /tensorflow/lib/python2.7/site-packages/ it has ipython and jupyter.
(not in the same path of matplotlib)
3. My ipython under TF cannot find my existing matplotlib.
(tensorflow) mickeyfromd#DK-DESKTOP:~$ ipython
Python 2.7.11+ (default, Apr 17 2016, 14:00:29)
In [1]: import matplotlib ImportError: No module named matplotlib
4. I wanted to setup virtualenv, so I just run this
I followed this site. site:http://help.pythonanywhere.com/pages/IPythonNotebookVirtualenvs
(tensorflow) mickeyfromd#ipython kernelspec install-self --user
.....
Installed kernelspec python2 in /home/mickeyfromd/.local/share/jupyter/kernels/python2
(tensorflow) mickeyfromd#DK-DESKTOP:~$
I cannot move the the folder (in the second step)
How can I make ipython to have path for Matplotlib?

That import error is due to change in environment of the jupyter notebook. You might have installed the packages in one environment and you are running the jupyter notebook in another environment.
I have got two environments (envs) in my Anaconda folder ( I have Anaconda3 folder to be specific ).
(windows key+cmd ) -> open the windows command prompt run as administrator.
Activate (name of the environment) -> eg.: activate tensorflow-gpu
Start installing packages using conda install
Note: For each environment you need to install all the packages you want to use, separately using the same process. This solution is for windows users, might work for linux users not sure though.
Additionally to make sure your conda environment is up to date run:
conda update conda
conda update anaconda
check this out : https://pradyumnamajumder.wordpress.com/2017/09/30/solution-to-the-python-packages-import-error-in-jupyter/

Related

ModuleNotFoundError: No module named 'tensorflow' error in vscode terminal while running fine in wsl

I installed tensorflow using conda in wsl, and now I'm using it through Jupyter notebook and it's working fine. I also write codes in vscode and then run it through wsl using python3 filename.py and it works fine too.
the problem is when I try to run the code using the 'running' option of vscode and then I get the error ModuleNotFoundError: No module named 'tensorflow' in terminal.
I checked my environments and I only have 'base' environment so the packages should be installed there and I also tried 'conda ...' in vscode terminal and it didn't recognize conda either.
what can be the problem with my vscode?
also I have to say that numpy and matplotlib packages are running fine using vscode itself but tensorflow and some other modules are not identified
You need to create a new virtual environment in anaconda to install Tensorflow and can access the Visual Studio code from the same virtual environment.
Open anaconda prompt and type below code to create virtual environment
conda create -n tf tensorflow
conda activate tf
install TensorFlow in that "tf" virtual environment
pip install tensorflow
launch VS code by selecting the same "tf" virtual environment in anaconda navigator.
Now, type the below code to check if TensorFlow installed successfully in VS code.
import tensorflow as tf
print(tf.__version__)
Likewise, you can install other required packages in the same virtual environment.
pip install <package-name>

Tensorflow version different in conda env & jupyter notebook

I created a conda env and installed tensorflow 1.4
pip install tensorflow==1.4
This installed, and from within the environment, when i type
import tensorflow as tf
tf.__version__
it prints '1.4.0'
Now, i open jupyter notebook from the same environment, and when i type the same in the notebook, it gives me 1.10.0
How is this possible? Jupyter notebook should take the same libraries within which it is running. How does it have a different version of tensorflow
And btw I'm concerned with this because i wanna use 1.4.0 in my notebook
These are the questions I ask myself in situations like this, and they usually resolve my issue:
Was the kernel of my notebook running while I pipped? Shut down and restart.
Are pip and conda sharing PATH, et cetera? I don't take it for granted that my pip is modifying my conda env. You can check with these commands.
Your conda env is created, but is it active? You list created envs with these, and you can activate/deactivate with these.
Besides checking all of these, where am I launching jupyter from? I like to install jupyter with a simply pip install jupyter and that could add jupyter to the command line globally. Anaconda navigator/command line reliably launches the jupyter that lives in your Anaconda distribution.
To add to Charles Landau:
You likely installed tensorflow in parallel with pip and with conda.
Within your env, just check with pip list tensorflow and conda list tensorflow. Most likely you will see two different versions.
To be consistent do:
pip unistall tensorflow to keep only one.

How to connect tensoflow to jupyter notebook?

I have installed Anaconda with jupyter notebook in /home/serg/anaconda/bin and installed tensoflow in ./.local/lib/python3.5/site-packages/tensorflow. My operation system is Ubuntu 16.04.
Is it possible to use tensorflow in jupyter notebook via changing some configuration in anaconda or jupyter?
P.S.: I know it is possible for most python IDE, but I need to do it with jupyter notebook in anaconda.
Looks like you may have installed stuff in the wrong order.
I run TensorFlow in Jupyter notebook all the time, I don't get your issue? If you have installed Anaconda and it is active eg when you type python, you get the Anaconda version of python, you just install TensorFlow with Pip (following instructions at TF.. ) and the fire up jupyter notebook do your imports including TF and you should be off and going, nothing special required. If you did it in the wrong order, eg TF then Anaconda just make sure that Anaconda is your default interpeter (as above) and re-install TF with pip. If that does not seem to fix whatever issue you are having, post more info...
post your notebook info that tries to import TF?
I understood my problem: just used anaconda promts and installed tensoflow right here. Then I run jupyter notebook in anaconda promts and imported tensorflow.

Jupyter notebook can not import module installed in the same environment

I have created a virtual environment using conda and then I have installed tensorflow within this environment. I can import it when I run interactive python shell. However, I can not import the tensorflow module within jupyter notebook.
You need install Jupyter into same environment. Activate the environment with tensorflow and:
conda install jupyter
to make sure you use the corresponding jupyter start with:
python -m jupyter notebook
You probably have two installations of python on your box. When you run your notebook, run the following:
import sys
print(sys.path)
and then
import os
print(os.environ['PATH'])
print(os.environ['PYTHONPATH'])
Make sure they are pointing the location where you installed tensorflow. You can check where you installed tensorflow by running from the command line:
pip show -f tensorflow
or from the shell by running:
import tensorflow
tensorflow.__file__
As David said, you probably have two installation of python on you machine,
check sys.path from your environment in terminal and from your notebook, they should ideally point to your local environment.
To use jupyter notebook from your local environment install it and run kernel using
python -m jupyter notebook

Install Keras on Anaconda OSX

I am trying to install keras on an anaconda environment (OSX), because I want to use it with spyder - ipython. To do that I just used pip install keras (I already have tensorflow). After the installation when I call python 2.7 from the terminal, keras works fine. But, when I call python 3.5 or spyder and try to import keras I receive:
No module named 'keras'
I assume the issue might be with the PATHS on my MacBook, because which python returns
/usr/local/bin/python2.7
while which python3.5 (or spyder) returns
/Users/georgiospapadopoulos/anaconda/bin/python3.5
/Users/georgiospapadopoulos/anaconda/bin/spyder
Also, during pip install keras shows that
Requirement already satisfied: keras in /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages
My ~/.bash_profile contains
# added by Anaconda3 2.4.0 installer
export PATH="/Users/georgiospapadopoulos/anaconda/bin:$PATH"
# added by Anaconda3 4.2.0 installer
export PATH="/Users/georgiospapadopoulos/anaconda/bin:$PATH"
export CUDA_HOME=/usr/local/cuda
export DYLD_LIBRARY_PATH="$DYLD_LIBRARY_PATH:$CUDA_HOME/lib"
export PATH="$CUDA_HOME/bin:$PATH"
# Setting PATH for Python 2.7
# The original version is saved in .bash_profile.pysave
#PATH="/Library/Frameworks/Python.framework/Versions/2.7/bin:${PATH}"
PATH="/usr/local/bin/python:$PATH"
export PATH
You are probably mixing up the virtual environments. The best way to handle this is to create a vertual env in Anaconda - say, neural-net-venv, and then open the terminal for that venv, and install keras and other related modules. Then go back to Anaconda dash and select that venv as active environment to work on. Then select Jupyter and Spyder and run your imports.
Note that you also should not mix your Python versions - if you must work on Py2 and Py3 - create separate virtual environments for both, install keras, theanos/tensorflows separately in these environments, and you should be good to go.
I have run this setup on MacOS and it works like a charm.
For installing keras in Anaconda, the best and hassle free way is just use open the anaconda prompt and then type:
conda install keras
Keras runs on either tensorflow or theano backends. Once the keras install is complete, just open the python shell and type
>>>import keras
If some error is thrown, then there must be some problem with the backend. So just open the anaconda prompt, and type
conda import tensorflow
theano also can be used. Nevertheless tensorflow is the default one.
I wanted to insatll keras on Anaconda, tried the above approach, but it still did not work. Specifically, I started Anaconda Navigator and then opened a Mac OS terminal in the base environment. Then I followed the conda install commands for keras and tensorflow. It worked fine for keras. But with tensorflow, I got the following error message:
Downloading and Extracting Packages
_tflow_select-2.3.0 | 3 KB | ######################################################### | 100%
ChecksumMismatchError: Conda detected a mismatch between the expected content and downloaded content
for url 'https://conda.anaconda.org/Anaconda/osx-64/_tflow_select-2.3.0-mkl.tar.bz2'.
download saved to: /Users/dlin/opt/anaconda3/pkgs/_tflow_select-2.3.0-mkl.tar.bz2
expected sha256: cc155b27e7bf91ec5370ce1fd2d5fceccbf13ac19706229674ba971fa3751446
actual sha256: aad248699de112a7a5ead1695dfdf51b5693c2927303844b29dd7d9138dc95b9