Tensorflow Not Working from Anaconda on Mac - python

When it comes to installing Tensorflow, I've tried each of the installation suggestions on this page.
https://www.tensorflow.org/install/install_mac
-Pip + Pip3
-virtualenv
-With Docker
The only installation method that I was unable to apply was Conda. My default environment for Data Science is Spyder launched from Anaconda_Navigator. However, I am unable to get the Conda command to work, in any form, from the command line.
My goal is to get tensor flow working from the iPython console from with in Spyder.
I am trying to run the suggested validation code:
# Python
import tensorflow as tf
hello = tf.constant('Hello, TensorFlow!')
sess = tf.Session()
print(sess.run(hello))
Here is the information on my iPython installation:
Python 3.6.1 |Anaconda 4.4.0 (x86_64)| (default, May 11 2017, 13:04:09)
Type "copyright", "credits" or "license" for more information.
IPython 5.3.0 -- An enhanced Interactive Python.
The first line of code throws the following error.
ModuleNotFoundError: No module named 'tensorflow'
When I try to run from Python 2.7 in the command line from terminal, I get:
ImportError: numpy.core.multiarray failed to import
Failed to load the native TensorFlow runtime.
When I try to run it from the terminal command line in Python 3.6.1, I get the following error in regards to the second line of code:
AttributeError: module 'tensorflow' has no attribute 'constant'

I followed the below steps and it worked for me.
Use Conda to create a new virtual environment called "tensorflow" and install the tensorflow in the new conda environment, following the instructions of below link: https://www.tensorflow.org/install/install_mac. The section name is "Installing with Anaconda". All these steps are run through a MAC terminal.
Launch the Anaconda Navigator as usual
Switch to the new "tensorflow" environment using the drop-down box at the top. This is important. By default, "root" environment is chosen.
The new "tensorflow" environment does not have spyder installed. Click the "Install" button. You should see the screenshot like below
5. Launch spyder and type the sample tensorflow codes you want to execute.
Good luck.

Try this:
export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-0.8.0-cp27-none-linux_x86_64.whl
sudo pip install --upgrade $TF_BINARY_URL

The first error
ModuleNotFoundError: No module named 'tensorflow'
is related to Anaconda path. Anaconda does't use the PYTHONPATH. Try:
unset PYTHONPATH
source activate anaconda-x.x #your version instead of x.x
python
>>>> import tensorflow as tf
The second error
ImportError: numpy.core.multiarray failed to import
Failed to load the native TensorFlow runtime.
is due numpy version needs of tensorflow, try upgrade numpy.
The third error,
AttributeError: module 'tensorflow' has no attribute 'constant'
may be related to your binary (hint: check if you are using the correct CPU (or GPU) for your OS).
Let me know if something helped you. Good luck! :)

Related

DLL load failed: The specified module could not be found, importing tensorflow_datasets in python

I installed tensorflow 2.0 and tensorflow_dataset in a Python 3 environment.
then when i try to import tensorflow_datasets,
import tensorflow_datasets as tfds
this error appears:
I tried to install again tensorflow-dataser from Jupyter and not from prompt, and this error appears:
I think your cuDNN DLL is not in %PATH% environment variable can you check it?
If not you have to add cuda\bin subdirectory to get Windows to find the DLL :)
I was finally able to solve the issue.
I did this by downloading and installing openssl from
openssl
(thanks to Udemy technical assistant)
but then, failed to load MNIST, so I should have updated jupyter and ipywidget with
conda install -c conda-forge ipywidgets
jupyter nbextension enable --py widgetsnbextension
(the solution is from here)

How do I import scikit-learn in a jupyter notebook?

