Jupyter Notebook creates new line instead of running block - python

I recently downloaded Jupyter Notebook to run some simple python code but whenever I try to run it by pressing shift and enter, all it does is give me a new line. I tried to run
import sys
import tensorflow.keras
import pandas as pd
import sklearn as sk
import tensorflow as tf
import platform
print(f"Python Platform: {platform.platform()}")
print(f"Tensor Flow Version: {tf.__version__}")
print(f"Keras Version: {tensorflow.keras.__version__}")
print()
print(f"Python {sys.version}")
print(f"Pandas {pd.__version__}")
print(f"Scikit-Learn {sk.__version__}")
gpu = len(tf.config.list_physical_devices('GPU'))>0
print("GPU is", "available" if gpu else "NOT AVAILABLE")
into a notebook, but it doesn't work. I also tried to run it on vs code but when I do, the terminal says...
import tensorflow.keras
ModuleNotFoundError: No module named 'tensorflow'
Please help! I have no clue what I am supposed to do!

If you install "tensorflow" in Jupyter Notebook
I will NOT be installed in python environment.
If you want to run it in vscode. You ought to install "tensorflow" with pip in the python environment.
Just open the "cmd" (not the one in Jupyter) and use pip to install
pip install tensorflow

Related

No module named 'tensorflow.contrib' error [duplicate]

I installed Tensorflow on Windows, using pip (Python 3.5)
C:\> pip install --upgrade https://storage.googleapis.com/tensorflow/windows/cpu/tensorflow-0.12.1-cp35-cp35m-win_amd64.whl
Afterwards, I went to my command line and succesfully ran:
>> python
>> import tensorflow as tf
However, then I opened up Python IDLE (3.5) and tried to import tensorflow, with no luck:
ImportError: No module named 'tensorflow'
Same story with numpy. My Numpy and tensorflow packages are installed here:
AppData\Local\Programs\Python\Python35\lib\site-packages
What am I doing wrong?
I also faced the same problem in Python version 3.6. Finally, I managed to solve it by accessing Windows Command Prompt in Admin mode and type
to enter to python IDLE
python
and successfully import tensorflow
import tensorflow as tf
and checked the version with
print(tf.version)
I think you got your answer. As asked here is how you can check your python version.
import sys
print (sys.version)

Spyder imports numpy from the wrong python environment

I want to import opencv in spyder using the IPython console. However, when I try to do this, I get an import error on numpy.
Platform: Jetson xavier
Spyder version: 3.2.6
I'm using 'virtualenv' to manage my virtual environment.
What works:
I can import cv2 in python using a bash terminal. Steps to reproduce:
Activate my virtual environment by running source my_env/bin/activate
Start python by running python
Import opencv by running import cv2
Success
I use spyder as my IDE and I want to import opencv in a python script run by spyder. However, when I run import cv2 I get ImportError: numpy.core.multiarray failed to import. Steps to reproduce:
In spyder in preferences set the Python interpreter to the one from my virtual environment (~/my_env/bin/python3)
Restart IPython console
Type import cv2 in the IPython console
I get the ImportError: numpy.core.multiarray failed to import
I already tried updating numpy, as recommended by ImportError: numpy.core.multiarray failed to import but this did not work.
Update
In spyder I can import numpy manually, and using inspect.getfile(numpy) tells me that spyder imported numpy from /usr/lib/python3/dist-packages/numpy/__init__.py, While it sohuld import numpy from ~/my_env/lib/python3.6/site-packages/numpy. Why is spyder importing numpy from the wrong environment?

Jupyter Notebook Tensorflow MNIST import Error: ImportError: cannot import name batching

I tried to execute this line in my application in Jupyter notebook.
from tensorflow.contrib.learn.python.learn.datasets.mnist import read_data_sets
I get the following error:
.conda/envs/py27/lib/python2.7/site-packages/tensorflow/contrib/__init__.py in <module>()
23
24 # Add projects here, they will show up under tf.contrib.
---> 25 from tensorflow.contrib import batching
26 from tensorflow.contrib import bayesflow
27 from tensorflow.contrib import cloud
ImportError: cannot import name batching
I do not get this error if I ran it like this:
python -c "from tensorflow.examples.tutorials.mnist import input_data"
Can someone tell me what's causing the error in Jupyter Notebook
I have run into similar issue with importing libraries from jupyter notebook, and the problem has always been that I forgot to install jupyter in my new conda environment, but jupyter is installed in the root environment.
Based on the error given, make sure you have jupyter installed within the py27 conda environment. If it not installed, it will default to open the one in the root environment.
So in the terminal:
source activate py27 # activate py27 if on windows
conda install jupyter

cannot import tensorflow - "Import Error: cannot import name 'self_check' "

I'm trying to use tensorflow-gpu. Using Anaconda, I installed the libraries and active both (tensorflow and tensorflow-gpu) I have also installed Keras in this anaconda environment as well
Next I launch Spider IDE within my Anaconda environment and run my py script, that is when I get the following error:
cannot import tensorflow - Import Error: cannot import name 'self_check'
I'm totally lost. Any recommendations?
Using Python 3.5
Thanks
pip install --upgrade tensorflow
or
you can uninstall the tensorflow and re-install it.

ModuleNotFoundError: No module named 'tensorflow'?

I got this error in Jupyter Notebook.
this may duplicate post but no any proper answer found
Neither below links helped me to solve this.
No module named tensorflow in jupyter
No module named tensor flow -- iPython notebook
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-1-b88de1945047> in <module>()
1 get_ipython().magic('matplotlib inline')
2 import numpy as np
----> 3 import tensorflow as tf
4 from tensorflow.contrib import rnn
5 import data_loader
ModuleNotFoundError: No module named 'tensorflow'
B:S -
enter image description here
I faced the same problem at first. I realized that in the conda-->envs -->
tensorflow-->Scripts-->idle.exe, is the python 3.5.5 shell. Where you can import tensorflow without any problems. So, I chaged the directory as C:\Users\Temp\Anconda3..... and then activated and deactived the tensorflow once. In the next step I entered the python mode and import tensorflow as tf worked right.
I had the same problem and I think I've found the solution!
See, I had installed both CPU and GPU versions of Tensorflow at the same time which apparently is wrong. Then, I uninstalled the GPU version, it didn't work this time either. So I uninstalled both of them and reinstalled the CPU version. It's working now.
BTW I'm on Win10(1803) and python 3.6.5 (not using anaconda.)
You can execute the following instructions and command to solve the issue: (do not include inverted commas)
Open Anaconda Prompt
type: "create --name py3-TF2.0 python = 3"
press "y" to continue
After successful completion:
type "conda activate py3-TF2.0"
type "pip install matplotlib"
type "pip install tensorflow==2.1.0"
type "conda deactivate"
type "pip install ipykernel"
type "conda install nb_conda_kernels"
Now exit the Anaconda prompt and open the Anaconda Navigator application. Switch to "py3-TF2.0" from the base environment using the Environment tab. Install Jupyter in the specified environment.
After successful installation, open Jupyter notebook and switch to "py3-TF2.0" kernel using Kernel Tab.
You're good to go for using TensorFlow 2.
Make Sure you use Anaconda 2020.02 or lower version.
As new version does not support TensorFlow 2.

Categories