Installing Python
Table of Content
Installing Python
In this guide, I will be installing python for MacOS. Latest Python version at time of writing is 3.11.2 and is found on Python official website, under downloads section: Python.org
Once installed, we have to make sure the right python version is called in. In my case, I have both Python2 installed and an older Python3 version. I run the following command to change the default mapping to Python3:
sam@sam-iMac /Users % echo "alias python3=/usr/local/bin/python3.11.2" >> ~/.bashrc |
---|
Close your terminal, then check which Python version is installed:
sam@sam-iMac ~ % python3 --version Python 3.11.2 |
---|
Next step is to start python3 by simply typing Python3, then quit by typing quit()
sam@sam-iMac /Users % python3 Python 3.11.2 (v3.11.2:878ead1ac1, Feb 7 2023, 10:02:41) [Clang 13.0.0 (clang-1300.0.29.30)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> quit() |
---|
Using an editor
Since you will be writing long codes, it is important to use an editor which comes with features aiming at making management of code easier. The one I use is Visual Studio Code, as it comes with:
- Syntax Highlighter.
- Integration with GitHub for source control.
- Other features I have yet to find out about.
Once Visual Studio is installed, click on extensions on lefty pane and search for Python. and install Python extension for Visual Studio. This will help you with syntax highlighting and indentation, amongst other things.
Virtual Environment
Virtual environments create a space where you can install packages independently and run your scripts. Once the work is done, the environment can be deactivated.
To create a virtual environment on MacOS, I follow these steps:
Install virtualenv |
---|
Create a working directory
mkdir TEST_SCRIPTS |
---|
Change to new directory
cd TEST_SCRIPTS/ |
---|
Setup your virtual environment
virtualenv sam_lab |
---|
Activate your environment
virtualenv sam_lab --system-site-packages source sam_lab/bin/activate |
---|
The terminal prompt should change to show your environment.
To deactivate, simply type “deactivate”
Hello World
Now that Python is installed and my editor is ready, it is almost a must to start with print the famous “Hello World!”.
Let’s create our 1st python script. Under file, click on new text file, then select language “Python” and save. The file will be saved as *.py, so ready to execute as a python script.
As we have in the previous steps created a virtual environment, and pop windows appears asking to select the python interpreter of choice.
I can run the script either from the terminal, by typing “python3 test1.py” or press the play icon on top right.
Python crash course video
To learn more about Python for Engineers, watch this 1hr crash course video.
This course covers foundation topics: