Python not showing correct version on Mac OSX - python

I am trying to update Python to 3.7.4 on my Mac. Currently when I type python3 --version, it shows that I have Python 3.7.3 I have tried to update using just the standalone installer from the website as well as homebrew but even through all of these methods, python3 --version still returns to me 3.7.3. Even when I try to upgrade with Brew, it says that 3.7.4 is installed...
Picture for reference:
Please help and thank you in advance!

Update: Check what python version you are using by running which python, at this point you might figure out what version of Python is being used to solve this problem. Otherwise, follow up this steps:
I would remove all Python installations and use Homebrew to install it.
First, run which python:
/Library/Frameworks/Python.framework/Versions/2.7/bin/python
Delete the entire Python.framework directory from /Library/Frameworks.
Second, run which python3:
/usr/local/bin/python3
Again, delete the entire python3 directory.
Now use brew doctor in order to see possible issues with symlinks. If you have issues run brew prune or brew cleanup --prune. This will remove all of the symlinks.
Reinstall python and python3 via homebrew:
brew install python

Some older apps may count on the Python2.7 framework being installed. It sounds like you may not have your bash $PATH set to look for homebrew binaries before the built-in ones. You can edit your ~/.profile file, and make sure that /usr/local/bin is the first entry by adding the following line to the end of .profile:
export PATH=/usr/local/bin:$PATH

ln -s -f /usr/local/bin/python /usr/local/bin/python
https://dev.to/malwarebo/how-to-set-python3-as-a-default-python-version-on-mac-4jjf

Just type in the below and it'll work
python3 --version

Related

Use python 3.8 instead of 3.6 on elementary OS (or Ubuntu)

I recently started using elementary OS. It's based on Ubuntu.
During installation it installs python 3.6.
I installed python 3.8 manually by installing the following packages: python3.8, python3.8-dev, python3.8-minimal, python3.8-venv.
I also updated the link to the python binary with:
sudo ln -sf /usr/bin/python3.8 /usr/bin/python3
After this a couple of things stopped working.
For example when I tried to execute a non-existing command it didn't print the error message that it cannot found the command, but it displayed a python stack trace. That one I solved with:
cd /usr/lib/python3/dist-packages
sudo ln -s apt_pkg.cpython-36m-x86_64-linux-gnu.so apt_pkg.so
sudo ln -s apt_inst.cpython-36m-x86_64-linux-gnu.so apt_inst.so
As you can see the python error was because it couldn't find the compiled binaries for the apt module.
So this issue was solved, but there are a couple of similar ones, and none of them can be solved this way, as the module binary is not compatible with python 3.8.
Is it possible to remove python 3.6 completely and override it with 3.8 so that the module binaries also get updated? Or can python 3.8 and 3.6 coexist? I would be fine with the /usr/bin/python3 link pointing to python 3.6 and I would manually execute /usr/bin/python3.8 or create a different alias or link for it. However when I print out the sys.path with /usr/bin/python3.8 I get this:
/usr/lib/python38.zip
/usr/lib/python3.8
/usr/lib/python3.8/lib-dynload
/home/{username}/.local/lib/python3.8/site-packages
/usr/local/lib/python3.8/dist-packages
/usr/lib/python3/dist-packages
The trouble is with the last one. That's where the modules are with the 3.6 compatible so files. Can I somehow force python 3.8 to completely ignore the last module search path (without always stating sys.path.remove in my scripts) ?
I recommend you undo your symlink overwriting your systemwide version of Python 3. As you have found, replacing the python3 executable might create some problems, as it is used under the hood.
In general, you should leave your system python[3] installation alone for this reason and it is common to use virtual environments. This can be done as follows (assuming you are in some project directory):
python3.8 -m venv venv
source venv/bin/activate
The first command will create a virtual environment ('venv') in the directory venv. The second command will 'activate': now python (and in this case, python3 and python3.8) all refer to your original python3.8 in the context of this shell. You will have to repeat this if you launch a new shell.
This will also allow you to install packages using pip without cluttering your system installation. The use of virtual environments and pip is an incredibly common workflow in the Python development world.
In terms of shell and 'global' Python management, you can also use pyenv to manage your Python versions and what is available in the shell. pyenv is quite nice if you want to run a particular version of python, say 3.8.0 but not 3.8.1.

How to run Python2 after Homebrew Update?

