Cannot start jupyter notebook with tensorflow on M1 mac - tensorflow

To get tensorflow working on an M1 mac I installed anaconda navigator, and then followed these instructions:
install miniforge, and then
tensorflow-deps
tensorflow-macos
tensorflow-metal
Installing these packages did not give any errors, but when I try to launch a Jupyter notebook from the Anaconda Navigator interface I get this error
ImportError: dlopen(/Users/../miniforge3/envs/macos-tensorflow64/lib/python3.8/site-packages/zmq/backend/cython/_device.cpython-38-darwin.so, 0x0002): tried: '/Users/../miniforge3/envs/macos-tensorflow64/lib/python3.8/site-packages/zmq/backend/cython/_device.cpython-38-darwin.so' (mach-o file, but is an incompatible architecture (have 'x86_64', need 'arm64e'))
It seems that _device.cpython-38-darwin.so is not ready for M1 ? Is this a bug?
How can I get tensorflow working on the M1 mac? (with a jupyter notebook). Note: I did get Jupyter notebook working with python in general, just not with tensorflow).

It seems you have installed miniforge 'x86_64' file whereas you need 'arm64' as shown in the ImportError message.
Please try again by installing miniconda macOS (Miniconda3 macOS Apple M1 64-bit bash) package and run below code step by step in the terminal to access the tensorflow with jupyter notebook:
Download the miniconda package, open the terminal and cd to the
download directory to access the miniconda.sh file
cd downloads
bash <the_downloaded_package.sh> - agree(yes) to the License terms and confirm the miniconda installation directory.
cd to the installed miniconda directory and create conda virtual environment for tensorflow
conda create --name tf python=3.9
conda activate tf
Now install TensorFlow using
pip install --upgrade pip
python -m pip install tensorflow #or
python -m pip install tensorflow-macos
python -m pip install tensorflow-metal
To verify if tensorflow installed, type:
python (enter)
>>import tensorflow as tf
>>tf.__version__
Install and open the jupyter notebook.
conda install jupyter
jupyter notebook

Related

Error when installing TensorFlow through pip

I'm following the instructions from https://www.tensorflow.org/install/pip#python-version-support to install tensorflow. I've used tensorflow before but for some reason, it has just stopped working. I'm using VS Code.
First, I check that pip and python are compatible:
python3 --version
python3 -m pip --version
I have Python 3.10.8, with pip 22.3.1 so it should be compatible. Then, I create a conda environment and activate it:
conda create --name tf python=3.9
conda activate tf
However, now when I try
pip install tensorflow
I get the error
ERROR: Could not find a version that satisfies the requirement tensorflow (from versions: none)
ERROR: No matching distribution found for tensorflow
How can I fix this? I also tried things in the past like installing tensorflow through Conda, but I get errors when importing tensorflow there.
Could you please try again by opening anaconda cmd propmt and run below code:
conda create --name tf_new python=3.10
conda activate tf_new
then install tensorflow using:
pip install tensorflow
Now open the VS code by selecting the same created VirEnv tf_new from Anaconda navigator.
In VScode, Open a new jupyter notebook file and run the below code by selecting the tf_new (Conda VirEnv) in kernel(from top right) .
import tensorflow as tf
tf.__version__
Output:
'2.11.0'

Unable to install Tensorflow with Python 3.8 in virtual environment

On my Mac M1 (Monterey) I have created a new virtual environment with
virtualenv --python=/opt/homebrew/bin/python3.8 ~/.virtualenvs/datascience_env
to link it with specific Python 3.8 version. Now I'm trying to install tensorflow. The following command (issued in the virtual environment):
(datascience_env)% pip install tensorflow
causes the following error:
ERROR: Could not find a version that satisfies the requirement tensorflow (from versions: none)
ERROR: No matching distribution found for tensorflow
Just to be sure, I also did:
(datascience_env)% python -c "import sys; print(sys.version)" or python -c "import struct; print(struct.calcsize('P')*8)"
and the output confirms the 3.8 version:
3.8.14 (default, Sep 6 2022, 23:17:06)
[Clang 13.1.6 (clang-1316.0.21.2.5)]
I've finally opted for a simpler solution. I followed here the procedure to install Tensorflow on Mac M1 with miniforge. Works like a charm. In summary the steps are install miniforge, then:
conda config --set auto_activate_base false
conda create --name mytfenv python=3.8
conda activate mytfenv
conda install -c apple tensorflow-deps
pip install tensorflow-macos
pip install tensorflow-metal
conda install -c conda-forge -y pandas jupyter

