Skip to content

1st/python-on-macos

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

91 Commits
 
 
 
 
 
 

Repository files navigation

How to setup python environment on macOS

On this page I describe how to setup python environment on macOS Catalina (10.13).

Known problems

When I upgrade to a next major version of macOS it's almost always some problems appear - some tools stop working, eapecially if you use your system for software development.

How to setup macOS


Known problems

Virtual env stopped work when I upgraded python via homebrew

How to fix a broken virtual env after python version upgrade?

I've created an alias that is easy to use every time when you upgrade the python version via homebrew, and your virtusl env becomes not working. Use it like this fix_virtualenv <env_name> and it will auto-fix your python version by replacing a broken links to an actual version of python.

Find the snippet in my gist. Find it by the name fix_virtualenv

Bad file permissions

Repair disk permissions with Disk Utility. It happens that permissions on some files and directories broken after upgrade to newer version of macOS.

Then run next command to make this directory writable:

sudo chown -R $(whoami) $(brew --prefix)/*

Previously it was possible to do like this sudo chown -R $(whoami):admin /usr/local but no anymore.

Homebrew doesn't work

Fix issue with these commands:

xcode-select --install
cd /usr/local/Library
git pull origin master

You can try to find some problems by running:

brew doctor

Python doesn't work

brew reinstall python
brew reinstall python@2

See also list of known bugs in HomeBrew.

Ruby gems can't be installed

To install ruby gems, use this command:

sudo gem install -n /usr/local/bin [package]

where [package] is what you need to install (compass, bundler, etc).

How to clean unused homebrew dependencies

Command brew bundle dump generates a Brewfile with all the packages installed by user. Dependent packages are not listed here. It allows to use this file for the next time to install all listed software wiith one command brew bundle --force cleanup.

brew bundle dump
brew bundle --force cleanup

How to setup macOS

HomeBrew

Before you start, open Terminal application and install Xcode command-line tool. It's required to install a lot of software on your Mac.

xcode-select --install

Install HomeBrew to have ability to install up-to-date software, like apt-get install in Ubuntu.

My list of brew software (use brew install [package_name]):

  • required: memcached, git, mysql, postgresql, node, wget
  • optional: mercurial, mongodb, rabbitmq, zookeeper --with-python, boost --with-python, jpeg, libpng

System changes

Edit nano ~/.profile file and insert this content.

Press Cmd + O to save file, Cmd + X to exit from nano. Run in terminal source ~/.profile to load changes.

If you use mercurial

Edit ~/.hgrc and insert info about my user:

[ui]
username = User Name <user@gmail.com>

Python setup

  • brew install python installs python and pip
  • pip install virtualenv virtualenvwrapper

Python virtualenv

If your virtual environments are broken, then you need to recreate links to the newer version of Python.

Do these two commands for each of your project:

# delete all broken links
find ~/.virtualenvs/my_project_name/ -type l -delete
# create new links to python
virtualenv ~/.virtualenvs/my_project_name/

Python 3 support

  • brew install python3 installs python3 and pip3
  • pip3 install virtualenv virtualenvwrapper

To create virtual environment with python3 support you need to specify path to specific version of python.

mkvirtualenv --python=$(which python3) project_name
# you can also use my shortcut from ~/.profile (see link to file above)
mkvirtualenv3 project_name

Django completion

Add autocompletion in terminal when we type manage.py or django-admin.py and press <tab> button two times.

  • cd /usr/local/etc/bash_completion.d/
  • wget https://raw.github.com/django/django/master/extras/django_bash_completion
  • source ~/.profile to affect changes

Post-installation

  • create virtual environments for projects mkvirtualenv [env_name] and run pip install -r requirements.txt
  • restore MySQL/Postgres/MongoDB databases
    • mongodb: mongodump --out backup/ -> mongorestore backup/

Useful software

This is my list of sofrware that I use:

Source code editor

I use Visual Studio Code and you can read how to configure VS Code on macOS for Python dev.

In the past I've used Sublime Text 3 and tried Atom.

Development tools

Other software

Login to web services on macOS

Read more

About

How to setup python environment on macOS

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published