Python2 version set to Python3 in version check - python

So I was trying to install OpenCV in on the MacOS following this tutorial: https://www.pyimagesearch.com/2016/12/19/install-opencv-3-on-macos-with-homebrew-the-easy-way/
In step 3 the versions of python should be python 2.x checked with python --version and python 3.y checked with python3 --version. However on both commands I am getting Python 3.6.4. How can I fix this? I have tried to install python 2 again with brew install python#2 and the output is python#2 2.7.14_1 is already installed. which python shows /usr/local/bin/python whereas which python3 shows /usr/local/bin/python3.

If you installed Python 2 and Python 3 with Homebrew, then the Python 2 binary is named python2, but will not be linked into /usr/local unless you use brew link with the --force flag.
See brew info python#2:
$ brew info python#2 | grep Caveats -A 4
==> Caveats
This formula installs a python2 executable to /usr/local/opt/python#2/bin
If you wish to have this formula's python executable in your PATH then add
the following to ~/.bash_profile:
export PATH="/usr/local/opt/python#2/libexec/bin:$PATH"
$ brew info python#2 | grep 'not symlinked' -A 2
This formula is keg-only, which means it was not symlinked into /usr/local,
because this is an alternate version of another formula.
You can run
/usr/local/opt/python\#2/bin/python2
if you need to run it directly, or you can force homebrew to link it in anyway with:
brew link --force python#2
at which point /usr/local/bin/python2 will be added.
The tutorial is rather outdated; how homebrew handles Python has changed. Just use python2 wherever it uses python.
To further address the tutorial:
brew linkapps is deprecated
the homebrew/science tap is deprecated, the formulas in that tap have been migrated, opencv3 can be installed without additional taps.
the current version of the opencv3 natively supports both Python 2 and 3, no configuration switches required
the formula directly depends on the Python 2 and 3 formulae, installing opencv3 will automatically install Python.
So just run brew install opencv3, follow any additional instructions that command prints (could be none).
Do make sure you update Homebrew as there were some dependencies missing (see my bug report with Homebrew). If you don't, you'll have to run brew install hdf5 tesseract to install dependencies that should have been optional.
The tutorial is almost entirely obsolete now.

Related

Install python#2 on Mac with M1 chip

Long story short, I want to install Apache Cassandra on a MacBook Pro with M1 chipset.
In order to install it, one must install other softwares before, one of them being python which must be updated to the latest version. I have followed this tutorial.
This is what terminal shows up when asking about python existing version:
> python --version
Python 2.7.16
In tutorial they are updating it to 2.7.17, so I try to do the same:
> brew install python#2
and this is the output:
Warning: No available formula or cask with the name "python#2". Did you mean bpython, ipython, jython or cython?
==> Searching for similarly named formulae...
These similarly named formulae were found:
bpython ipython jython cython
To install one of them, run (for example):
brew install bpython
==> Searching for a previously deleted formula (in the last month)...
Error: No previously deleted formula found.
==> Searching taps on GitHub...
Error: No formulae found in taps.
Any ideas how to overcome this issue?
I could install 2.7.18 on M1.
brew install pyenv
pyenv install 2.7.18
Set the python version.
pyenv global 2.7.18
Export PATH if necessary.
export PATH="$(pyenv root)/shims:${PATH}"
Add if necessary.:
echo 'PATH=$(pyenv root)/shims:$PATH' >> ~/.zshrc
Sometimes you will get some errors like missing something
if you are using rosetta then run
arch -arm64 pyenv install 2.7.18

install caffe on mac " Error: invalid option: --with-python"

when I install the caffe on mac according to :
Remaining dependencies, with / without Python
# with Python pycaffe needs dependencies built from source
brew install --build-from-source --with-python -vd protobuf
brew install --build-from-source -vd boost boost-python
# without Python the usual installation suffices
brew install protobuf boost
I paste the second one to the terminal, and result is:
Error: invalid option: --with-python
I can not find any similar problem, can some help me with that?
Options have been removed from Homebrew core formulas and only exist in taps now. See this GitHub issue. Building with python has been made the default behavior for protobuf.
See the formula, lines 49-51.
chdir "python" do
system "python3", *Language::Python.setup_install_args(libexec),
"--cpp_implementation"
So it should be fine to run
brew install --build-from-source -vd protobuf

Attempted install of Python 3 via homebrew fails on Mac OS X Sierra

I'm trying to install Python 3 alongside 2.7 with Homebrew but am receiving an error message I can't find a resolution to.
When attempting brew update && brew install python3 I get the following error:
Error: python 2.7.12_2 is already installed
To upgrade to 3.6.4_3, run `brew upgrade python`
I want to leave the python 2.7 installation alone so I can have both Python 2 & 3 accessible on my machine so I'm nervous that upgrading will overwrite the current 2.7 installation.
I figure I can still perform a clean side-by-side install with the package from python.org, but I want to know why I'm getting this homebrew error
brew doctor shows the following Warnings containing python
Warning: "config" scripts exist outside your system or Homebrew directories.
`./configure` scripts often look for *-config scripts to determine if
software packages are installed, and what additional flags to use when
compiling and linking.
Having additional scripts in your path can confuse software installed via
Homebrew if the config script overrides a system or Homebrew provided
script of the same name. We found the following "config" scripts:
/Library/Frameworks/Python.framework/Versions/2.7/bin/python-config
/Library/Frameworks/Python.framework/Versions/2.7/bin/python2-config
/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7-config
Warning: Python is installed at /Library/Frameworks/Python.framework
Homebrew only supports building against the System-provided Python or a
brewed Python. In particular, Pythons installed to /Library can interfere
with other software installs.
Warning: Some installed formulae are missing dependencies.
You should `brew install` the missing dependencies:
brew install python#2
To be honest, and what I also have on my own system, is Python 3 as the default and Python 2 available if I need it. With homebrew, you can just update your default as it wants you to do with the upgrade. That means when you run python, Python 3 will run as the default.
For Python 2, install brew install python#2. When you want to run Python 2, just run python2 in the terminal and you'll have it.
Python install will run once you fix the broken links by running brew link, and the missing link component.
These usually don't need SUDO, they will link then and run brew doctor once they have all been linked.

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.]