How to access to tensorflow from Jupyter notebook?

I installed tensorflow 2.0 from source, following the official instructions: https://www.tensorflow.org/install/source
It works when I access from python console (Outside of tensorflow folder), I want to get access from Jupyter notebook.
I copied tensorflow's folder to:
/home/kati/anaconda3/pkgs/tensorflow
but still not working
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-2-d6579f534729> in <module>
----> 1 import tensorflow
ModuleNotFoundError: No module named 'tensorflow'
System Info
Ubuntu 18.04.3
jupyter core : 4.5.0
jupyter-notebook : 6.0.0
Here are the steps:
Download Anaconda
Create a virtual environment and activate it
Install K̵e̵r̵a̵s̵ ̵a̵n̵d̵ TensorFlow etc.
Launch Jupyter Notebook
Since you already have Jupyter Notebook, you will need to create an env variable, install tensorflow and keras. Commands are mentioned below:
update conda in your default environment
$ conda upgrade --all
create a new environment with conda
$ conda create -n [my-env-name] python=[python-version]
activate the environment you created
$ source activate [my-env-name]
install pip in the virtual environment
$ conda install pip
install Tensorflow CPU version
$ pip3 install --upgrade tensorflow # for python 3.*
install Keras (Note: please install TensorFlow first)
$ pip install Keras
remove an environment
$ conda env remove --name [my-env-name]
Install pip package and then you can just use "pip install [package name]" command to install any package easily.
Did you start running jupyter notebook from the virtual environment which you installed tensorflow?
Also you don't need to copy tensorflow to anaconda3 folder it's better to install by using pip or anconda navigator.
pip install tensorflow
Reference:
https://pypi.org/project/tensorflow/

Adding Tensorflow to my anaconda platform

I have been using anaconda environment. Now that I have installed tensorflow in my windows through the command prompt and following the necessary steps. I am not able to use tensorflow in anaconda platform. Please help. Thank you.
Create a conda environment named tensorflow by invoking the following command:
C:> conda create -n tensorflow pip python=3.5
Activate the conda environment by issuing the following command:
C:> activate tensorflow
Issue the appropriate command to install TensorFlow inside your conda environment. To install the CPU-only version of TensorFlow, enter the following command:
(tensorflow)C:> pip install --ignore-installed --upgrade tensorflow
To install the GPU version of TensorFlow, enter the following command (on a single line):
(tensorflow)C:> pip install --ignore-installed --upgrade tensorflow-gpu
You can read further by visiting here

Can't download numpy and tensorflow to python 3.5

I have been trying to download tensorflow, and numpy so that they will work when I run them in python 3.5 idle ( the newest model that tensorflow seems to work with). I have both python 3.6 and 3.5 downloaded and I can't seem to make anything that I download( numpy so far) apply to anything but 3.6. I'm not sure if this has something to do with the path that I am calling them through command line, but I was wondering if there was an answer to this situation.
1] Using pip package manager
Use pip to install numpy for you : pip install numpy
If you do not have pip you can install it by following command : python get-pip.py
Use pip to install tensorflow for you: pip install tensorflow
2] Using Anaconda or Miniconda (RECOMMENDED)
Install Anaconda or Miniconda
conda is the package manager for Anaconda and Miniconda
Open the Conda prompt
Anaconda comes with numpy package so no need to explicitly install numpy
In case if you have downloaded Miniconda then you have to install numpy I guess. Run the following command to install numpy : conda install numpy
Run the following command to install tensorflow :conda install -c conda-forge tensorflow or simply conda install tensorflow
Anaconda also comes with it's own pip. You can use Anaconda's pip to install tensorflow by running pip install tensorflow
===========================================================================
By any chance if you get an error in the terminal saying that "command" is not recognized as an internal or external command which means you have not specified the absolute path of pip or python or conda
One solution to the above error can be to add the path of python, pip and anaconda as an environmental variable if you are on windows
If you are on Linux you can add the path of python, pip and anaconda to the PATH variable by export PATH=$PATH:/path/to/dir