ModuleNotFoundError: No module named 'tensorflow_hub' - python

I followed instructions given in the TensorFlow website to install tensorflow_hub and installed it within a conda environment.
$ pip install "tensorflow>=2.0.0"
$ pip install --upgrade tensorflow-hub
I ran the above in anaconda prompt
But I'm still getting ModuleNotFoundError for 'tensorflow_hub'.
Any help here is appreciated. Thanks in advance

First thing
Check whether you have installed tensorflow_hub within that environment
conda list
If you can not find it there, maybe you have been installing it to another environment which does not matter, just install it again here.
pip install tensorflow_hub
You have probably done that so most likely you are using another kernel within your jupyter notebook, so either go to the environment of that kernel and install your package there. Or the preferred way, install your current environment yourenvironment as a new kernel and use that one in your jupyter notebook
python -m ipykernel install --user --name=yourenvironment
Now start your jupyter notebook and enjoy your package

Related

Cannot import tensorflow in vscode

I have installed tensorflow in a virtual environment and can import Tensorflow in jupyter notebook or terminal. I am now learning to use VScode, so I try to launch it in the jupyter notebook within the VScode, but Tensorflow cannot be imported.
It shows ModuleNotFoundError: No module named 'tensorflow', but I have installed it and it can be used in the terminal or jupyternotebook.
I also tried to define the path of the python interpreter, but it did not work. Could someone help me out? Tons of thanks.
Try:
This works for me.
pip --version
python -m pip install --upgrade pip
pip3 install tensorflow==2.0.0
pip --version
python -m pip install --upgrade pip
pip3 install tensorflow==2.8.0
Worked for me.

non-installed packages work when trying to import them in jupyter notebook

I need some help with some weird things happening in jupyter.
It all started when I was trying to install tweepy, the package was installed successfully but it doesn't work when I try to import it in jupyter and it outputs this error:
ImportError: No module named 'tweepy'
The package was installed via pip, first thing I checked was if the package was really installed in the environment or not..and it was. In short I tried the following:
I uninstalled the package with pip uninstall tweepy and then installed it again with pip install tweepy.
I then tried to install it with conda install -c conda-forge tweepy
I then tried to install it from inside the notebook with !conda install -y -c conda-forge tweepy
Well..nothing from the above did work, unfortunately. and it was getting really frustrating!
I found this post which suggest this:
import sys
!{sys.executable} -m pip install package
It did work. but here is the weird thing: whatever package I import ,even non-installed packages!, the cell just work without any errors! and when I check the list of packages installed in a totally new environment that I created to test this, which does not have any packages installed, with !conda list I get empty list as expected but still any package I import still work!
So, can anyone help me understand what just happened?
You should not use pip if you are using Anaconda. Create a virtual environment by typing: conda create -n yourenvname python=x.x anaconda where yourenvname is the name of your virtual environment and python=x.x is the version number of python you wish to use like python=3.8. Now after this activate you virtual environment. The type conda install package. And see if it works.

ModuleNotFoundError in jupyter notebook when importing a module after conda install

I'm trying to use conda to set up one of my projects. I installed openCV by conda install -c conda-forge opencv. When I run conda list, I can see openCV in the list. Running python -i and then import cv2 works, but when I open up Jupyter Notebook and navigate to that folder (I have to do it this way because running jupyter notebook in the directory also pulls up an error), and open up a notebook which imports cv2, I get an error. Why is this happening, and how would I solve it? Any kind of help will be greatly appreciated.
So as I said before, I wasn't able to start Jupyter Notebook from the command line, I had to start it from the start menu and navigate to my folder. Because of that, my notebook wasn't working in the conda environment that I created. I fixed that by running python -m ipykernal install --user --name <env_name> --display-name "<display_name>". I had to conda install ipykernel. It works now. Thanks for the other answers.
Usually that indicates that the notebook is running with a different Python or in a different environment from Python in the command prompt. Check sys.executable to see which Python it's running in, and sys.path to see where it's looking for imports
Everybody says that pip installing from notebook is not the best practice, but maybe for a fast try it would do the thing:
# Install a conda package in the current Jupyter kernel
import sys
!conda install --yes --prefix {sys.prefix} packagename
# Install a pip package in the current Jupyter kernel
import sys
!{sys.executable} -m pip install packagename
I used it from Install python packages on Jupyter Notebook and it worked for me.
Step 1: Activate environment before running
conda activate <environment-name>
Step 2: Install ipykernel
conda install -c anaconda ipykernel
Step 3: Add the conda environment to ipykernel
ipython kernel install --name <environment-name> --user
Step 4: Install your package
conda install -c conda-forge opencv
References:
How to add your Conda environment to your jupyter notebook in just 4 steps
Conda environments not showing up in Jupyter Notebook

No module named 'cv2' error in new environment

I have tried to rectify this issue by using
pip install opencv-python
pip install opencv-contrib-python
pip uninstall panda
pip install panda
conda install opencv-python
Some info is that im currently using python 3.6.10 and Windows 10.
opencv-python 4.2.0.32
numpy 1.18.1
panda 0.3.1
tensorflow-gpu 1.14.0
I created a new env but cant seem to import cv2 over on Jupyter Notebook. My earlier environment was able to do so. When i tried to pip install the opencv-python==4.1.2.30 (from the old environment's pip list), the problem was still there. Thank you for reading!
Firstly check which pip you are using
which pip # for linux
where pip # for windows
If path is in your new enviroment then try to reinstall it else first set PATH of new enviroment pip to the terminal/command prompt.
I found the elementary mistake I made. I forgot to download jupyter notebook in my env. As a result, I think it brought me to the another directory which doesn't have opencv. Thank you pygirl and nitr_himanshu for your help!

Tensorflow module not found by Jupyter notebook

Problem: Jupyter says "Module not found" when I try to import Tensorflow on Mac.
Background:
Virtualenv 16.7.9 installed & activated (venv)
Python 3.7.6 installed
PIP 19.3.1 installed
Tensorflow 2.1.0 installed using PIP as per official install instructions
At the command line, I can activate venv, start python3 then import tensorflow and display the tensorflow version correctly. But Tensorflow won't import from within Jupiter. And if I try to change the kernel within a Jupyter notebook, only Python3 is listed.
On other threads, people recommend using anaconda, but I've used PIP to install Tensorflow as recommended in the above link.
Any ideas?
Install a Jupyter Kernel
You need to install a kernel inside the env and then use Jupyter.
ipython kernel install --user --name=.venv
Then restart jupyter, click new, you should see .venv in your kernel list.

Categories