I used to have python and python3 installed. So I could decide which python to use. I now updated python with homebrew and it migrated python3 to python. How do I run python2 now?
When I check the installation I get:
$ python -V
Python 3.6.4
$ python2 -V
-bash: python2: command not found
$ python3 -V
Python 3.6.4
If I try to simply reinstall python2 I get:
$ brew install python2
Warning: python#2 2.7.14_1 is already installed
However I cannot use python2.
python2: command not found
You're missing the symbolic link that Homebrew makes from the Cellar to the actual bin directory on your path.
Use:
brew link python2
to fix that.
You may run into a warning:
Warning: python#2 is keg-only and must be linked with --force Note that doing so can interfere with building software.
See the accepted answer to this SO question for some details on that.
In most case, you can then safely use
brew link --force python2
if you're not planning building your own libraries that require the source code (libpython.so and Python.h) for Python 2.
If you do require the source code, you'll need to provide the include paths and library paths to e.g. /usr/local/Cellar/python/2.7.14_3/Frameworks/Python.framework/Versions/2.7/include/python2.7 and /usr/local/Cellar/python/2.7.14_3/Frameworks/Python.framework/Versions/2.7/lib/. But that's a different topic or question.
To use pip for Homebrew's Python 2, best to use is
python2 -m pip <command>
Then you can clearly see what Python your pip command goes with, and keep it apart from Python 3 (which would be python3 -m pip).
Note
If you have Homebrew problems, first cause of action is to run
brew doctor
The error messages are usually quite helpful to fix at some of the problems.
It seems that Homebrew has changed things again - as I had both python 2.7.x and 3.x.x installed through Homebrew for the same reasons. After the initial update, python2 would no longer work, but if you used python#2 you would get access as before.
Now however, they have seemed to partially revert some of these changes. Calling python now points to the keg-only 2.7.x homebrew installation instead of the 3.x.x installation. In addition, python#2 no longer works, but python2 no does. python3 still points to the 3.x.x installation as before.
[NOTE: I have not modified my ~/.bash_profile for any of these changes to occur.]

Python not finding pip modules MacOS

I have installed some modules using pip and whenever I try to import them in Python I am told that no module exists. I think there is something wrong with my paths. This is some terminal output, does anyone know how I can fix this?
Nicks-MacBook-Pro:~ nickporter$ which python
/usr/bin/python
Nicks-MacBook-Pro:~ nickporter$ which pip
/usr/local/bin/pip
Nicks-MacBook-Pro:~ nickporter$
You can use pip freeze to find the packages installed.
I do not know whether you use a virtualenv. If you use it, you have to source it, to activate it.
It looks like you probably have multiple installations of Python or pip (or both). First, determine if you have multiple Python binaries installed and decide on the one you want to use by default.
You clearly have /usr/bin/python...check if there is another one in /usr/local/bin/python. If you do have one in /usr/local/bin, I'm guessing you installed it with Homebrew. Pick the Python you want to use (if there are multiple) by playing with your PATH. If you want to use /usr/local/bin/python (assuming it exists) ensure /usr/local/bin comes before /usr/bin in your PATH.
Once you do that, I would remove your current pip. After you remove it, look for it again with which pip, just to make sure you don't have any old one laying around. Once you've totally removed it, reinstall.
If you're installing with Homebrew, I'd stick to using the Homebrew python and pip. If you're not using Homebrew I'd install pip using: https://bootstrap.pypa.io/get-pip.py
curl -q https://bootstrap.pypa.io/get-pip.py | sudo python
You have homebrew version of python shadowed by the system python, but because system python doesn't have pip, a pip you see is actually a brew one.
I found, that the reason for this is that brew by default names python2.7 as python2 in /usr/local/bin (probably for preventing possible compatibility issues with some system services (?)) , i.e. you may try to run python2 or try to ln /usr/local/bin/python2 /usr/local/bin/python and restart the term session.
Make sure that /usr/local/bin goes before /usr/bin and /bin in your $PATH.
BTW. brew info {package} often contains recommendations for proper configuration of packages.

Which python am I using?

