At my work I recently started working on a project with a lot of Python and C++ in it. I have never worked on Python before, so I started learning it. I liked it a lot. It allows you write code a bit like Scala (which is another language I like).
So I thought recording my learning here on blog. Just to read them again if needed.
In this post I am writing about how I set up Python dev env on my personal Mac Mini.
Mac OS has oldPython pre installed.
I bought my mac mini some 18 months back. At that time I used to work on Scala (for work and personal projects). Afterwards at my new job, I moved away from Scala and started working on C++ Python etc.
So when I decided to get a python dev env on my Mac mini, I checked it. I found out that Mac OS comes with Python pre installed, but it is an old version. You wont use it for development.
➜ ~ which python
/usr/bin/python
➜ ~ python --version
Python 2.7.18
I didnt want to mess with this installed version.I never installed it and if it is preinstalled, I thought it will be for some reason. Later I figured out that this version of python is needed by the operating system as a dependency for some pre installed software packages. So I left it as it is.
Brew or Pip
When I decided to install a 3.x version of python, I realized that it can be installed using brew or Pip. I guess both should work, but I chose brew. Primarily because I am familiar with it and it is specific/exclusive to Mac OS.
So I did
brew install python3
which failed with, following error
cannot be installed as binary package and must be built from source.
Install the Command Line Tools:xcode-select — install
This was self explaining, as Brew formulae needs a compiler or some command line tool, xcode-select bring most of it. I think sometime you may need full XCode, but I havent come across that situation yet.
Post xcode select installation, I was able to do brew python2 install successfully.
Two versions
Now I have two versions of python on my box.
➜ ~ python --version
Python 2.7.18
➜ ~ python3 --version
Python 3.8.9
➜ ~ which python3
/usr/bin/python3
I always wanted to use python3, so I defined an alias in my .zshrc
alias python=/usr/bin/python3
I restarted my session, and I do have python3 as my default
➜ ~ python --version
Python 3.8.9
Now I got python interpreter working
➜ ~ python
Python 3.8.9 (default, Jul 19 2021, 09:37:32)
[Clang 13.0.0 (clang-1300.0.27.3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> print("hello")
hello
Python IDE
I didnt researched much on this. I has VSCode installed on my box, so I installed python extention.
This extension doesnt work out of the box, it requires two steps.
- It requires you to install python. (we already completed that step we completed)
- It requires you to manually select python interpreter. You need to make sure you choose the right version (i.e. 3.x)
With this you are ready to go.
Launch IDE & Run some code
Add following to you .zshrc
export PATH=$PATH:/Applications/Visual\ Studio\ Code.app/Contents/Resources/app/bin
Now you can launch IDE like this using code
command
➜ ~ cd PythonTestCode
➜ PythonTestCode code .
➜ PythonTestCode code .
Now add some code and try to run it
You should see something like this in terminal window in IDE (usually at bootom)
➜ PythonTestCode /usr/bin/python3 /Users/vikrantsingh/PythonTestCode/first_file.py
Hello World!
➜ PythonTestCode
You can also debug it
Which will give you usual debug control on IDE
This setup up worked well for me till now. I am sure I will face some challenge if I go advance, but I do not think thats happening on this setup very soon.
Thanks for reading.