Homebrew python version different than Mac python version? - python

I've searched a while and haven't found an answer to this particular issue.
brew info python returns python: stable 3.7.7 (bottled), HEAD
However, python -V and python3 -V return Python 3.6.1 :: Anaconda 4.4.0 (x86_64)
Why is my Mac python version different than what I've installed with HB, and how can I fix it? Thank you!

One version of python was installed using the Anaconda distribution. The other by Homebrew. It's not surprising that they are different versions. You should run in virtual environments and then you won't need to worry about 2 versions. Using the Anaconda distribution it is easy to set up a virtual environment to run that version.

Erg. Yes—this is a challenge sometimes. macOS ships with python as part of its system. Catalina 10.15.4 ships with /usr/bin/python3 of 3.7.3. brew (at the moment) has installed 3.7.7 as its stable branch, but it also offers a python#3.8 which installs 3.8.2. That's significant because there are other brew packages (notably vim) which specity python#3.8 as a dependency. Yuck.
I'm not running anaconda at the moment, but I'll take your word that it's yet-another version.
Whichever appears first in your path is going to prevail unless you're operating inside a context like a virtual environment. You can arrange your path to set your preferred python3 to run. You can always check which python3 is running by using "which python3" and that will give you the full path of the running program.
Word of caution: Each distinct python3 will maintain its own packages library. Once you have your paths sorted out, you should manage your packages with "python3 -m pip " to be sure you're managing the libraries for your intended version.
And yes—eventually, it'd probably be good for us to work in virtual environments or skip straight to Docker containers.

Related

Trouble switching between two Python 3.x versions

I am new to Python, and I have been trying to follow their online tutorial which relies on the current version Python 3.10 being installed. I've tried several solutions to upgrade from 3.9.12 to the most recent 3.10.4, but all modes of calling python default to 3.9.12.
I am using a Mac, and I first tried to use homebrew with "brew install", but I've read that homebrew hasn't yet switched from 3.9. I next tried to update through Anaconda by running "conda install python=3.10.4", but Anaconda Navigator is still using 3.9 for all environments.
I know that 3.10.4 has been installed on my computer; however, I lack the skillset right now to find out where it is and how I can change the default to calling from wherever 3.10 is right now.
I would appreciate help with this! Please let me know ways that I could change the default version of python being called (whether in Anaconda or just through access via terminal). I've found a lot of resources for changing between python2 and python3, but not much for switching between python3 versions.
Thanks!!!!
I'm not familiar with Conda, but I would recommend pyenv to solve this exact problem. pyenv helps you manage different Python versions, and set different versions per project, or per shell. This is very useful when working on different project or testing different distributions.
You basically install with brew like
brew update
brew install pyenv
Then set your environment, with these steps. (It will depend on your shell).
Then you can start installing different versions like:
pyenv install 3.10.4
pyenv install 3.7.3
...
With versions installed, you can set the global, local and shell versions.
# This will set the default global python3 to the specified version
pyenv global 3.10.4
# This will set the default interpreter in a specific project/folder
pyenv local 3.7.3
# This will set the current shell to a specific version
pyenv shell 3.9.6
IMHO it's the easiest way to work with different versions.
Here's the link to the official repo, and I would also suggest this article from RealPython, that's explains it all in much greater detail.

Updating Python3 and Pip3 on Mac

I have two versions of python3 installed on my computer. They are located here:
/usr/local/bin/python3
/usr/bin/python3
I have set my PATH variable to use the first version. Running "which python3" routes to this version: /usr/local/bin/python3 -- this is what I want.
Unfortunately, pip3, and yet another version of Python, are installed in a different location (I think the version that comes pre-installed with mac). When I run "pip3 --version" I get the below:
pip 20.1.1 from /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pip (python 3.7)
Shouldn't these match? Is there a way to make sure python3 uses the pip3 version via /usr/local/bin/pip3? Do I just need to change / add it to my path somehow?
Another option is to uninstall everything with homebrew (what I used to originally install python3), and then reinstall. However, apparently, per my co-worker, we need to stay on python3.7. I'm worried if I reinstall python3, it will default to 3.8 or higher.
Please help!
There are a few things that I have found increase the chances of success here:
don't mess with the Mac-installed default Python
don't use homebrew to install Python
use pyenv to install and manage Python versions
Here's a useful write-up on The right and wrong way to set Python 3 as default on a Mac.

Error when installing Tensorflow - Python 3.8

