Python 3 on MacOS
I recently had the need to create a small python script to run a process. I quickly looked up a process I could utilize to read a Rest API then do some work based on that content.
I fired up Visual Studio code and wrote out the framework of the program I needed in about 10 min. (NOTE: I have not used Python in a long time and it was fun to quickly put something together).
Once I was done writing the code I jumped into a term terminal and ran the program “python GetAPIData.py”. It worked well – opened a file of records added to an array and displayed the line items and did a count.
I then added some code that referenced how to call an API and it used the “requests”, knowing I needed to run “pip install requests” before I ran the program, I was rewarded with an error.
zsh: command not found: pip
After this error, I checked what python version was on my Mac (python –version) I then learned I was running python 2.7.
This was confusing as I had just run “brew install python” before starting the project. Apparently, Mac comes with python 2.7 and when you install brew it does not update the path. I was able to run the app via command python3. And install “requests” via pip3 requests. But this was not the best solution for me. I really just want to run python 3 all the time.
The solution I found was to alias Python to python3 in my profile.
I updated my .zshrc file with the following nano ~/.zshrc and added this to the last line “alias python=python”
After this, I had one more error. I was using the print command incorrectly, python3 has the print function using () vs “”.
Here is what has changed:
print 'Hello, World!'
print('Hello, World!')
Lesson learned – Take a look at what version of the app you are using. I spent more time figuring out how to update python on my Mac than I did writing my code.