Python Dev Setup on Mac | Gotchas Documented
Tool Installation #
Here are some personal notes on getting Python setup for my new Mac.
Python Base Install #
- Install Homebrew
- Install Python 3 via homebrew:
brew install python
At this point, you can use python3
and pip3
commands, but for pipenv
you'll need
to symlink pip
and python
.
To do this, note the caveat
message produced by brew install python
:
Python has been installed as
/opt/homebrew/bin/python3
Unversioned symlinks `python`, `python-config`, `pip` etc. pointing to
`python3`, `python3-config`, `pip3` etc., respectively, have been installed into
/opt/homebrew/opt/python@3.11/libexec/bin
We need to add the following to the end our ~/.zshrc
file (Per this
SO Post):
# point python to python3
path+=('/opt/homebrew/opt/python@3.11/libexec/bin')
export PATH
Pipenv #
Get pipenv
setup to manage dependencies and virtual environments.
Reference Hitchiker's Guide to Python
NOTE: The link above contains a previous section on Python base install. These instructions didn't
address symlinking python
to python3
. Instead of using these, follow notes in Python Base Install
section above.
VS Code #
- Linter: Pylint Extension
- Code formatter: Black Formatter Extension
Project Setup #
Python Environment With Pipenv #
-
Cloning an existing project with a
Pipfile
, runpipenv install --dev
. This creates a new virtualenv and installs dependencies. -
Create a new project, just install the first dependency with
pipenv install <package foo>
from the project's root dir. That will generate the new virtual env. -
NOTE: For better cross-compatibility with other systems, update the
Pipfile
to reference Python3
as opposed to a specific version e.g3.11
. See this example CL
Visual Studio Code Launch Configuration #
The following is an example script launch configuration. Note the relative path
to the script file in program
and the command line args in args
.
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "mp3-convert.py with test args",
"type": "python",
"request": "launch",
"program": "${workspaceFolder}/mp3-convert/mp3-convert.py",
"console": "integratedTerminal",
"justMyCode": true,
"args": [
"${workspaceFolder}/test_data/source",
"${workspaceFolder}/test_data/dest"
]
}
]
}