I created a fresh conda environment for using scikit-learn and used
conda install <package> to install scikit-learn, jupyter, pandas, etc. for compatible dependencies..
I checked if sklearn was working after loading the environment:
$python
Python 3.7.4 (default, Aug 13 2019, 15:17:50)
[Clang 4.0.1 (tags/RELEASE_401/final)] :: Anaconda, Inc. on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sklearn
>>>
Since import command didn't throw errors, sklearn is ready for use. However, I am getting a ModuleNotFoundError while trying to import it in a jupyter notebook, that I am running from the same environment.
import sklearn
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-1-b7c74cbf5af0> in <module>()
----> 1 import sklearn
ModuleNotFoundError: No module named 'sklearn'
I was able to import numpy and pandas in the same notebook without any errors.
Please help me understand the problem and how to troubleshoot it.
Best practice: Install everything via conda or pip3, as mentioned in this answer.
If that didn't work, check the system paths in jupyter notebook:
import sys
sys.path
and the system executable:
sys.executable
These must correspond to the python in your current loaded environment.
For me, the issue was with the jupyter Notebook's kernel. See the kernel specifications in kernel.json file in the path. You can find the directory for this file from jupyter kernelspec list. I manually changed the python path to the python in my environment (this is a bad idea, but it worked).
Make sure that your jupyter notebook is finding the same version of python as your terminal, otherwise installing modules with conda install in your terminal won't show up in your notebook. Do
import sys
print(sys.version)
in your notebook and in your terminal. If they do not match up, then add your terminal's python version to your notebook:
conda install nb_conda_kernels
conda install ipykernel
and then in the notebook switch to the kernel you just installed (kernel -> change kernel)
Check your path for Python and Jupyter:
import sys
sys.path
You may find different results from Python and Jupyter. This can be fixed temporarily by appending this line of code if you are using macos:
sys.path.append('/Users/**your_user_name**/anaconda3/lib/python3.7/site-packages')
If you want to solve this permanently, you can create an iPython profile and fix it there.
First activate the finds environment, second launch jupyter notebook, third import sklearn. Inside jupyter notebook.
I have also found the same issue
ModuleNotFoundError: No module named 'sklearn'
but after searching I found its best solution.
You should also try the following steps:
Step 1:
open "cmd"
Step 2:
write "pip install notebook"
Step 3:
After installation of notebook, write "jupyter notebook " in cmd.
Tell me if you got your solution
thank you!

"import torch" giving error "from torch._C import *, DLL load failed: The specified module could not be found"