Having trouble making sure I'm actually using the latest versions of Python even though they are already installed via homebrew.
$ brew upgrade python3
Error: python3 3.6.2 already installed
but:
$ python3 --version
Python 3.6.0
same goes for python2:
$ brew upgrade python
Error: python2 2.7.13_1 already installed
$ python --version
Python 2.7.10`
❯ echo `which python`
If this doesn't print '/usr/local/bin/python' (where brew actually installs binaries), then there's something wrong with your $PATH (probably '/usr/local/bin' is not there or it's inserted after '/usr/bin', so the system default python is being run instead).
++ it seems that brew names its python2.7 as python2 by default, and not as python, so you may also need to create a python2->python link in /usr/local/bin directory.
Do you have separate environment variables set up for each?
I have found in the past that having multiple versions of python 2 for example, without all the environment variables, can get quite confusing!
You may find when typing python3, windows is only looking at python 3.0, unless you are in the python 3.6.2 directory.
First I modified my $PATH: sudo nano /etc/paths so that /Library/Frameworks/Python.framework/Versions/3.6/bin was not being invoked. I made sure my paths were in the right order so that python looked for /usr/local/bin/python3 and /usr/local/bin/python2 first to force the issue.
However, $ python3 --version still returned
Python 3.6.0, though brew says python3 3.6.2 already installed.
brew doctor to the rescue: homebrew recommended a couple things. python was incorrectly symlinked so I ran brew --overwrite python3. Finally, it diagnosed python coming from /Library/Frameworks/Python.framework/Versions/3.6/bin, so I sudo rm -rf /Library/Frameworks/Python.framework. Now python versions are correct.
Homebrew brew doctor warning about /Library/Frameworks/Python.framework, even with brew's Python installed
Thank you everyone for your help.

How do I use brew installed Python as the default Python?

I try to switch to Homebrew (after using fink and macport) on Mac OS X 10.6.2. I have installed python 2.7 with
brew install python
The problem is that, contrary to Macport, it seems that there is no python_select utility, and my default mac python is always default
which python
give me
/usr/bin/python
and /usr/bin/python is not a symlink
How can I do to make python brew flavour to be my default python ?
As suggested by the homebrew installer itself, be sure to add this to your .bashrc or .zshrc:
export PATH="/usr/local/opt/python/libexec/bin:$PATH"
As you are using Homebrew the following command gives a better picture:
brew doctor
Output:
==> /usr/bin occurs before /usr/local/bin This means that system-provided programs will be used instead of those provided by
Homebrew. This is an issue if you eg. brew installed Python.
Consider editing your .bash_profile to put: /usr/local/bin ahead of
/usr/bin in your $PATH.
See: How to symlink python in Homebrew?
$ brew link --overwrite python
Linking /usr/local/Cellar/python/2.7.3... 28 symlinks created
$ which python
/usr/local/bin/python
Quick fix:
Open /etc/paths
Change the order of the lines (highest priority on top)
In my case /etc/paths looks like:
/usr/local/bin
/usr/local/sbin
/usr/bin
/bin
/usr/sbin
/sbin
If you want to know more about paths in OSX I found this article quite useful:
http://muttsnutts.github.com/blog/2011/09/12/manage-path-on-mac-os-x-lion/
I did "brew install python" for OSX High Sierra. The $PATH had /usr/local/bin before any other path but still which python was pointing to the system's python.
When I looked deeper I found that there is no python executable at /usr/local/bin. The executable is named python2. To fix this problem create a symbolic link python pointing to python2:
/usr/local/bin $: ln -s python2 python
For Apple Silicon machines, the path are slightly different. After running brew install python, you must ensure your ~/.zshrc uses the correct Homebrew paths:
# Homebrew
eval "$(/opt/homebrew/bin/brew shellenv)"
# Homebrew: Python
export PATH="/opt/homebrew/opt/python/libexec/bin:$PATH"
Results:
% which python
/opt/homebrew/opt/python/libexec/bin/python
% python --version
Python 3.9.9
% which pip
/opt/homebrew/opt/python/libexec/bin/pip
% pip -V
pip 21.3.1 from /opt/homebrew/lib/python3.9/site-packages/pip (python 3.9)
python formula now uses python3(v3.6.5 for now), brew will link the directory:
/usr/local/opt/python -> ../Cellar/python/3.6.5
it will also link the binary:
/usr/local/bin/python3 -> ../Cellar/python/3.6.5/bin/python3
If you still need to use python2.x, use:
brew install python#2
To use homebrew's python, just put its directory in PATH, for bash:
export PATH="/usr/local/opt/python/libexec/bin:$PATH"
for fish:
set -x PATH /usr/local/opt/python/libexec/bin $PATH
Note:
doing this will shadow the system default version of python
homebrew used to link python to /usr/local/share/python in older versions.
Homebrew does NOT replace stuff in "/usr/bin". You'll just want to put "/usr/local/bin" ahead of "/usr/bin" in your path, then "which python" will give you "/usr/local/bin/python".
Replacing /usr/bin/python (or /usr/bin/ruby) is highly unrecommended.
Modify your $PATH, Add this in your bashrc or bash_profile:
export PATH=/usr/local/bin:/usr/local/sbin:~/bin:$PATH
more click here:
Issue #89791
I did brew install python, my $PATH was good, but still, which python gave me the system installed one. Restarting the terminal fixed it.
You need to edit your PATH environmental variable to make sure wherever the homebrew python is located is searched before /usr/bin. You could also set things up in your shell config to have a variable like PYTHON be set to your desired version of python and call $PYTHON rather than python from the command line.
Also, as another poster stated (and especially on mac) DO NOT mess with the python in /usr/bin to point it to another python install. You're just asking for trouble if you do.
python now points to python3, if you need python 2 then do:
brew install python#2 and then in your .zshrc or .bashrc file
export PATH="/usr/local/opt/python#2/libexec/bin:$PATH"
Now, pyhon --version = Python 2.7.14 and python3 --version = Python 3.6.4.
That's the behavior I'm used to seeing in my terminal.
I believe there are means to make homebrew python default, but in my opinion the proper way to solve a problem is not to mess with system python paths: it is better to create a virtualenv in which homebrew python would be default (by using virtualenv --python option). Using tools like python_select is almost always a bad idea.
Use pyenv instead to install and switch between versions of Python. I've been using rbenv for years which does the same thing, but for Ruby. Before that it was hell managing versions.
Consult pyenv's github page for installation instructions. Basically it goes like this:
- Install pyenv using homebrew. brew install pyenv
- Add a function to the end of your shell startup script so pyenv can do it's magic. echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n eval "$(pyenv init -)"\nfi' >> ~/.bash_profile
Use pyenv to install however many different versions of Python you need. pyenv install 3.7.7.
Set the default (global) version to a modern version you just installed. pyenv global 3.7.7.
If you work on a project that needs to use a different version of python, look into pyevn local. This creates a file in your project's folder that specifies the python version. Pyenv will look override the global python version with the version in that file.
Add the /usr/local/opt/python/libexec/bin explicitly to your .bash_profile:
export PATH="/usr/local/opt/python/libexec/bin:$PATH"
After that, it should work correctly.
Just do:
brew install python
brew link python
After doing that, add this to your bashrc or bash_profile:
alias python='/usr/local/bin/python2'
Enjoy!
You can edit /etc/paths. Here is mine:
/usr/local/bin
/usr/bin
/bin
/usr/sbin
/sbin
Then add a symlink for the python version. In my case
$ cd /usr/local/bin
$ ln -s python3 python
Voila!
If you are fish shell
echo 'set -g fish_user_paths "/usr/local/opt/python/libexec/bin" $fish_user_paths' >> ~/.config/fish/config.fish
try this
which python3
Try typing python3 instead of just python
Greeting folks! I have the need to use python 3.10 version to harness its new features. My solved solution at 2022-07-03 is as follows. Have fun coding python!
» rm '/usr/local/bin/pip3.10'
» brew link python#3.10
» echo 'export PATH="/usr/local/opt/python#3.10/bin:$PATH"' >> ~/.zshrc
» python3
Python 3.10.5 (main, Jun 23 2022, 17:15:25) [Clang 13.1.6 (clang-1316.0.21.2.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
Since High Sierra, you need to use:
sudo chown -R $(whoami) $(brew --prefix)/*
This is because /usr/local can no longer be chowned
brew link python
And you must create/add an alias for python and put it in your .zprofile (Located in Users/username folder, if you press Shift+command+.
This must point to your homebrew python installation location.
alias python ='opt/homebrew/bin/python3'
No idea what you mean with default Python. I consider it bad practice to replace the system Python interpreter with a different version. System functionality may depend in some way on the system Python and specific modules or a specific Python version. Instead install your custom Python installations in a safe different place and adjust your $PATH as needed in order to call you Python through a path lookup instead of looking for the default Python.

Categories