How to make Mac OS use the python installed by Homebrew - python

I have searched online for a while for this question, and what I have done so far is
installed python32 in homebrew
changed my .bash_profile and added the following line to it:
export PATH=/usr/local/bin:/usr/local/sbin:~/bin:$PATH
but when I close the terminal and start again, I type 'which python', it still prints:
/usr/bin/python
and type 'python --version' still got:
Python 2.7.2
I also tried the following instruction:
brew link --overwrite python
or try to remove python installed by homebrew by running this instruction:
brew remove python
but both of the above two instructions lead to this error:
Error: No such keg: /usr/local/Cellar/python
can anybody help, thanks

brew install python or brew info python output mentions:
Unversioned symlinks python, python-config, pip etc. pointing to
python3, python3-config, pip3 etc., respectively, have been installed into
/opt/homebrew/opt/python#3.9/libexec/bin
So running
% echo 'export PATH=/opt/homebrew/opt/python#3.9/libexec/bin:$PATH' >> ~/.zprofile
% source ~/.zprofile
# For bash use ~/.bash_profile.
gets you those symlinks created by Homebrew - python=python3, pip=pip3 etc
% python --version
Python 3.9.10
% pip --version
pip 21.3.1 from /opt/homebrew/lib/python3.9/site-packages/pip (python 3.9)

