How to Setup Mac for Python Development
CHANGELOG:
- updated on 11/9/2021: add more pyenv setup
- updated on 1/8/2021: add pyenv and VSCode
- updated on 9/272019
- Originally written on August 30, 2016
This is how I set up Mac for Python development and data analysis.
Install Homebrew
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
Setup Python
It’s much easier to manage different versions of python using pyenv
(https://github.com/pyenv/pyenv), which you can install using:
brew update
brew install pyenv
Check the different python versions you can install:
pyenv install --list
Install a specific python version (choose from the list above):
pyenv install 3.8.1
Set the global python version:
$ pyenv global 3.8.1
Then, check the python versions installed:
$ pyenv versionssystem3.7.9* 3.8.1 (set by /Users/harrywang/.pyenv/version)3.9.0
You may also need to do the following to make sure the pyenv
python will be executed instead of the Mac default one:
eval "$(pyenv init --path)"
To make the above command work after reboot, run
vim ~/.zshrc
and add eval "$(pyenv init --path)"
to the file.
if you want to use a specific python version within a specific folder, go to that folder and set a local python version:
pyenv local 3.7.9
Setup Github
https://help.github.com/articles/set-up-git/
Once completed, try the following two commands to verify:
$ git config --global user.nameHarry Wang$ git config --global user.emailharryjwang@gmail.com
Caching your GitHub password in Git: https://help.github.com/articles/caching-your-github-password-in-git/
Setup Virtual Environment
See why you need virtual environments: https://realpython.com/python-virtual-environments-a-primer/
Python 2:
Install virtualenv (https://virtualenv.pypa.io/en/stable/)
$ sudo pip install virtualenv
To create a virtual environment:
- go to the folder of the project
- generate a venv folder for the virtual environment: $ virtualenv venv
- activate the environment:$ source venv/bin/activate
- install packages only in the newly created environment (a requirement.txt file is often used to include the packages): $ pip install -r requirements.txt
Python 3:
To create a virtual environment:
- go to the folder of the project
- generate a venv
folder for the virtual environment: $ python3 -m venv venv
- activate the environment:$ source venv/bin/activate
- install packages only in the newly created environment (a requirement.txt file is often used to include the packages): $ pip install -r requirements.txt
You can check out my other repo for an example: https://github.com/harrywang/house-price-prediction
Setup Jupyter
To set up a system-wide juypter using pip or pip3:
Python 2:
pip install jupyter
Run Jupyter: jupyter notebook
Python 3:
pip3 install jupyter
Run Jupyter: jupyter notebook
If you want to use add a virtual environment to the system-wide Jupyter, you can follow the tutorial https://janakiev.com/blog/jupyter-virtual-envs/
I often install Jupyter in the virtual environment by including it in the requirement.txt file aforementioned.
Setup VSCode
https://code.visualstudio.com/
Just download the installation package and install
Setup MySQL locally
Install MySQL: brew install mysql
, which installs MySQL without a password.
To start MySQL: mysql.server start
and then connect: mysql -uroot
We've installed your MySQL database without a root password. To secure it run:
mysql_secure_installation
MySQL is configured to only allow connections from localhost by default
To connect run:
mysql -uroot
To have launchd start mysql now and restart at login:
brew services start mysql
Or, if you don't want/need a background service you can just run:
mysql.server start
If MySQL does not work, try the following to completely reset the database — Warning: you will lose all your data.
brew remove mysql
brew cleanup
launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist
rm ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist
sudo rm -rf /usr/local/var/mysql
brew install mysql
mysql.server start # no sudo!