Why can I not import tensorflow into my Jupyter notebook? - python

I have run a simple neural network through pycharm, pointing it to a conda virtual environment with tensorflow and dependencies installed. When I launch a jupyter notebook from that same env the exact code aborts when I try and import tensorflow.
This is the code at the top of the notebook:
import tensorflow as tf
from tensorflow import keras
.....
I expected tensorflow and keras to load ok.
Instead I got:
`---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-3-2be7691b079e> in <module>
----> 1 import tensorflow as tf
2 from tensorflow import keras
...
ModuleNotFoundError: No module named 'tensorflow'
I added a line to list installed modules at the top of the notebook in case it was looking somewhere without tensorflow installed :
!conda list
`
And got:
# packages in environment at /home/user/anaconda3/envs/tf-cert:
#
# Name Version Build Channel
......
cudnn 8.2.1 cuda11.3_0
.....
keras 2.9.0 pypi_0 pypi
keras-preprocessing 1.1.2 pypi_0 pypi
libclang 14.0.6 pypi_0 pypi
libedit 3.1.20210910 h7f8727e_0
.......
scipy 1.7.3 pypi_0 pypi
setuptools 65.5.0 py38h06a4308_0
six 1.16.0 pypi_0 pypi
sqlite 3.33.0 h62c20be_0
tensorboard 2.9.1 pypi_0 pypi
tensorboard-data-server 0.6.1 pypi_0 pypi
tensorboard-plugin-wit 1.8.1 pypi_0 pypi
tensorflow 2.9.0 pypi_0 pypi
tensorflow-datasets 4.6.0 pypi_0 pypi
tensorflow-estimator 2.9.0 pypi_0 pypi
tensorflow-io-gcs-filesystem 0.28.0 pypi_0 pypi
tensorflow-metadata 1.11.0 pypi_0 pypi
termcolor 2.1.1 pypi_0 pypi
tk 8.6.12 h1ccaba5_0

I faced a similar problem while using VS Code. I had created a virtual environment using venv, but when I selected that environment, I wasn't able to import any libraries even though I had installed all the required ones.
I simply deleted the environment and created a new one using -
conda create --prefix ./env pandas numpy ... in the terminal/command prompt. I then restarted VS Code and selected this particular environment. That fixed the issue. The python version and the library versions need to be in sync with each other to work properly. Hope this works for you too.
Edit:
I was again facing the same problem when I created a new environment. I then selected the python environment where I had installed the Tensorflow library from the top right corner in Jupyter notebook (underlined red). That solved the issue!

Related

Why does Anaconda install pytorch cpuonly when I install cuda?