If you want to install Python 3 using Homebrew:
$ brew install python3
==> Downloading http://python.org/ftp/python/3.3.0/Python-3.3.0.tar.bz2
Already downloaded: /Library/Caches/Homebrew/python3-3.3.0.tar.bz2
==> ./configure --prefix=/usr/local/Cellar/python3/3.3.0 --enable-ipv6 --datarootdir=/usr/local/Cell
==> make
==> make install PYTHONAPPSDIR=/usr/local/Cellar/python3/3.3.0
==> make frameworkinstallextras PYTHONAPPSDIR=/usr/local/Cellar/python3/3.3.0/share/python3
==> Downloading https://pypi.python.org/packages/source/d/distribute/distribute-0.6.35.tar.gz
Already downloaded: /Library/Caches/Homebrew/distribute-0.6.35.tar.gz
==> /usr/local/Cellar/python3/3.3.0/bin/python3.3 -s setup.py install --force --verbose --install-li
==> Downloading https://pypi.python.org/packages/source/p/pip/pip-1.3.1.tar.gz
Already downloaded: /Library/Caches/Homebrew/pip-1.3.1.tar.gz
==> /usr/local/Cellar/python3/3.3.0/bin/python3.3 -s setup.py install --force --verbose --install-li
==> Caveats
Homebrew's Python3 framework
/usr/local/Cellar/python3/3.3.0/Frameworks/Python.framework
Distribute and Pip have been installed. To update them
pip3 install --upgrade distribute
pip3 install --upgrade pip
To symlink "Idle 3" and the "Python Launcher 3" to ~/Applications
`brew linkapps`
You can install Python packages with
`pip3 install <your_favorite_package>`
They will install into the site-package directory
/usr/local/lib/python3.3/site-packages
Executable python scripts will be put in:
/usr/local/share/python3
so you may want to put "/usr/local/share/python3" in your PATH, too.
See: https://github.com/mxcl/homebrew/wiki/Homebrew-and-Python
Once installed update your system PATH variable, add the next line to ~/.bash_profile
export PATH=/usr/local/bin:/usr/local/sbin:~/bin:$PATH
And then:
$ source ~/.bash_profile
Now launch Python:
$ python3
Python 3.3.0 (default, Mar 26 2013, 10:01:40)
[GCC 4.2.1 Compatible Apple LLVM 4.2 (clang-425.0.27)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
You can check python3 path:
$ which python3
/usr/local/bin/python3

You may try adding this line to your .bash_profile
alias python='python3'

I came through the same issue and did some research. I found that someone has created a bug for the same issue under the azure/cli repository. You can find that issue here. I am providing the same solution here which was very easy and fixed my issue:
Most probably the Brew is broken and needs some patching or fixing. So run brew doctor command which will give you a summary about what is happening.
Below is what I got:
mymac:bin sidmishra$ brew doctor
Please note that these warnings are just used to help the Homebrew maintainers
with debugging if you file an issue. If everything you use Homebrew for is
working fine: please don't worry or file an issue; just ignore this. Thanks!
Warning: The following directories do not exist:
/usr/local/sbin
You should create these directories and change their ownership to your account.
sudo mkdir -p /usr/local/sbin
sudo chown -R $(whoami) /usr/local/sbin
Warning: Unbrewed dylibs were found in /usr/local/lib.
If you didn't put them there on purpose they could cause problems when
building Homebrew formulae, and may need to be deleted.
Unexpected dylibs:
/usr/local/lib/LibSideSyncOSX9.dylib
/usr/local/lib/ss_conn_lib.dylib
Warning: You have unlinked kegs in your Cellar
Leaving kegs unlinked can lead to build-trouble and cause brews that depend on
those kegs to fail to run properly once built. Run `brew link` on these:
python
The awesome stuff about the command brew doctor is that it not only tells you issues but also suggests you the solution steps in most of the cases.
So, I ran all the commands suggested by the brew and to link I ran the following command:
brew link python
Above command threw me an error:
mymac$ brew link python
Linking /usr/local/Cellar/python/3.7.1... Error: Permission denied # dir_s_mkdir - /usr/local/Frameworks/Python.framework
It seems that /urs/local/Frameworks doesn't have enough rights for my current user. So, I ran the following command and gave enough rights to my current user:
sudo chown -R $(whoami) /usr/local/Frameworks/
After running above command I ran linking command again, and it worked!!!
mymac$ brew link python
Linking /usr/local/Cellar/python/3.7.1... 1 symlinks created
Now run following command to get the current selected python version:
python --version
Above command should give you 3.7.1 (as of 21st Dec 2018) or new version for the python. There might be a chance that your Mac would have python2 set by default. If the version is not python3 then you have to a couple of steps to use the latest python3 over python2 version. Here are the steps:
Using Shell:
Open ~/.bash_loginor ~/.bash_profileor ~/.cshrcor ~/.profileor ~/.tcshrcor ~/.zprofile, whatever shell you are using for commands, in edit mode. You may have to use sudo to edit them.
Add following steps to it:
PATH="/Library/Frameworks/Python.framework/Versions/3.2/bin:${PATH}"
export PATH
Also, add following for backup:
alias python=python3
OR
Using homebrew:
Run following commands to unlink python2 and link python3:
mymac$ brew unlink python#2
mymac$ brew link python#3
Above will unlink python2 and link python3.
Hope some of you will get helped from this answer.
Good Day!!!

From $ brew info python:
This formula installs a python2 executable to /usr/local/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/libexec/bin:$PATH"
Then confirm your python executable corresponds to the correct installation:
$ which python or
$ python --version

Installing with Homebrew is recommended on macOS. That being said, Python 2.7 comes with Mac OS; however, it is deprecated and will be removed soon. As such, you should be using Python3 and newer.
Install Homebrew.
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
Run brew install python
Once Python is installed, Homebrew will say that the installation is complete, but that you already have Python 2.7 installed. This is nice, but we want to set it to actually see python3 as an option
Use brew link
Confirm by running which python3, the path should be /usr/local/bin/python3

Related

Have python 3 and pip 3 installed but pip -V shows 19.2.3

I have followed the instructions at https://opensource.com/article/19/5/python-3-default-mac:
Now, python -V shows 3.8.1, but pip -V still shows 19.2.3.
I checked with which pip3, which shows
/Library/Frameworks/Python.framework/Versions/3.8/bin/pip3, but using
echo "alias pip=/Library/Frameworks/Python.framework/Versions/3.8/bin/pip3>> ~/.zshrc does not seem to work.
pip -V gives: pip 19.2.3 from /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pip (python 3.8).
I have MacOS Cataline 10.15.3. Can someone help please?
There seems nothing wrong with your setup. pip has a version number that is independent of that of python. Your python version is 3.8 and the corresponding pip (which resides in python3.8/site-packages/pip) has version 19.2.3. You have set up everything just fine
In fact I was trying to install python in pyenv and get my IDLE to work. The following worked for me (mostly from https://github.com/pyenv/pyenv/issues/1375; Installed Python 3 on Mac OS X but its still Python 2.7; https://opensource.com/article/19/5/python-3-default-mac, with a few additional steps). Thanks for your help. I posted my solution at https://github.com/pyenv/pyenv/issues/1375, too, for reference)
ran $brew install pyenv
ran $brew install tcl-tk
Output after "brew reinstall tcl-tk':
tcl-tk is keg-only, which means it was not symlinked into /usr/local,
because tk installs some X11 headers and macOS provides an (older) Tcl/Tk.
If you need to have tcl-tk first in your PATH run:
echo 'export PATH="/usr/local/opt/tcl-tk/bin:$PATH"' >> ~/.zshrc
(I added it to ~/.zshrc as the first line)
For compilers to find tcl-tk you may need to set:
export LDFLAGS="-L/usr/local/opt/tcl-tk/lib"
export CPPFLAGS="-I/usr/local/opt/tcl-tk/include"
(I added these two to ~/.zshrc, too, after adding the above)
For pkg-config to find tcl-tk you may need to set:
export PKG_CONFIG_PATH="/usr/local/opt/tcl-tk/lib/pkgconfig"
(I added this two to ~/.zshrc, too, after adding the above)
To get tcl-tk 8.6 to work with the pyenv install of python, I found:
/usr/local/Cellar/pyenv/1.2.13/plugins/python-build/bin/python-build
and replaced the following:
$CONFIGURE_OPTS ${!PACKAGE_CONFIGURE_OPTS} "${!PACKAGE_CONFIGURE_OPTS_ARRAY}" || return 1
with:
$CONFIGURE_OPTS --with-tcltk-includes='-I/usr/local/opt/tcl-tk/include' --with-tcltk-libs='-L/usr/local/opt/tcl-tk/lib -ltcl8.6 -ltk8.6' ${!PACKAGE_CONFIGURE_OPTS} "${!PACKAGE_CONFIGURE_OPTS_ARRAY}" || return 1
ran $pyenv install 3.8.1
Installed Python-3.8.1 to /Users/ryan/.pyenv/versions/3.8.1
ran $pyenv global 3.8.1
Refreshed the current terminal and checked
$pyenv version
output: 3.8.1 (set by /Users/ryan/.pyenv/version)
Ran $python -V
output: Python 3.8.1
ran $pip install --upgrade pip (since I had previously already installed pip using $pip install)
output: Successfully installed pip-20.0.2
Tested my tcl-tk installation with $python -m tkinter -c 'tkinter._test()'
Output: Tk window popped up. Hit ‘Quit’ to back to Terminal.
Ran $ idle
Output: Python 3.8.1 Shell window popped up.
The installation was done on a MacBook Pro with macOS Catalina 10.15.3.

How do you make virtualenv use a version of python it did not install? [duplicate]

I have a shared account in a web-hosting that has Python 2.4 installed, but my code is not compatible with 2.4. Is it possible to install Python 2.6 directly to Virtualenv?
Note: I don´t have permission to install it in the shared server.
Here are the options for virtualenv
$ virtualenv
You must provide a DEST_DIR
Usage: virtualenv [OPTIONS] DEST_DIR
Options:
--version show program's version number and exit.
-h, --help show this help message and exit.
-v, --verbose Increase verbosity.
-q, --quiet Decrease verbosity.
-p PYTHON_EXE, --python=PYTHON_EXE
The Python interpreter to use, e.g.,
--python=python2.5 will use the python2.5 interpreter
to create the new environment. The default is the
interpreter that virtualenv was installed with
(/usr/bin/python)
--clear Clear out the non-root install and start from scratch
--no-site-packages Don't give access to the global site-packages dir to
the virtual environment
--unzip-setuptools Unzip Setuptools or Distribute when installing it
--relocatable Make an EXISTING virtualenv environment relocatable.
This fixes up scripts and makes all .pth files
relative
--distribute Use Distribute instead of Setuptools. Set environ
variable VIRTUALENV_USE_DISTRIBUTE to make it the
default
--prompt==PROMPT Provides an alternative prompt prefix for this
environment
1) What you want to do is install python to a directory that you are able to write too.
You can follow the instructions here.
For Python 2.7.1
Python source
mkdir ~/src
mkdir ~/.localpython
cd ~/src
wget http://www.python.org/ftp/python/2.7.1/Python-2.7.1.tgz
tar -zxvf Python-2.7.1.tgz
cd Python-2.7.1
make clean
./configure --prefix=/home/${USER}/.localpython
make
make install
2) Install virtualenv
virtualenv source
cd ~/src
wget http://pypi.python.org/packages/source/v/virtualenv/virtualenv-1.5.2.tar.gz#md5=fbcefbd8520bb64bc24a560c6019a73c
tar -zxvf virtualenv-1.5.2.tar.gz
cd virtualenv-1.5.2/
~/.localpython/bin/python setup.py install
3) Create a virtualenv using your local python
virtualenv docs
mkdir /home/${USER}/virtualenvs
cd /home/${USER}/virtualenvs
~/.localpython/bin/virtualenv py2.7 --python=/home/${USER}/.localpython/bin/python2.7
4) Activate the environment
cd ~/virtualenvs/py2.7/bin
source ./activate
5) Check
(py2.7)$ python
Python 2.7.1 (r271:86832, Mar 31 2011, 15:31:37)
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()
(py2.7)$ deactivate
$ python
Python 2.6.6 (r266:84292, Sep 15 2010, 15:52:39)
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
Pre-requisites:
sudo easy_install virtualenv
sudo pip install virtualenvwrapper
Installing virtualenv with Python2.6:
You could manually download, build and install another version of Python to /usr/local or another location.
If it's another location other than /usr/local, add it to your PATH.
Reload your shell to pick up the updated PATH.
From this point on, you should be able to call the following 2 python binaries from your shell python2.5 and python2.6
Create a new instance of virtualenv with python2.6:
mkvirtualenv --python=python2.6 yournewenv
Now a days, the easiest way I found to have a more updated version of Python is to install it via conda into a conda environment.
Install conda(you may need a virtualenv for this)
pip install conda
Installing a new Python version inside a conda environent
I'm adding this answer here because no manual download is needed. conda will do that for you.
Now create an environment for the Python version you want. In this example I will use 3.5.2, because it it the latest version at this time of writing (Aug 2016).
conda create -n py35 python=3.5.2
Will create a environment for conda to install packages
To activate this environment(I'm assuming linux otherwise check the conda docs):
source activate py35
Now install what you need either via pip or conda in the environemnt(conda has better binary package support).
conda install <package_name>
Full guide with pyenv
If pyenv is not installed then install it with pyenv-installer:
$ curl https://pyenv.run | bash
To use any custom python version, e.g. 3.5.6 use the following:
pyenv install 3.5.6
pyenv virtualenv 3.5.6 NAME_OF_YOUR_ENV
cd YOUR_PROJECT_PATH
pyenv local NAME_OF_YOUR_ENV
The usual approach is to download the source and build and install locally (but not directly in virtualenv), and then create a new virtualenv using that local Python install. On some systems, it may be possible to download and install a prebuilt python, rather than building from source.
This procedure installs Python2.7 anywhere and eliminates any absolute path references within your env folder (managed by virtualenv). Even virtualenv isn't installed absolutely.
Thus, theoretically, you can drop the top level directory into a tarball, distribute, and run anything configured within the tarball on a machine that doesn't have Python (or any dependencies) installed.
Contact me with any questions. This is just part of an ongoing, larger project I am engineering. Now, for the drop...
Set up environment folders.
$ mkdir env
$ mkdir pyenv
$ mkdir dep
Get Python-2.7.3, and virtualenv without any form of root OS installation.
$ cd dep
$ wget http://www.python.org/ftp/python/2.7.3/Python-2.7.3.tgz
$ wget https://raw.github.com/pypa/virtualenv/master/virtualenv.py
Extract and install Python-2.7.3 into the pyenv dir. make clean is optional if you are doing this a 2nd, 3rd, Nth time...
$ tar -xzvf Python-2.7.3.tgz
$ cd Python-2.7.3
$ make clean
$ ./configure --prefix=/path/to/pyenv
$ make && make install
$ cd ../../
$ ls
dep env pyenv
Create your virtualenv
$ dep/virtualenv.py --python=/path/to/pyenv/bin/python --verbose env
Fix the symlink to python2.7 within env/include/
$ ls -l env/include/
$ cd !$
$ rm python2.7
$ ln -s ../../pyenv/include/python2.7 python2.7
$ cd ../../
Fix the remaining python symlinks in env. You'll have to delete the symbolically linked directories and recreate them, as above. Also, here's the syntax to force in-place symbolic link creation.
$ ls -l env/lib/python2.7/
$ cd !$
$ ln -sf ../../../pyenv/lib/python2.7/UserDict.py UserDict.py
[...repeat until all symbolic links are relative...]
$ cd ../../../
Test
$ python --version
Python 2.7.1
$ source env/bin/activate
(env)
$ python --version
Python 2.7.3
Aloha.
I'm using virtualenvwrapper and don't want to modify $PATH, here's how:
$ which python3
/usr/local/bin/python3
$ mkvirtualenv --python=/usr/local/bin/python3 env_name
You may use pyenv.
There are a lot of different versions anaconda, jython, pypy and so on...
https://github.com/yyuu/pyenv
Installation as simple as pyenv install 3.2.6
pyenv install --list
Available versions:
2.1.3
2.2.3
2.3.7
2.4
2.4.1
2.4.2
2.4.3
2.4.4
2.4.5
2.4.6
2.5
2.5.1
2.5.2
2.5.3
2.5.4
2.5.5
2.5.6
2.6.6
...
Although the question specifically describes installing 2.6, I would like to add some importants points to the excellent answers above in case someone comes across this. For the record, my case was that I was trying to install 2.7 on an ubuntu 10.04 box.
First, my motivation towards the methods described in all the answers here is that installing Python from deadsnake's ppa's has been a total failure. So building a local Python is the way to go.
Having tried so, I thought relying to the default installation of pip (with sudo apt-get install pip) would be adequate. This unfortunately is wrong. It turned out that I was getting all shorts of nasty issues and eventually not being able to create a virtualenv.
Therefore, I highly recommend to install pip locally with wget https://raw.github.com/pypa/pip/master/contrib/get-pip.py && python get-pip.py --user. This related question gave me this hint.
Now if this doesn't work, make sure that libssl-dev for Ubuntu or openssl-dev for CentOS is installed. Install them with apt-get or yum and then re-build Python (no need to remove anything if already installed, do so on top). get-pip complains about that, you can check so by running import ssl on a py shell.
Last, don't forget to declare .local/bin and local python to path, check with which pip and which python.
No, but you can install an isolated Python build (such as ActivePython) under your $HOME directory.
This approach is the fastest, and doesn't require you to compile Python yourself.
(as a bonus, you also get to use ActiveState's binary package manager)
I have not found suitable answer, so here goes my take, which builds upon #toszter answer, but does not use system Python (and you may know, it is not always good idea to install setuptools and virtualenv at system level when dealing with many Python configurations):
#!/bin/sh
mkdir python_ve
cd python_ve
MYROOT=`pwd`
mkdir env pyenv dep
cd ${MYROOT}/dep
wget https://pypi.python.org/packages/source/s/setuptools/setuptools-15.2.tar.gz#md5=a9028a9794fc7ae02320d32e2d7e12ee
wget https://raw.github.com/pypa/virtualenv/master/virtualenv.py
wget https://www.python.org/ftp/python/2.7.9/Python-2.7.9.tar.xz
xz -d Python-2.7.9.tar.xz
cd ${MYROOT}/pyenv
tar xf ../dep/Python-2.7.9.tar
cd Python-2.7.9
./configure --prefix=${MYROOT}/pyenv && make -j 4 && make install
cd ${MYROOT}/pyenv
tar xzf ../dep/setuptools-15.2.tar.gz
cd ${MYROOT}
pyenv/bin/python dep/virtualenv.py --no-setuptools --python=${MYROOT}/pyenv/bin/python --verbose env
env/bin/python pyenv/setuptools-15.2/setup.py install
env/bin/easy_install pip
echo "virtualenv in ${MYROOT}/env"
The trick of breaking chicken-egg problem here is to make virtualenv without setuptools first, because it otherwise fails (pip can not be found). It may be possible to install pip / wheel directly, but somehow easy_install was the first thing which came to my mind. Also, the script can be improved by factoring out concrete versions.
NB. Using xz in the script.
First of all, Thank you DTing for awesome answer. It's pretty much perfect.
For those who are suffering from not having GCC access in shared hosting, Go for ActivePython instead of normal python like Scott Stafford mentioned. Here are the commands for that.
wget http://downloads.activestate.com/ActivePython/releases/2.7.13.2713/ActivePython-2.7.13.2713-linux-x86_64-glibc-2.3.6-401785.tar.gz
tar -zxvf ActivePython-2.7.13.2713-linux-x86_64-glibc-2.3.6-401785.tar.gz
cd ActivePython-2.7.13.2713-linux-x86_64-glibc-2.3.6-401785
./install.sh
It will ask you path to python directory. Enter
../../.localpython
Just replace above as Step 1 in DTing's answer and go ahead with Step 2 after that. Please note that ActivePython package URL may change with new release. You can always get new URL from here : http://www.activestate.com/activepython/downloads
Based on URL you need to change the name of tar and cd command based on file received.
virtualenv --python=".localpython/bin/python2.7" env

/usr/bin/python3: Error while finding spec for 'virtualenvwrapper.hook_loader' (<class 'ImportError'>: No module named 'virtualenvwrapper')

I'm trying to follow How To Serve Django Applications with uWSGI and Nginx on Ubuntu 14.04 and I'm failing at the very earlier stage, due to amount of output, I placed all related information into Pastebin.com - #1 paste tool since 2002!.
the actual error:
root#alexus:~# echo "source /usr/local/bin/virtualenvwrapper.sh" >> ~/.bashrc
root#alexus:~# source ~/.bashrc
/usr/bin/python3: Error while finding spec for 'virtualenvwrapper.hook_loader' (<class 'ImportError'>: No module named 'virtualenvwrapper')
virtualenvwrapper.sh: There was a problem running the initialization hooks.
If Python could not import the module virtualenvwrapper.hook_loader,
check that virtualenvwrapper has been installed for
VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3 and that PATH is
set properly.
root#alexus:~#
I'm following how to to the teeth, what am I doing wrong?
I faced similar problem, when using python3 with virtualenvwrapper,
sudo apt-get install python3-pip
Then install virtualenv and virtualenvwrapper from pip3,
sudo pip3 install virtualenv virtualenvwrapper
then source it again,
source $HOME/.bashrc
Thanks to #Alexander, I was able to fix my issue by changing the line in ~/.bashrc:
export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3
to
export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python
due to backwards compatibility.
I encountered a similar issue with virtualenvwrapper complaining not being to find virtualenvwrapper.hook_loader. I was able to narrow down to this particular line in my .bash_profile
source /usr/local/bin/virtualenvwrapper.sh
as seen below
$ source /usr/local/bin/virtualenvwrapper.sh
/usr/local/opt/python3/bin/python3.6: Error while finding module specification for 'virtualenvwrapper.hook_loader' (ModuleNotFoundError: No module named 'virtualenvwrapper')
virtualenvwrapper.sh: There was a problem running the initialization hooks.
If Python could not import the module virtualenvwrapper.hook_loader,
check that virtualenvwrapper has been installed for
VIRTUALENVWRAPPER_PYTHON=/usr/local/bin/python3 and that PATH is
set properly.
After some trial and error, it turns out that a reinstallation of the virtualenvwrapper package resolved it. This was a manifest of the fact that I recently upgrade python3 (from version 3.5.2 to 3.6.1) via homebrew and in turn it broke virtualenvwrapper's shell script that hardcoded references to the older version of python3 (in my case it was 3.5.2). In short, this below line should fix it (at least in my case it did).
pip3 install virtualenvwrapper
In macOS Sierra,
If you installed virtualenv package using pip3,
add following to .bash_profile
export VIRTUALENVWRAPPER_PYTHON=/usr/local/bin/python3
or
export VIRTUALENVWRAPPER_PYTHON=$(which python3)
after that every new virtualenvironment you created using workon myvenv
uses python3 as python interpreter
I had already installed virtualenv so just needed to run
pip3 install virtualenvwrapper
In my case, there was somehow a mismatch between pip3 and python3 (because I have multiple pythons installed). This worked for me:
sudo python -m pip install virtualenvwrapper
export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python
good choice, but you already knew /usr/bin/python is 2.7
If you want to use version 3
sudo apt-get install python3-pip
and logout & login
check your python3 installation directory:
which python3
If installed by brew you should get:
/usr/local/python3
export python version to be used virtualenvwrapper:
export VIRTUALENVWRAPPER_PYTHON=/usr/local/python3
source your shell configuration file:
bash
source .bashrc
zsh
source .zshrc
This error appeared for me after running brew update and brew upgrade on mac os high sierra.
The issue was resolved by reinstalling virtualenvwrappper i.e. pip install virtualenvwrapper.
Here is what for me solved
Error while finding module specification for 'virtualenvwrapper.hook_loader' (ModuleNotFoundError: No module named 'virtualenvwrapper')
obtained in creating and activating environments.
In my .bashrc I had wrote:
export VIRTUALENVWRAPPER_PYTHON=python3
Changing it to:
export VIRTUALENVWRAPPER_PYTHON=$(which python3)
solved the problem for me.
NOTE: If you activate an environment and run which python3, you will find out that the python run is the one of the environment, not the global one specified in VIRTUALENVWRAPPER_PYTHON
I've got this error for entirely different reasons, but since I ended up on this thread, I gather somebody might find this useful. So in my case, it turned out that my script was attempting to activate the virtual environment while I was already on it (the workon statement is issued from my .profile file). And so the solution was to simply deactivate the environment first and then run the script.
If you're here after a macOS update, and installed python3 via brew, try re-linking your version of python3
brew link python#3.8
brew link python#3.9
etc
On my computer, I ran sudo apt install python-pip and then pip install virtualenvwrapper but on my settings, I set it as follows:
export WORKON_HOME=$HOME/.Envs
export VIRTUALENVWRAPPER_PYTHON=$(which -a python3)
source $HOME/.local/bin/virtualenvwrapper.sh
That got rid of the error and note that I am using python3 and I use pip3 as my default
I had this warning after a brew upgrade, which updated python3 packages and brew-installed python3.
I found a pip3 and python3 version mismatch as Evan Thomas hinted:
❯ which python3 && python3 --version
/usr/bin/python3
Python 3.8.2
❯ which pip3 && pip3 --version
/usr/local/bin/pip3
pip 21.0.1 from /usr/local/lib/python3.9/site-packages/pip (python 3.9)
Running brew link python#3.9 as snakesNbronies suggested removes the warning.
I had a similar issue on Mac OS Catalina 10.15.6 after I installed vim and macvim
using following:
brew install vim && brew install macvim
which I believe was the cause for the problem.
While workon worked but always gave an error message as below:
~ $ workon loom
/Library/Frameworks/Python.framework/Versions/3.6/bin/python3: Error while finding module specification for 'virtualenvwrapper.hook_loader' (ModuleNotFoundError: No module named 'virtualenvwrapper')
/Library/Frameworks/Python.framework/Versions/3.6/bin/python3: Error while finding module specification for 'virtualenvwrapper.hook_loader' (ModuleNotFoundError: No module named 'virtualenvwrapper')
(loom) ~ $
What worked for me finally was:
adding export VIRTUALENVWRAPPER_PYTHON=/usr/local/bin/python3 to .bash_profile
sudo pip3 install virtualenvwrapper
I found that problem can be reproduced with or without pyenv.
Here checklist how to solve this problem which helps me on Mac with fresh install brew and pyenv:
pyenv install 3.11.1 (or whatever version you need) and global it with command pyenv global 3.11.1
type python and make sure, that it's the same version that you set-up in previous step.
install virtualenvwrapper with python -m pip install virtualenv virtualenvwrapper
FIRST IMPORTANT! SET THE FIRST VARIABLE CORRECT! which python tells you python path to your installed path. Use this path to selected by system python executable to tell virtualenvwrapper which python it need to use with variable VIRTUALENVWRAPPER_PYTHON.
SECOND IMPORTANT! Make sure you execute correct virtualenvwrapper.sh. I use pyenv so I just use virtualenvwrapper in my global version (look for config below)
command -v pyenv >/dev/null || export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init -)"
export PYENV_VERSION="$(pyenv version-name)"
VIRTUALENVWRAPPER_PYTHON="$HOME/.pyenv/shims/python"
export WORKON_HOME=$HOME/.virtualenvs
export PROJECT_HOME=$HOME/Devel
source ~/.pyenv/versions/$PYENV_VERSION/bin/virtualenvwrapper.sh
# Tell pyenv-virtualenvwrapper to use pyenv when creating new Python environments
export PYENV_VIRTUALENVWRAPPER_PREFER_PYVENV="true"
# Set the pyenv shims to initialize
if command -v pyenv 1>/dev/null 2>&1; then
eval "$(pyenv init -)"
fi
try to install python-is-python3:
1.Update the package index:
sudo apt-get update
2.Install python-is-python3 deb package:
sudo apt-get install python-is-python3
If anyone's still having this issue, I followed the top answer on this thread to fix a pip3 mismatch with my version of python, then ran sudo pip3 install virtualenv virtualenvwrapper followed by source ~/.bashrc which resolved the issue for me.

Python on my Mac is a mess, help me uninstall what I don't need

I'm unable to import numpy in Python 2.7 in the shell. I installed Python with brew install, then numpy with sudo pip install. I tried without sudo but I get a permission denied error. Anyway, I get this:
ImportError: dlopen(/usr/local/Cellar/python/2.7.10_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload/_io.so, 2): Symbol not found: __PyErr_ReplaceException
Referenced from: /usr/local/Cellar/python/2.7.10_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload/_io.so
Expected in: flat namespace
in /usr/local/Cellar/python/2.7.10_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload/_io.so
In /usr/local/bin/ I have the following:
pip
pip2
pip2.7
pip3
pip3.4
And:
python
python-config
python2
python2-config
python2.7
python2.7-config
python3
python3-config
python3.4
python3.4-config
python3.4m
python3.4m-config
pythonw
pythonw2
pythonw2.7
The output of which python is /usr/local/bin/python
As for workaround, try downgrading your Python to 2.7.9 like:
brew switch python 2.7.9
by overriding the existing one, as it seems there is some particular problem with 2.7.10. Then re-link it again (if required).
Also make sure your PYTHONPATH is correct (you don't override it anywhere in your startup files).
That output is normal. Have you tried running brew install openssl followed by brew link --force openssl and then brew install python. The following thread helps describe the issue https://github.com/Homebrew/homebrew/issues/40516

How to link home brew python version and set it as default

I just switched from MacPorts to HomeBrew. After installing all the required XCode versions and other software I tried installing python using homebrew: I think it successfully installed, but when I do which python it still shows me 2.7.3 which I think is the version shipped with Mountain Lion.
which python
/usr/local/bin/python
python --version
Python 2.7.3
so I tried to install again
brew install python --framework --universal
Warning: python-2.7.5 already installed, it's just not linked
But it says python 2.7.5 already install and not linked, I tried to do brew link python
That led me to following message so, I have no idea what I should be doing:
Linking /usr/local/Cellar/python/2.7.5... Warning: Could not link python. Unlinking...
Error: Could not symlink file: /usr/local/Cellar/python/2.7.5/bin/smtpd2.py
Target /usr/local/bin/smtpd2.py already exists. You may need to delete it.
To force the link and overwrite all other conflicting files, do:
brew link --overwrite formula_name
To list all files that would be deleted:
brew link --overwrite --dry-run formula_name
After installing python3 with brew install python3
I was getting the error:
Error: An unexpected error occurred during the `brew link` step
The formula built, but is not symlinked into /usr/local
Permission denied # dir_s_mkdir - /usr/local/Frameworks
Error: Permission denied # dir_s_mkdir - /usr/local/Frameworks
After typing brew link python3 the error was:
Linking /usr/local/Cellar/python/3.6.4_3... Error: Permission denied # dir_s_mkdir - /usr/local/Frameworks
To solve the problem:
sudo mkdir -p /usr/local/Frameworks
sudo chown -R $(whoami) /usr/local/*
brew link python3
After this, I could open python3 by typing python3 👍
(From https://github.com/Homebrew/homebrew-core/issues/20985)
In the Terminal, type:
brew link python
If you used
brew install python
before 'unlink'
you got
brew info python
/usr/local/Cellar/python/2.7.11
python -V
Python 2.7.10
so do
brew unlink python && brew link python
and open a new terminal shell
python -V
Python 2.7.11
For those looking for a version re-link using brew, I've found this command useful:
brew unlink python#3.9 && brew link python#3.10
It is possible that some errors appear, like:
Error: Could not symlink bin/pip3
Target /usr/local/bin/pip3
already exists. You may want to remove it:
rm '/usr/local/bin/pip3'
To force the link and overwrite all conflicting files:
brew link --overwrite python#3.10
To list all files that would be deleted:
brew link --overwrite --dry-run python#3.10
So following brew advice and running:
rm '/usr/local/bin/pip3'
brew link --overwrite python#3.10
Just worked for me. To check if symlinks are ok, run: ls -l /usr/local/bin/python*, you should see something like:
/usr/local/bin/python3 -> ../Cellar/python#3.10/3.10.2/bin/python3
/usr/local/bin/python3-config -> ../Cellar/python#3.10/3.10.2/bin/python3-config
/usr/local/bin/python3.10 -> ../Cellar/python#3.10/3.10.2/bin/python3.10
/usr/local/bin/python3.10-config -> ../Cellar/python#3.10/3.10.2/bin/python3.10-config
I think you have to be precise with which version you want to link with the command brew link python like:
brew link python 3
It will give you an error like that:
Linking /usr/local/Cellar/python3/3.5.2...
Error: Could not symlink bin/2to3-3.5
Target /usr/local/bin/2to3-3.5
already exists.
You may want to remove it:
rm '/usr/local/bin/2to3-3.5'
To force the link and overwrite all conflicting files:
brew link --overwrite python3
To list all files that would be deleted:
brew link --overwrite --dry-run python3
but you have to copy/paste the command to force the link which is:
brew link --overwrite python3
I think that you must have the version (the newer) installed.
On OS X High Sierra, I had to do this:
sudo install -d -o $(whoami) -g admin /usr/local/Frameworks
brew uninstall --ignore-dependencies python
brew install python
python --version # should work, returns 2.7, which is a Python thing (it's weird, but ok)
credit to https://gist.github.com/irazasyed/7732946#gistcomment-2235469
I think it's better than recursively chowning the /usr/local dir, but that may solve other problems ;)
You can follow these steps.
$ python3 --version
$ brew unlink python#2
$ brew link python3
$ python3 --version
This answer is for upgrading Python 2.7.10 to Python 2.7.11 on Mac OS X El Capitan . On Terminal type:
brew unlink python
After that type on Terminal
brew install python
brew switch to python3 by default, so if you want to still set python2 as default bin python, running:
brew unlink python && brew link python2 --force
I wouldn't recommend playing around with the symlinks, it's not just to where python points, its the entire env around it as well that maybe mixing version
I would do this:
$ brew install pyenv
$ pyenv install 3.7.3
$ pyenv global 3.7.3
The problem with me is that I have so many different versions of python, so it opens up a different python3.7 even after I did brew link. I did the following additional steps to make it default after linking
First, open up the document setting up the path of python
nano ~/.bash_profile
Then something like this shows up:
# Setting PATH for Python 3.7
# The original version is saved in .bash_profile.pysave
PATH="/Library/Frameworks/Python.framework/Versions/3.7/bin:${PATH}"
export PATH
# Setting PATH for Python 3.6
# The original version is saved in .bash_profile.pysave
PATH="/Library/Frameworks/Python.framework/Versions/3.6/bin:${PATH}"
export PATH
The thing here is that my Python for brew framework is not in the Library Folder!! So I changed the framework for python 3.7, which looks like follows in my system
# Setting PATH for Python 3.7
# The original version is saved in .bash_profile.pysave
PATH="/usr/local/Frameworks/Python.framework/Versions/3.7/bin:${PATH}"
export PATH
Change and save the file. Restart the computer, and typing in python3.7, I get the python I installed for brew.
Not sure if my case is applicable to everyone, but worth a try. Not sure if the framework path is the same for everyone, please made sure before trying out.
I use these commands to solve it.
mkdir /usr/local/lib
mkdir /usr/local/lib/pkgconfig
brew link python

Categories