I am currently using Python 3.5.5 on Anaconda and I am unable to import torch. It is giving me the following error in Spyder:
Python 3.5.5 |Anaconda, Inc.| (default, Mar 12 2018, 17:44:09) [MSC v.1900
64 bit (AMD64)]
Type "copyright", "credits" or "license" for more information.
IPython 6.2.1 -- An enhanced Interactive Python.
import torch
Traceback (most recent call last):
File "<ipython-input-1-eb42ca6e4af3>", line 1, in <module>
import torch
File "C:\Users\trish\Anaconda3\envs\virtual_platform\lib\site-
packages\torch\__init__.py", line 76, in <module>
from torch._C import *
ImportError: DLL load failed: The specified module could not be found.
Many suggestions on the internet say that the working directory should not be the same directory that the torch package is in, however I've manually set my working directory to C:/Users/trish/Downloads, and I am getting the same error.
Also I've already tried the following: reinstalling Anaconda and all packages from scratch, and I've ensured there is no duplicate "torch" folder in my directory.
Pls help! Thank you!
I had this similar problem in windows 10...
Solution:
Download win-64/intel-openmp-2018.0.0-8.tar.bz2 from https://anaconda.org/anaconda/intel-openmp/files
Extract it and put the dll files in Library\bin into C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.0\bin
Make sure your cuda directory is added to your %PATH% environment variable
I had the same problem. In my case I didn't want the GPU version of pytorch.
I uninstalled it. The version was pytorch: 0.3.1-py36_cuda80_cudnn6he774522_2 peterjc123.
The problem is that cuda and cudnn . then installed with the following command and now it works!
conda install -c peterjc123 pytorch-cpu
I also encountered the same problem when I used a conda environment with python 3.6.8 and pytorch installed by conda from channel -c pytorch.
Here is what worked for me:
1:) conda create -n envName python=3.6 anaconda
2:) conda update -n envName conda
3:) conda activate envName
4:) conda install pytorch torchvision cudatoolkit=9.0 -c pytorch
and then tested torch with the given code:
5:) python -c "import torch; print(torch.cuda.get_device_name(0))"
Note: 5th step will return your gpu name if you have a cuda compatible gpu
Summary: I just created a conda environment containing whole anaconda and then to tackle the issue of unmatched conda version I updated conda of new environment from the base environment and then installed pytorch in that environment and tested pytorch.
For CPU version, here is the link for my another answer: https://gist.github.com/peterjc123/6b804651288e76db7b5fabe5348e1f03#gistcomment-2842825
https://gist.github.com/peterjc123/6b804651288e76db7b5fabe5348e1f03#gistcomment-2842837
Had the same problem and fixed it by re-installing numpy with mkl (Intel's math kernel library)
https://www.lfd.uci.edu/~gohlke/pythonlibs/#numpy
Download the right .whl for your machine. For me it was numpy‑1.14.5+mkl‑cp36‑cp36m‑win_amd64.whl (python 3.6, windows, 64-bit)
and then install using pip.
pip install numpy‑1.14.5+mkl‑cp36‑cp36m‑win_amd64.whl
I am using a Windows 10 computer with an NVIDIA GeForce graphics card. NVIDIA showed I had CUDA 10.1, but I was getting this error when running import torch in Jupyter Lab and suspected it had something to do with CUDA support.
I fixed this problem by downloading and installing the CUDA Toolkit directly from NVIDIA. It installed all required Visual Studio components. When I returned to Jupyter Lab, import torch ran without error.
Make sure you installed the right version of pytorch for your enviroment. I had the same problem I was using pytorch on windows but I had the default package installed which was meant for cuda 8. So I reinstalled the pytorch package for cpu which was what I needed.
I had the same issue with running torch installed with pure pip and solved it by switching to conda.
Following steps:
uninstall python 3.6 from python.org (if exists)
install miniconda
install torch in conda ("conda install pytorch -c pytorch")
Issue with pip installation:
import torch
File "C:\Program Files\Python35\lib\site-packages\torch\__init__.py", line 78, in <module>
from torch._C import *
ImportError: DLL load failed: The specified module could not be found.
After switching to conda it works fine. I believe the issue was resolved by conda through installing the vs_redist 2017
vs2017_runtime 15.4.27004.2010 peterjc123
But I have tried it w/o conda and it did not help. Could not find how to check (and tweak) Python's vs_redist.
Windows10 Solution(This worked for my system):
I was having the same issue in my system. Previously I was using Python 3.5 and I created a virtual environment named pytorch_test using the virtualenv module because I didn't want to mess up my tensorflow installation(which took me a lot of time). I followed every instruction but it didn't seem to work. I installed python 3.6.7 added it to the path. Then I created the virtual environment using:
virtualenv --python=3.6 pytorch_test
Then go to the destination folder
cd D:\pytorch_test
and activate the virtual environment entering the command in cmd:
.\Scripts\activate
After you do this the command prompt will show:
(pytorch_test) D:\pytorch_test>
Update pip if you have not done it before using:
(pytorch_test) D:\pytorch_test>python -m pip install --upgrade pip
Then go for installing numpy+mkl from the site:
https://www.lfd.uci.edu/~gohlke/pythonlibs/#numpy
Choose the correct version from the list if you have python 3.6.7 go with the wheel file:
numpy‑1.15.4+mkl‑cp36‑cp36m‑win_amd64.whl (For 64 bit)
(Note if the whole thing doesnot work just go with simple numpy installation and mkl installation separately)
Then go for installing openmp using:
(pytorch_test) D:\pytorch_test>pip install intel-openmp
Now you are done with the prerequisites. To install pytorch go to the previous versions site:
https://pytorch.org/get-started/previous-versions/
Here select the suitable version from the list of Windows Binaries. For example I am having CUDA 9.0 installed in my system with python 3.6.7 so I went with the gpu version:
cu90/torch-1.0.0-cp36-cp36m-win_amd64.whl
(There are two available versions 0.4.0 and 1.0.0 for pytorch, I went with 1.0.0)
After downloading the file install it using pip(assuming the whl file is in D:).You have to do this from the virtual environment pytorch_test itself:
(pytorch_test) D:\pytorch_test>pip install D:\torch-1.0.0-cp36-cp36m-win_amd64.whl
Prerequisites like six, pillow will be installed automatically.
Then once everything is done, install the models using torchvision.
Simply type :
(pytorch_test) D:\pytorch_test>pip install torchvision
To check everything is working fine try the following script:
import torch
test = torch.rand(4, 7)
print(test)
If everything was good then it wont be an issue. Whenever there is an issue like this it is related to version mismatch of one or more dependencies. This also occurred during tensorflow installation.
Deactivate the following virtual environment using the command deactivate in the cmd:
(pytorch_test) D:\pytorch_test>deactivate
This is the output of pip list in my system:
Package Version
------------ -----------
intel-openmp 2019.0
mkl 2019.0
numpy 1.16.2
Pillow 6.0.0
pip 19.0.3
setuptools 41.0.0
six 1.12.0
torch 1.0.0
torchvision 0.2.2.post3
wheel 0.33.1
Hope this helps. This is my first answer in this community, hope you all find it helpful. I setup pytorch today in the afternoon after trying all sorts of combinations. The same import problem occurred to me while installing CNTK and tensorflow. Anyway I kept them separate in different virtual environments so that I can use them anytime.

Error while import tensorflow module

I try to learn TensorFlow with Python. My problem is with import TF module. Here is my configuration: Python 3.6.1, Windows 7 (with MSVCP140.dll)
I've installed TensorFlow by command (in power shell). It works.
python -m pip install --upgrade tensorflow
But when I run python environment and try import Tensor Flow
import tensorflow as tf
I get errors, this error raise another errors related with it, but at the beginning I want to resolve this first
Traceback (most recent call last): File
"C:\Users\Jacek\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\pywrap_tensorflow.py",
line 18, in swig_import_helper
fp, pathname, description = imp.find_module('_pywrap_tensorflow', [dirname(file)]) File
"C:\Users\Jacek\AppData\Local\Programs\Python\Python36\lib\imp.py",
line 296, in find_module
raise ImportError(_ERR_MSG.format(name), name=name) ImportError: No module named '_pywrap_tensorflow'
The problem was the cuDNN Library for me - for whatever reason cudnn-8.0-windows10-x64-v6.0 was NOT working - I used cudnn-8.0-windows10-x64-v5.1 - ALL GOOD!
My setup working with Win10 64 and the Nvidia GTX780M:
Be sure you have the lib MSVCP140.DLL by checking your system/path - if not get it here
Run the windows installer for python 3.5.3-amd64 from here - DO NOT try newer versions as they probably won't work
Get the cuDNN v5.1 for CUDA 8.0 from here - put it under your users folder or in another known location (you will need this in your path)
Get CUDA 8.0 x86_64 from here
Set PATH vars as expected to point at the cuDNN libs and python (the python path should be added during the python install)
If you run Windows 32 be sure to get the 32 bit versions of the files mentioned above.
Solution is downgrade Python to version 3.5, and install again TensorFlow. It works for me
This is a known error. There is a file named MSVCP140.DLL, that you will need in your system to run TensorFlow. Check if this file is in your %path%. If it is not, download Visual Studio C++. It is free and you can download it here: https://www.visualstudio.com/vs/cplusplus.
Solution:-
Don't suffer a lot. Simply downgrade your python version from 3.6.1 to 3.5.2 and
install tensorflow again.. you need not to upgrade the package.
"pip install tensorflow" will automatically download latest version (probably
1.0.1)
Steps:-
Step 1:- conda search python
Step 2:- conda install python=3.5.2
Step 3:- pip install tensorflow
Step 4:- import tensorflow as tf
Horray!!.. It works..
Hope you may not get the same error again!!
At first, I used an anaconda environment with Python 3.5 and PIP version 19 but had the same problem. so instead of PIP, I used Conda package manager:
conda install TensorFlow
it worked well after using Conda instead of PIP
If you have a GPU in your system and it is conflicting with the current set of libraries or throwing a cudnn error then you can add the following line in your code to disable the GPU
os.environ["CUDA_VISIBLE_DEVICES"] = "-1"
put this in your code and it should be fine.
pip install tensorflow-gpu
Install this package this helps me to solve my issues
or else use python 3.9.7

Tensorflow import error on Pycharm (Mac)

Error msg (check the screenshot picture please):
ImportError: cannot import name symbol_database
Error importing tensorflow. Unless you are using bazel,
you should not try to import tensorflow from its source directory;
please exit the tensorflow source tree, and relaunch your python interpreter
from there.
Process finished with exit code 1
Thanks a lot !
I tried to reproduce the problem you're running into using this sample code:
import tensorflow as tf
hello = tf.constant('Hello, Tensorflow!')
But I was not able to. I can run the script without issues within PyCharm and in my command line (exit code 0).
These are the packages (and their version) that are required for tensorflow 0.8.0 to work:
numpy==1.11.0
protobuf==3.0.0b2
six==1.10.0
I'm using PyCharm Community Edition 2016.1.2 and Python 2.7.10 on Mac OS X 10.11.4
How did you install tensorflow?
You should install it via pip (as recommended by the site: https://www.tensorflow.org/versions/r0.8/get_started/os_setup.html#pip-installation)
For Python 2, Mac OS X, this is the installation command:
pip install --upgrade https://storage.googleapis.com/tensorflow/mac/tensorflow-0.8.0-py2-none-any.whl

Categories

Resources