I'm struggling to install tensorflow with a M1 mac. I've got python 3.9.7 and Monterrey 12.3 and apple silicon visual studio code. There is an apple solution involving miniconda apple dependancies and tensorflow-macos and tensorflow-metal. However this solution is not good for me as I have to use Rosetta2 emulator for multiple packages including PyQt5 etc. I was wondering if anyone has been able to use their M1 macs and pip installed tensorflow in a venv rosetta terminal. Thank you.
Kevin
Running TensorFlow on miniforge + conda-forge (arm64)
TensorFlow can run natively on M1 (arm64) macs. A highly recommended, easy way to install TensorFlow on arm64 macs is to via conda-forge. You should install python via miniforge or miniconda, because there is an arm64 (Apple Sillicon) distribution. With this, as of today, you can install the latest version 2.10.0 of TensorFlow:
$ lipo -archs $(which python3) # python3 is running natively as arm64
arm64
$ conda install -c conda-forge tensorflow
Note: tensorflow-macos 2.4.0 is obsolete so you shouldn't be using that.
But still want Rosetta 2? Try conda-forge.
If you really need to have python running on Rosetta 2 (x86_64) in cases where some packages does not support arm64,
you can still install TensorFlow with a macOS x86_64 release via conda. Installing via pip and PyPI repository won't work here, because you will run into Illegal hardware instruction segfault because Google's official TF macos-x86_64 wheel releases on PyPI assumes a target platform that has AVX instructions.
$ lipo -archs $(which python3) # x86_64 means Rosetta 2
x86_64
$ conda install -c conda-forge tensorflow # install via conda
$ python -c 'import tensorflow; print(tensorflow.__version__)'
Background
I had Anaconda 5.2 with python version 3.6.5 On windows 10.
As the time of the writing, the latest Anaconda version is 5.3, which uses python 3.7.0. The latest python release is 3.7.1.
I used the following commands in cmd 2 times to update to the latest Anaconda:
conda update conda
conda update anaconda
The end result of above is that, the Anaconda is now version 5.3, but python is updated to the latest 3.6, but not to 3.7
list anaconda
# packages in environment at C:\Anaconda3:
#
# Name Version Build Channel
anaconda 5.3.0 py36_0
anaconda-client 1.7.2 py36_0
anaconda-navigator 1.7.0 py36_0
anaconda-project 0.8.2 py36_0
python --version
Python 3.6.6 :: Anaconda, Inc.
Question
how to update to the latest Anaconda with the latest python version it supports?
(Besides Uninstall&Reinstall)
Try
conda install python==3.7.0
then just create new environment with python version you want, like
conda create --name python3_7 python=3.7.0 --channel conda-forge
EDIT: just tested on my mac terminal and
conda update python
updated my python version 3.6.5 to 3.7.0
Anaconda is already the latest version but if you want to create python 3.7 environments you need to update python. Afterwards fire up Anaconda-Navigator and create a new environment and you will see "3.7" as one of your choices in the drop down menu.
To update anaconda to be the same as the latest official fresh installation (anaconda 5.3):
conda install anaconda=5.3
Im a windows 10 user and this is my version of python at the moment
Python 3.6.3 (v3.6.3:2c5fed8, Oct 3 2017, 18:11:49) [MSC v.1900 64 bit (AMD64)] on win32
and when i try to install tensorflow with pip this pop up
tensorflow_gpu-0.12.1-cp35-cp35m-win_amd64.whl is not a supported wheel on this platform.
I ve looking on the internet which suggest that tensorflow now only work on python 3.5.2. Is it true? if not then how do i bypass that error?
TensorFlow only supports Python 3.5 64-bit as of now. Support for Python 3.6 is a work in progress and you can track it here as well as chime in the discussion.
The only alternative to use Python 3.6 with TensorFlow on Windows currently is building TF from source.
If you don't want to uninstall your Anaconda distribution for Python 3.6 and install a previous release you can create a conda environment for Python=3.5 as in:
conda create --name tensorflow python=3.5
activate tensorflow
pip install tensorflow-gpu
Also one more alternative
If you are using anaconda distribution, you can do the following to use python 3.5 on the new environnement "tensorflow":
conda create --name tensorflow python=3.5
activate tensorflow
conda install jupyter
conda install scipy
pip install tensorflow
or
pip install tensorflow-gpu
It is important to add python=3.5 at the end of the first line, because it will install Python 3.5.
I use Jupyter notebook in a browser for Python programming, I have installed Anaconda (Python 3.5). But I'm quite sure that Jupyter is running my python commands with the native python interpreter and not with anaconda. How can I change it and use Anaconda as interpreter?
from platform import python_version
print(python_version())
This will give you the exact version of python running your script. eg output:
3.6.5
import sys
sys.executable
will give you the interpreter. You can select the interpreter you want when you create a new notebook. Make sure the path to your anaconda interpreter is added to your path (somewhere in your bashrc/bash_profile most likely).
For example I used to have the following line in my .bash_profile, that I added manually :
export PATH="$HOME/anaconda3/bin:$PATH"
EDIT: As mentioned in a comment, this is not the proper way to add anaconda to the path. Quoting Anaconda's doc, this should be done instead after install, using conda init:
Should I add Anaconda to the macOS or Linux PATH?
We do not recommend adding Anaconda to the PATH manually. During
installation, you will be asked “Do you wish the installer to
initialize Anaconda3 by running conda init?” We recommend “yes”. If
you enter “no”, then conda will not modify your shell scripts at all.
In order to initialize after the installation process is done, first
run source <path to conda>/bin/activate and then run conda init
import sys
print(sys.executable)
print(sys.version)
print(sys.version_info)
Seen below :- output when i run JupyterNotebook outside a CONDA venv
/home/dhankar/anaconda2/bin/python
2.7.12 |Anaconda 4.2.0 (64-bit)| (default, Jul 2 2016, 17:42:40)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)]
sys.version_info(major=2, minor=7, micro=12, releaselevel='final', serial=0)
Seen below when i run same JupyterNoteBook within a CONDA Venv created with command --
conda create -n py35 python=3.5 ## Here - py35 , is name of my VENV
in my Jupyter Notebook it prints :-
/home/dhankar/anaconda2/envs/py35/bin/python
3.5.2 |Continuum Analytics, Inc.| (default, Jul 2 2016, 17:53:06)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)]
sys.version_info(major=3, minor=5, micro=2, releaselevel='final', serial=0)
also if you already have various VENV's created with different versions of Python you switch to the desired Kernel by choosing KERNEL >> CHANGE KERNEL from within the JupyterNotebook menu...
JupyterNotebookScreencapture
Also to install ipykernel within an existing CONDA Virtual Environment -
http://ipython.readthedocs.io/en/stable/install/kernel_install.html#kernels-for-different-environments
Source --- https://github.com/jupyter/notebook/issues/1524
$ /path/to/python -m ipykernel install --help
usage: ipython-kernel-install [-h] [--user] [--name NAME]
[--display-name DISPLAY_NAME]
[--profile PROFILE] [--prefix PREFIX]
[--sys-prefix]
Install the IPython kernel spec.
optional arguments:
-h, --help show this help message and exit
--user Install for the current user instead of system-wide
--name NAME Specify a name for the kernelspec. This is needed to
have multiple IPython kernels at the same time.
--display-name DISPLAY_NAME
Specify the display name for the kernelspec. This is
helpful when you have multiple IPython kernels.
--profile PROFILE Specify an IPython profile to load. This can be used
to create custom versions of the kernel.
--prefix PREFIX Specify an install prefix for the kernelspec. This is
needed to install into a non-default location, such as
a conda/virtual-env.
--sys-prefix Install to Python's sys.prefix. Shorthand for
--prefix='/Users/bussonniermatthias/anaconda'. For use
in conda/virtual-envs.
You can check python version using
!python -V
or
!python --version
Python 3.6.5 :: Anaconda, Inc.
You can add Conda environment to your jupyter notebook
Step 1: Create a Conda environment.
conda create --name firstEnv
Step 2: Activate the environment using the command as shown in the console.
conda activate firstEnv
conda install -c conda-forge <package-name>
E.g.
conda install -c conda-forge tensorflow
Step 3: set this conda environment on your jupyter notebook
conda install -c anaconda ipykernel
python -m ipykernel install --user --name=firstEnv
Step 4: Just check your Jupyter Notebook, to see firstEnv
You can refer this article
https://medium.com/#nrk25693/how-to-add-your-conda-environment-to-your-jupyter-notebook-in-just-4-steps-abeab8b8d084
Looking the Python version
Jupyter menu help/about will show the Python version
Assuming you have the wrong backend system you can change the backend kernel by creating a new or editing the existing kernel.json in the kernels folder of your jupyter data path jupyter --paths. You can have multiple kernels (R, Python2, Python3 (+virtualenvs), Haskell), e.g. you can create an Anaconda specific kernel:
$ <anaconda-path>/bin/python3 -m ipykernel install --user --name anaconda --display-name "Anaconda"
Should create a new kernel:
<jupyter-data-dir>/kernels/anaconda/kernel.json
{
"argv": [ "<anaconda-path>/bin/python3", "-m", "ipykernel", "-f", "{connection_file}" ],
"display_name": "Anaconda",
"language": "python"
}
You need to ensure ipykernel package is installed in the anaconda distribution.
This way you can just switch between kernels and have different notebooks using different kernels.
Creating a virtual environment for Jupyter Notebooks
A minimal Python install is
sudo apt install python3.7 python3.7-venv python3.7-minimal python3.7-distutils python3.7-dev python3.7-gdbm python3-gdbm-dbg python3-pip
Then you can create and use the environment
/usr/bin/python3.7 -m venv test
cd test
source test/bin/activate
pip install jupyter matplotlib seaborn numpy pandas scipy
# install other packages you need with pip/apt
jupyter notebook
deactivate
You can make a kernel for Jupyter with
ipython3 kernel install --user --name=test
Check the Python Version
import sys
print(sys.version)
I created a new python3 environment to install python 3; due to the incompatibility between conda and Windows PowerShell, I installed python 3 in both of the root and python3 environment by accident. Is it possible to revert to python 2 in the root environment?
C:\Users\Nick>conda info --env
# conda environments:
#
python3 C:\application\anaconda\envs\python3
root * C:\application\anaconda
C:\Users\Nick>python -V
Python 3.3.5 :: Continuum Analytics, Inc.
conda install python=2 should do the trick. Definitely check and make sure that all packages downgraded successfully by inspecting the output of conda list for versions that contain the py3 string. For any packages that contain py3 in the version string, you should probably just uninstall/reinstall them.
(Hypothetical) Example:
The third column in conda list is the python version. If you see:
anaconda-client 1.1.2 py35_0 defaults
then you should
conda remove anaconda-client
followed by
conda install anaconda-client so that it installs the python 2 version.
Then conda list should show the anaconda-client line like this:
anaconda-client 1.1.2 py27_0 defaults.