How can I use Homebrew to install both Python 2 and 3 on Mac?

I need to be able to switch back and forth between Python 2 and 3. How do I do that using Homebrew as I don't want to mess with path and get into trouble.
Right now I have 2.7 installed through Homebrew.
I would use pyenv You can install it:
$ brew install pyenv
To enable pyenv in your Bash shell, you need to run:
$ eval "$(pyenv init -)"
To do this automatically for Bash upon startup, add that line to your ~/.bash_profile. 1
Usage:
Once you have installed pyenv and activated it, you can install different versions of python and choose which one you can use. Example:
$ pyenv install 2.7.5
You can check the versions you have installed with:
$ pyenv versions
And you can switch between python versions with the command:
$ pyenv global 3.3.1
Also you can set a python version for the current directory with:
$ pyenv local 3.5.2
You can check by running python --version:
$ python --version
Python 3.5.2
1 Homebrew used to instruct you to do this upon installation of pyenv, but the message was removed. For Zsh and other shells, the precise steps may be different.
You can have both versions installed at the same time.
For Homebrew >=1.5.0:
Since 1st March 2018 the python formula will be upgraded to Python 3.x, while a new python#2 formula will be added for Python 2.7, specifically.
See changes announcement here or the final doc about using Homebrew for Python here.
For older Homebrew:
For Python 2.x:
brew install python
For Python 3.x:
brew install python3
Now, you will have both the versions installed in your machine. When you want to use version 2, use the python executable. When you want to use version 3, use the python3 executable.
Currently Homebrew provides two different formulas for Python 2 and 3. brew install python installs python3, and brew install python#2 installs python2. More details in Homebrew docs:
https://docs.brew.sh/Homebrew-and-Python
If you currently have 2.x installed via Homebrew, Homebrew will give you a message such as:
Error: python 2.7.14 is already installed
To upgrade to 3.6.5, run `brew upgrade python`
If you run:
brew upgrade python
you should be able to do:
python --version
and
python3 --version
To see what versions of Python 2.x and 3.x installed.
There are ways to use both , but the simplest solution today is to use pyenv. pyenv allows easy switching between versions.
Here is what I did to set up:
STEP1:
Remove all pythons from your mac
brew uninstall --ignore-dependencies --force python
sudo rm -rf ~/miniconda3/
sudo rm -rf ~/.conda/
Remove the following from ~/.bash_profile
export PATH="/Users/ishandutta2007/miniconda3/bin:$PATH"
and also the following from ~/.bashrc
export PYTHONPATH=/usr/local/lib/python2.7/site-packages:$PYTHONPATH
export PYTHONPATH=/usr/local/lib/python2.7/site-packages/google:$PYTHONPATH
alias python="/usr/bin/python"
STEP2:
Install pyenv and the python versions you need
brew update
brew install pyenv
pyenv install 2.7
pyenv install 3.7.0
STEP3:
add pyenv init to bash_profile or bashrc
echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n eval "$(pyenv init -)"\nfi' >> ~/.bash_profile
STEP4:
Check what got installed
pyenv versions
system (set by /Users/ishandutta2007/.pyenv/version)
2.7
3.7.0
STEP5:
Choose a default
pyenv global 3.7.0
When a project needs older version, just go its root folder and run
pyenv local 2.7
Alternatively, you probably can just enter "python3" to run your most current version of python3.x and "python" or "python2" to run the latest installed 2.x version.
Use asdf !
Ballad of asdf
Once upon a time there was a programming language
There were many versions of it
So people wrote a version manager for it
To switch between versions for projects
Different, old, new.
Then there came more programming languages
So there came more version managers
And many commands for them
I installed a lot of them
I learnt a lot of commands
Then I said, just one more version manager
Which I will write instead
So, there came another version manager
asdf version manager - https://github.com/asdf-vm/asdf
A version manager so extendable
for which anyone can create a plugin
To support their favourite language
No more installing more version managers
Or learning more commands
https://github.com/asdf-vm/asdf
https://github.com/tuvistavie/asdf-python
https://github.com/asdf-vm/asdf-plugins
I thought I had the same requirement - to move between Python versions - but I achieved all I needed with only Python3.6 by building from source instead of using homebrew.
git clone https://git.<theThingYouWantToInstall>
Depending on the repo, check if there is MAKE file already setup for this option.
I was able to just go to https://www.python.org/downloads/mac-osx/ and download the latest python. It installed along side current python in my system.
Okay, I was struggling with my brew installation of Python3, because I didn't have pip3
sudo pip3 command not found
and so I did
brew uninstall --force --ignore-dependencies python3
and installed the regular Python 3.6.2 from official distribution and then I had pip3 and all components were ok.

Categories