I have created a Python 3.7 conda virtual environment and installed the following packages using this command:
conda install pytorch torchvision torchaudio cudatoolkit=11.3 matplotlib scipy opencv -c pytorch
They install fine, but then when I come to run my program I get the following error which suggests that a CUDA enabled device is not found:
raise RuntimeError('Attempting to deserialize object on a CUDA '
RuntimeError: Attempting to deserialize object on a CUDA device but torch.cuda.is_available() is False. If you are running on a CPU-only machine, please use torch.load with map_location=torch.device('cpu') to map your storages to the CPU.
I have an NVIDIA RTX 3060ti GPU, which as far as I am aware is cuda enabled, but whenever I go into the Python interactive shell within my conda environment I get False when evaluating torch.cuda.is_available() suggesting that perhaps CUDA is not installed properly or is not found.
When I then perform a conda list to view my installed packages:
# packages in environment at /home/user/anaconda3/envs/FGVC:
#
# Name Version Build Channel
_libgcc_mutex 0.1 main
_openmp_mutex 4.5 1_gnu
blas 1.0 mkl
brotli 1.0.9 he6710b0_2
bzip2 1.0.8 h7b6447c_0
ca-certificates 2021.10.26 h06a4308_2
cairo 1.16.0 hf32fb01_1
certifi 2021.10.8 py37h06a4308_2
cpuonly 1.0 0 pytorch
cudatoolkit 11.3.1 h2bc3f7f_2
cycler 0.11.0 pyhd3eb1b0_0
dbus 1.13.18 hb2f20db_0
expat 2.4.4 h295c915_0
ffmpeg 4.0 hcdf2ecd_0
fontconfig 2.13.1 h6c09931_0
fonttools 4.25.0 pyhd3eb1b0_0
freeglut 3.0.0 hf484d3e_5
freetype 2.11.0 h70c0345_0
giflib 5.2.1 h7b6447c_0
glib 2.69.1 h4ff587b_1
graphite2 1.3.14 h23475e2_0
gst-plugins-base 1.14.0 h8213a91_2
gstreamer 1.14.0 h28cd5cc_2
harfbuzz 1.8.8 hffaf4a1_0
hdf5 1.10.2 hba1933b_1
icu 58.2 he6710b0_3
imageio 2.16.0 pypi_0 pypi
imageio-ffmpeg 0.4.5 pypi_0 pypi
imutils 0.5.4 pypi_0 pypi
intel-openmp 2021.4.0 h06a4308_3561
jasper 2.0.14 hd8c5072_2
jpeg 9d h7f8727e_0
kiwisolver 1.3.2 py37h295c915_0
lcms2 2.12 h3be6417_0
ld_impl_linux-64 2.35.1 h7274673_9
libffi 3.3 he6710b0_2
libgcc-ng 9.3.0 h5101ec6_17
libgfortran-ng 7.5.0 ha8ba4b0_17
libgfortran4 7.5.0 ha8ba4b0_17
libglu 9.0.0 hf484d3e_1
libgomp 9.3.0 h5101ec6_17
libopencv 3.4.2 hb342d67_1
libopus 1.3.1 h7b6447c_0
libpng 1.6.37 hbc83047_0
libstdcxx-ng 9.3.0 hd4cf53a_17
libtiff 4.2.0 h85742a9_0
libuuid 1.0.3 h7f8727e_2
libuv 1.40.0 h7b6447c_0
libvpx 1.7.0 h439df22_0
libwebp 1.2.0 h89dd481_0
libwebp-base 1.2.0 h27cfd23_0
libxcb 1.14 h7b6447c_0
libxml2 2.9.12 h03d6c58_0
lz4-c 1.9.3 h295c915_1
matplotlib 3.5.0 py37h06a4308_0
matplotlib-base 3.5.0 py37h3ed280b_0
mkl 2021.4.0 h06a4308_640
mkl-service 2.4.0 py37h7f8727e_0
mkl_fft 1.3.1 py37hd3c417c_0
mkl_random 1.2.2 py37h51133e4_0
munkres 1.1.4 py_0
ncurses 6.3 h7f8727e_2
networkx 2.6.3 pypi_0 pypi
ninja 1.10.2 py37hd09550d_3
numpy 1.21.2 py37h20f2e39_0
numpy-base 1.21.2 py37h79a1101_0
olefile 0.46 py37_0
opencv 3.4.2 py37h6fd60c2_1
openssl 1.1.1m h7f8727e_0
packaging 21.3 pyhd3eb1b0_0
pcre 8.45 h295c915_0
pillow 8.4.0 py37h5aabda8_0
pip 21.2.2 py37h06a4308_0
pixman 0.40.0 h7f8727e_1
py-opencv 3.4.2 py37hb342d67_1
pyparsing 3.0.4 pyhd3eb1b0_0
pyqt 5.9.2 py37h05f1152_2
python 3.7.11 h12debd9_0
python-dateutil 2.8.2 pyhd3eb1b0_0
pytorch 1.7.0 py3.7_cpu_0 [cpuonly] pytorch
pywavelets 1.2.0 pypi_0 pypi
qt 5.9.7 h5867ecd_1
readline 8.1.2 h7f8727e_1
scikit-image 0.19.1 pypi_0 pypi
scipy 1.7.3 py37hc147768_0
setuptools 58.0.4 py37h06a4308_0
sip 4.19.8 py37hf484d3e_0
six 1.16.0 pyhd3eb1b0_1
sqlite 3.37.2 hc218d9a_0
tifffile 2021.11.2 pypi_0 pypi
tk 8.6.11 h1ccaba5_0
torchaudio 0.7.0 py37 pytorch
torchvision 0.8.1 py37_cpu [cpuonly] pytorch
tornado 6.1 py37h27cfd23_0
typing_extensions 3.10.0.2 pyh06a4308_0
wheel 0.37.1 pyhd3eb1b0_0
xz 5.2.5 h7b6447c_0
zlib 1.2.11 h7f8727e_4
zstd 1.4.9 haebb681_0
There seems to be a lot of things saying cpuonly, but I am not sure how they came about, since I did not install them.
I am running Ubuntu version 20.04.4 LTS
I ran into a similar problem when I tried to install Pytorch with CUDA 11.1. Although the anaconda site explicitly lists a pre-built version of Pytorch with CUDA 11.1 is available, conda still tries to install the cpu-only version. After a lot of trial-and-fail, I realize that the packages torchvision torchaudio are the root cause of the problem. So installing just PyTorch would fix this:
conda install pytorch cudatoolkit=11.1 -c pytorch -c nvidia
You can ask conda to install a specific build of your required package.pytorch builds supporting cuda have the phrase cuda somewhere in their build string, so you can ask conda to match that spec. For more information, have a look at conda's package match spec.
$ conda install pytorch=*=*cuda* cudatoolkit -c pytorch
I believe I had the following things wrong that prevented me from using Cuda. Despite having cuda installed the nvcc --version command indicated that Cuda was not installed and so what I did was add it to the path using this answer.
Despite doing that and deleting my original conda environment and using the conda install pytorch torchvision torchaudio cudatoolkit=11.3 matplotlib scipy opencv -c pytorch command again I still got False when evaluating torch.cuda.is_available().
I then used this command conda install pytorch torchvision torchaudio cudatoolkit=10.2 matplotlib scipy opencv -c pytorch changing cudatoolkit from verison 11.3 to version 10.2 and then it worked!
Now torch.cuda.is_available() evaluates to True
Unfortunately, Cuda version 10.2 was incompatible with my RTX 3060 gpu (and I'm assuming it is not compatible with all RTX 3000 cards). Cuda version 11.0 was giving me errors and Cuda version 11.3 only installs the CPU only versions for some reason. Cuda version 11.1 worked perfectly though!
This is the command I used to get it to work in the end:
pip install torch==1.9.0+cu111 torchvision==0.10.0+cu111 torchaudio==0.9.0 -f https://download.pytorch.org/whl/torch_stable.html
If there is nothing wrong with your nvidia driver setup, maybe you are missing nvidia channel from installation arguments. The pytorch documentation helped me generate this command that eventually solved my problem:
conda install pytorch torchvision torchaudio pytorch-cuda=11.6 -c pytorch -c nvidia
Installing jupyter inside conda's virtual environment solve my issue. I was having the same issue, even pytorch with cuda is installed and !nvidia-smi showing GPU , but while trying to access jupyter notebook , it was showing only cpu.
While I was trying from command line torch is finding CUDA but from jupyter is not showing, So I just pip install jupyter on virtual environment of conda and after that problem is solved .
Use the exact script from the Pytorch website works for me:
conda install pytorch==1.12.1 torchvision==0.13.1 torchaudio==0.12.1 cudatoolkit=10.2 -c pytorch
But if I use
conda install pytorch==1.12.1 torchvision==0.13.1 cudatoolkit=10.2 -c pytorch
no installing torchaudio, it will install cpu versions of pytorch and torchvision. I found it interesting and don't know why.

Pandas will not import because of "dateutil.tz" inside git repo, runs fine outside

I've tried uninstalling and reinstalling Anaconda. and upgrading pandas and most other questions that ask this question. I am working with a brand new installation of Anaconda. with python 3.7, Why is pandas not importing normally?
I have tried manually installing pip install pytz --upgrade and pip install python-datutil --upgrade to no avail. However, After doing these two commands I can now import pandas in the terminal but not in my script where i need it.
The Script where i am trying to import pandas is inside a git repo that perhaps needs to be reconfigured. I suspect that might be the issue. but im not sure how to change how python interacts with pandas from within git.
Here is the stacktrace...
File "C:\Users\jgreaves\Anaconda3\lib\site-packages\pandas\__init__.py", line 37, in <module>
f"C extension: {module} not built. If you want to import "
ImportError: C extension: No module named 'dateutil.tz'; 'dateutil' is not a package not built. If you want to import pandas from the source directory, you may need to run 'python setup.py build_ext --inplace --force' to build the C extensions first.```
Here is the config of my virtual environment
```# Name Version Build Channel
ca-certificates 2020.1.1 0 anaconda
certifi 2020.4.5.2 py38_0 anaconda
numpy 1.18.5 pypi_0 pypi
openssl 1.1.1g he774522_0 anaconda
pandas 1.0.4 pypi_0 pypi
pip 20.0.2 py38_3 anaconda
pyodbc 4.0.30 pypi_0 pypi
python 3.8.3 he1778fa_0 anaconda
python-dateutil 2.8.1 pypi_0 pypi
pytz 2020.1 pypi_0 pypi
regex 2020.6.8 pypi_0 pypi
setuptools 47.1.1 py38_0 anaconda
six 1.15.0 pypi_0 pypi
sqlite 3.31.1 he774522_0 anaconda
vc 14.1 h0510ff6_4 anaconda
vs2015_runtime 14.16.27012 hf0eaf9b_2 anaconda
wheel 0.34.2 py38_0 anaconda
wincertstore 0.2 py38_0 anaconda ```
So after having a friend of mine take a look at my directory. We discovered that I had a file in my working directory called "dateutil.py" which was supposed to be a module for my code that I very uncleverly named. This was what was causing the issue. I have since renamed the file and everything is working fine now.

No module named tensorflow while i installed it with pip

I'm new to Tensorflow and machine learning. I'm following some tutorials on my PC, and everything worked just fine so far, importation and test with python are OK. I tried to test my code on my Macbook. So I installed Tensorflow, Matplotlib and Pandas using Pip3 on the command prompt. But when I ran the code on my Mac I got this :
Traceback (most recent call last):
File "test.py", line 3, in <module>
import tensorflow as ts
ModuleNotFoundError: No module named 'tensorflow'
There are the output and the Pip3 list :
Result of the pip install tensorflow
pip3 show tensorflow
gave me :
Name: tensorflow
Version: 2.0.0a0
Summary: TensorFlow is an open source machine learning framework for everyone.
Home-page: https://www.tensorflow.org/
Author: Google Inc.
Author-email: packages#tensorflow.org
License: Apache 2.0
Location: /usr/local/lib/python3.7/site-packages
Requires: google-pasta, gast, absl-py, astor, keras-preprocessing, keras-applications, six, numpy, wheel, grpcio, protobuf, tf-estimator-nightly, termcolor, tb-nightly
Required-by:
And :
pip3 list
gave me :
Package Version
-------------------- --------------------
absl-py 0.8.0
astor 0.8.0
cycler 0.10.0
gast 0.3.2
google-pasta 0.1.7
grpcio 1.23.0
h5py 2.10.0
Keras-Applications 1.0.8
Keras-Preprocessing 1.1.0
kiwisolver 1.1.0
Markdown 3.1.1
matplotlib 3.1.1
numpy 1.17.2
pandas 0.25.1
pip 19.1.1
protobuf 3.9.1
pyparsing 2.4.2
python-dateutil 2.8.0
pytz 2019.2
setuptools 41.0.1
six 1.12.0
tb-nightly 1.14.0a20190301
tensorflow 2.0.0a0
termcolor 1.1.0
tf-estimator-nightly 1.14.0.dev2019030115
virtualenv 16.7.5
Werkzeug 0.15.6
wheel 0.33.4
I did the exact same things on my Mac that I did on my PC, but I have more than one version of Python on my Mac, maybe this is the problem, but I just don't know how to solve it.
Sorry for my English, I'm not quit used to post on forum like these, and thanks for your help, I can give more information on my problem if you want.

No module named 'tensorflow_probability'

I need to use Tensorflow and Tensorflow_Probability. After installing it by these commands: conda install tensorflow-probability or pip install --upgrade tensorflow-probability , I ran it in the notebook:
import tensorflow_probability as tfp
but it returns this error:
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-8-41494c8c96ff> in <module>
----> 1 import tensorflow_probability as tfp
ModuleNotFoundError: No module named 'tensorflow_probability'.
The results of
pip list
is as below (related parts):
tblib 1.3.2
tensorboard 1.13.1
tensorflow 1.13.1
tensorflow-estimator 1.13.0
tensorflow-probability 0.7.0
termcolor 1.1.0
terminado 0.8.1
testpath 0.4.2
tfp-nightly 0.8.0.dev20190708
Theano 1.0.4
toolz 0.9.0
Can anyone help me to solve this problem (I am using Win 10)?
Your versions are correct and your command is correct too.
Seems like inconsistency in other module is causing this.
run the following commands and try again:
pip install -U dm-sonnet==1.23
pip install --upgrade tfp-nightly
References:
https://github.com/deepmind/graph_nets/issues/3
https://github.com/tensorflow/probability/issues/103
tensorflow-probability 0.7.0
is not compatible with:
tensorflow 1.13.1
check the tensoflow-probability version release page https://github.com/tensorflow/probability/releases
Correct solution will be either to upgrade tensorflow to 1.14.0 or downgrade tensorflow-probability to 0.6.0
pip install -U tensorflow-probability==0.6.0
As the previous answer, you have to find a version compatible with your TensorFlow version through this page: https://github.com/tensorflow/probability/releases

Conda and Anaconda both on Windows machine. Jupyter notebook cannot import Python packages

The goal is I'm trying to use autograd in Jupyter Notebook on my Windows 7 machine.
Here is what I have done:
I activated a conda environment, in git bash, using source activate myenv
I installed autograd using conda install -c omnia autograd
I started Jupyter notebook with jupyter notebook
Now when I try to import autograd in Jupyter notebook, I have the following error:
No module named 'autograd'
So I stopped the Jupyter notebook and tried to use pip to install again. But I have this error:
$ pip install autograd
Requirement already satisfied: autograd in c:\users\******\appdata\local\conda\conda\envs\myenv\lib\site-packages (1.1.2)
Requirement already satisfied: numpy>=1.9 in c:\users\******\appdata\local\conda\conda\envs\myenv\lib\site-packages (from autograd) (1.14.5)
Requirement already satisfied: future in c:\users\******\appdata\local\conda\conda\envs\myenv\lib\site-packages (from autograd) (0.16.0)
Apparently, conda thinks it already installs autograd.
So I thought I might have two versions of conda installed? Here are the results of my conda env list:
# conda environments:
#
base C:\ProgramData\Anaconda3
myenv * C:\Users\******\AppData\Local\conda\conda\envs\myenv
And in both conda installations there is a 'pkg' folder, with different packages installed.
My speculation is Jupyter notebook is connected to the 'base' anaconda3, which does not have autograd installed?
My question is simply how can I use autograd in Jupyter notebook, and possibly clean everything up so I do not have two condas installed on my machine?
Here are the results for activate myenv and run conda list:
# packages in environment at C:\Users\******\AppData\Local\conda\conda\envs\myenv:
#
_py-xgboost-mutex 2.0 cpu_0
autograd 1.1.2 np112py36_0 omnia
blas 1.0 mkl
certifi 2018.4.16 py36_0
chardet 3.0.4 <pip>
Cython 0.28.4 <pip>
django 2.0.5 py36hd476221_0 anaconda
future 0.16.0 py36_1
icc_rt 2017.0.4 h97af966_0
idna 2.7 <pip>
intel-openmp 2018.0.3 0
kaggle 1.3.12 <pip>
libxgboost 0.72 0
m2w64-gcc-libgfortran 5.3.0 6
m2w64-gcc-libs 5.3.0 7
m2w64-gcc-libs-core 5.3.0 7
m2w64-gmp 6.1.0 2
m2w64-libwinpthread-git 5.0.0.4634.697f757 2
mkl 2018.0.3 1
mkl_fft 1.0.1 py36h452e1ab_0
mkl_random 1.0.1 py36h9258bd6_0
msys2-conda-epoch 20160418 1
numpy 1.12.1 py36hf30b8aa_1
numpy-base 1.14.5 py36h5c71026_0
pandas 0.23.1 py36h830ac7b_0
pip 10.0.1 py36_0
py-xgboost 0.72 py36h6538335_0
pyodbc 4.0.23 <pip>
python 3.6.5 h0c2934d_0
python-dateutil 2.7.3 py36_0
pytz 2018.4 py36_0 anaconda
requests 2.19.1 <pip>
scikit-learn 0.19.1 py36h53aea1b_0
scipy 1.1.0 py36h672f292_0
setuptools 39.2.0 py36_0
six 1.11.0 py36h4db2310_1
tqdm 4.23.4 <pip>
urllib3 1.22 <pip>
vc 14 h0510ff6_3
vs2015_runtime 14.0.25123 3
wheel 0.31.1 py36_0
wincertstore 0.2 py36h7fe50ca_0
xgboost 0.72 <pip>
There are a few things you can check. First, guarantee that your package exists inside the environment by running:
> source activate myenv
(myenv) > conda list
There will be a list of packages that conda can find for that environment. Make sure you see autograd there!
Next, in your Jupyter notebook, run the following:
import sys
print(sys.executable)
This shows the full path of the python executable running the notebook. You should see something similar to: ~/anaconda3/envs/myenv/bin/python. If you don't see myenv in the path, then Jupyter is running in a different environment. It's likely that your system path finds a different Jupyter first. Check your environment variables to see if another Jupyter comes first.
You can force Jupyter to run from a specific environment by starting it with the full path: ~/anaconda3/envs/myenv/bin/juypter
You can use the exclamation bang in an iPython cell, to install autograd as such:
!pip install autograd.
This way the installation is guaranteed to correspond to the iPython kernel.

Categories