I'm new to programming and following a course where I must install Tensorflow. The issue is that I'm using Python 3.8 which I understand isn't supported by Tensorflow.
I've downloaded Python 3.6 but I don't know how to switch this as my default version of python.
Would it be best to set up a venv using python 3.6 for my program and install Tensorflow in this venv?
Also, I using Windows and Powershell.
Tensorflow is only supported until python 3.7 as of now.
You can check it here: https://www.tensorflow.org/install/pip
But there is a way to install it on Python3.8, just run the below command that will do your job:
python -m pip install --upgrade https://storage.googleapis.com/tensorflow/mac/cpu/tensorflow-1.12.0-py3-none-any.whl
This command work on mac and windows both, I haven't tested on Linux.
You should always use venv because by default every project on your system will use these same directories to store and retrieve site packages (third party libraries). At first glance, this may not seem like a big deal, and it isn’t really, for system packages (packages that are part of the standard Python library), but it does matter for site packages.
Consider the following scenario where you have two projects: ProjectA and ProjectB, both of which have a dependency on the same library, ProjectC. The problem becomes apparent when we start requiring different versions of ProjectC. Maybe ProjectA needs v1.0.0, while ProjectB requires the newer v2.0.0.
You can also take a look at anaconda, it’s the most populasr data sciencie platform and will be easy for you install tensorflow and jupiter notebook in just 2 clicks. Anaconda
Uninstall all your python versions and use the latest anaconda.
$ conda create --name tensorflow python=3.5
This way you create a virtual environment with python 3.5 which is supported by tensorflow.
So now you can install it.
$ activate tensorflow
(tensorflow) $ pip install tensorflow
it would have been nice if you would have the share the error screenshot
though as per i got the case
tensorflow work in both 3.8 and 3.6 just you have to check that you have 64bit version not 32 bit
you can acess both version from thier respective folder no need to install a venv
If you don't want to use Anaconda or virtualenv, then actually multiple Python versions can live side by side. I use Python38 as my default and Python35 for TensorFlow until they release it for Python38. If you wish to use the "non-default" Python, just invoke with the full path of the python.exe (or create a shortcut/batch file for it). Python then will take care of using the correct Python libs for that version.
Worked on Python 3.8.2 (default, Mar 05 2020, 18:58:42) [GCC] on linux
pip3 install --upgrade tf-nightly
Python Versions 3.5 - 3.8 are supported now.
You can verify on this page:
https://www.tensorflow.org/install/pip

Python 3.7.3 Inadvertently Installed on Mac OS 10.15.1 - Included in Xcode Developer Tools 11.2 Now?

I decided yesterday to do a clean install of Mac OS (as in, erase my entire disk and reinstall the OS).
I am on a Macbook Air 2018. I did a clean install of Mac OS 10.15.1.
I did this clean install due my previous Python environment being very messy.
It was my hope that I could get everything reigned in and installed properly.
I've started reinstalling my old applications, and took care to make sure nothing was installed in a weird location.
However, when I started setting up VS Code, I noticed that my options for Python interpreters showed 4 options. They are as follows:
Python 2.7.16 64-bit, located in /usr/bin/python
Python 2.7.16 64-bit, located in /System/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python
Python 3.7.3 64-bit, located in /user/bin/python
Python 3.7.3 64-bit, located in /Library/Developer/CommandLineTools/usr/bin/python3
In terminal, if I enter where python python3
it returns
/usr/bin/python /usr/bin/python3.
How in the world did python3 get there?
My only idea is that it now is included in the Xcode Developer Tools 11.2 package, as I did install that. I cannot find any documentation of this inclusion.
Any ideas how this got here? More importantly, how do I remove it? I want to use Homebrew for all of my installation needs. Also, why does VS Code show 4 options?
Thanks!
The command line tool to run the python 2.7 environment is at /usr/bin/python, but the framework and dependencies for it are in /System. This includes the Python.app bundle, which is just a wrapper for scripts that need to run using the Mac's UI environment.
Although these files are separate executables, it's likely that they point to the same environment.
Every MacOS has these.
Catalina does indeed also include /usr/bin/python3 by default. The first time you run it, the OS will want to download Xcode or the Command line tools to install the 'complete' python3. So these pair are also the same environment.
I don't think you can easily remove these, due to the security restrictions on system files in Catalina.
Interestingly, Big Sur still comes with python2 !

How to upgrade Python 2.x to latest 2.x version on MacOS

I have MacOS Sierra (10.12.6) and had Python 2.7.13 until I did brew upgrade and now it is showing 2.7.10
What is the best proven/safest way to upgrade to latest python 2.x (at this moment 2.7.14)?
It is curious Python 2.7.13 in Sierra. I thought that Apple has only included Python 2.7.10 for since at least as far back as El Capitan and still on High Sierra. I am on High Sierra and python -v is still 2.7.10 for example. I skipped Sierra so I don't know from personal experience on that one.
You are probably typing python -V in a terminal right? Are you sure which version of python is being found first in the path? Brew can't overwrite Sierra's Python (SIP would protect it from anything trying to change the included Python version) and installs it in usr/local. Brew is the best way I know of for installing Python and you have multiple versions of Python installed with Brew. Take a look in your /usr/local/bin folder. ls /usr/local/bin/python* at the terminal will do it. You probably will have a python2 and maybe a python3 in there, maybe more than one. Brew puts a number after Python for the major version to distinguish it from the system Python. Assuming everything went smooth with Brew then at the terminal you should be able to type python2 -V and or python3 -V depending on which version of python you installed with brew. A reasonably good tutorial for setting up Python on a mac is here http://docs.python-guide.org/en/latest/starting/install/osx/
If you have brew installed and Python 2.x.x run...
brew upgrade python#2
If you have brew installed and no Python 2.x.x run...
brew install python#2

Categories

Resources