Skip to content

Instantly share code, notes, and snippets.

@Nezteb
Last active July 23, 2020 04:48
Show Gist options
  • Save Nezteb/524da0dba1be4151b3b2d85d980df34c to your computer and use it in GitHub Desktop.
Save Nezteb/524da0dba1be4151b3b2d85d980df34c to your computer and use it in GitHub Desktop.
Some tips on installing Python on Mac... (this is very messy for now)
# First verify you have Homebrew: https://brew.sh/
# Put this line in your ~/.bash_profile, this will make sure your Homebrew binaries are used first
export PATH="/usr/local/bin":"/usr/local/sbin":"$PATH"
########## ON A FRESH INSTALL ##########
# Verify the output here does not contain anything related to Python
echo $PATH
# If it does contain Python stuff, figure out where that is coming from (probably ~/.bash_profile) and remove it
brew update
brew install python # this installs Python 3 as 'python3'
brew install python@2 # this installs Python 2 as 'python'
# TODO: What about --framework?
# Neccesary environment variables, put these in ~/.bash_profile
export PATH="/usr/local/opt/python@2/libexec/bin":"$PATH"
export PATH="/usr/local/share/python@2":"$PATH"
which python # Should give something like /usr/local/bin/python, NOT /usr/bin/python
python --version # Should give some form of Python 2
# Brew takes care of installing pip and setuptools/easy_install for you, upgrade them
pip install --upgrade pip setuptools
# Useful helper packages for python
pip install virtualenv
pip install virtualenvwrapper
# Verify the above packages were installed in /usr/local/lib/python2.7/site-packages
ls /usr/local/lib/python2.7/site-packages
# virtualenvwrapper needs these environment variables, put these in ~/.bash_profile
export VIRTUALENVWRAPPER_PYTHON=/usr/local/bin/python
export WORKON_HOME=$HOME/.virtualenvs
# And this source file, also put this line in ~/.bash_profile
source /usr/local/bin/virtualenvwrapper.sh
########## IF YOU'RE TRYING TO FIX A BROKEN INSTALL ##########
echo $PATH # See if there is Python in here somewhere, remove it (possibly set from ~/.bash_profile)
# CLOSE YOUR TERMINAL WINDOW (this will clear your previous PATH settings)
# OPEN NEW TERMINAL WINDOW
echo $PATH # Verify Python is gone
brew update
# Reinstall python
brew reinstall python # this installs python 3 as 'python3'
brew reinstall python@2 # this installs python 2 as 'python'
brew link python@2
# Reinstall pip
python3 -m pip install -U --force-reinstall pip
python -m pip install -U --force-reinstall pip
# If you run into issues where packages are already installed, try this installing them with:
pip install something --ignore-installed
# Or remove them all
ls /usr/local/lib/python2.7/site-packages
pip freeze | xargs pip uninstall -y
ls /usr/local/lib/python2.7/site-packages # Most of the packages should be gone now
# Now you can go up to line 16 and continue to line 36 of this guide
########## SOME OTHER USEFUL COMMANDS ##########
# Show all installed packages and where they are installed
pip list | awk 'FNR > 2 {print $1}' | xargs pip show
# Print global site packages
python -m site
# Print user site packages
python -m site --user-site
# Complex command to do both
python -c $'import site
print "GLOBAL SITE PACKAGES:"
sitepackages = site.getsitepackages()
if isinstance(sitepackages, list):
for i in sitepackages:
print i
else:
print sitepackages
print "USER SITE PACKAGES:"
usersitepackages = site.getusersitepackages()
if isinstance(usersitepackages, list):
for i in usersitepackages:
print i
else:
print usersitepackages
'
# Uninstall all user site packages
USER_PACKAGES=$(pip list --user | awk 'FNR > 2 {print $1}')
echo $USER_PACKAGES | xargs pip uninstall -y
echo $USER_PACKAGES | xargs pip install --ignore-installed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment