Installing Python
Before we start, it is important to install the necessary tools. This way, we can run the code to test see the result. This will also help us understand the behavior of Python. Follow the instructions below to set up your programming environment.
There are also some additional libraries we will be using.
- PIL: Used to render images.
- PILLOW: Modern replacement for PIL.
Windows
Important
You will need to be connected to the internet for ALL the installation steps as the required packages will need to be downloaded.
Step 1: Download Python
Head over to Python website. You should see something similar to the following. Choose Python 3.12.X where X is the latest patch version.
Then choose Windows installer (x-bit) where x depends on your computer architecture. Usually this will be 64-bit but you need to check your computer.
This will initialize the download of the installer. Choose the directory to store the installer. Alternatively, choose the default directory which is the Downloads directory.
Step 2: Run Installer
Open the directory containing the installer. If you choose the default directory, it should be in the Downloads directory. Double-click on the installer to execute the installer.
Important
Ensure that you check the box that says "Add python.exe to PATH" as seen in the red box above. This is important to ensure that Python is installed in your execution path.
Click on "Install Now" to install Python. This will complete in a few minutes. You should see a new program called IDLE in your programs menu once the installation is completed.
Step 3: Installing Packages
Open the Command Prompt from your Start Menu. You can click on Start Menu and start typing "command prompt".
Enter the following command and press enter.
Note
You may need to add python -m
or py -m
in front of the command above if it fails to run.
This will install the packages stated which is pillow
.
It will also install all other packages that are not stated but required by pillow
.
If successful, you should see a line saying "Successfully installed..." as seen below.
Note
You may ignore the yellow text (if any). However, if you see a bunch of red text, the packages may not be installed properly.
Troubleshooting
Important
Use the following troubleshooting guide if you encounter problem during installation.
Checking if Python is Installed
Your Python path should be in the following form.
Here, <USERNAME>
is your username on your computer.
This is how you can navigate to the directory.
-
Method 1: Windows Explorer
- Go to Windows Explorer.
- Click on the address bar.
- Type the following.
-
Method 2: Run
- Press Win+R. This will open up Run.
- Type the following.
-
Click "OK".
pip Not Recognized
You may see the following error when running pip
.
Note that the color may not be red.
You may try running one of the following alternative command instead.
You can find more information here.
Checking if pip is Installed
To check if pip
is installed, run one the following commands on Command Prompt.
Multiple Version
If you have multiple versions of Python, try running one of the following commands instead.
You should see an output similar to the one below.
Adding Python to PATH Manually
Open settings and find "edit system" by using the search bar.
Open "Edit the system environment variables" as seen above. You should see the following System Properties window.
Click o "Environment Variables..." as circled in red above. You should see the window below.
Select Path from the top list User variables for user. Then click Edit.... Add Python path by clicking New then key in the following two paths.
To check if Python is already added to PATH
variable, open Command Prompt and type the following.
This will gives you a list of directories added to the PATH
variable.
Ensure that the copied path is already there.
Checking if the Packages are Installed
Run the following command.
As usual, you may need to add python -m
(or other, similar as above).
The command checks all the Python packages insatlled. The result should be a list with the following format.
For example, you should see scipy==1.7.3
(or higher version),
Check that the required packages are installed correctly.
Alternatively, you can use pip list
instead of pip freeze
.
Upgrading pip
During installation of packages, you may receive a warning in yellow that pip
must be upgraded.
This is not necessary as long as you can work with the Python packages normally.
However, should you need it, run the following command on Command Prompt.
As usual, you may need to use python -m
.
Subprocess Error When Installing Packages
As of now, this problem arises due to the incorrect Python installer chosen (e.g., using 32-bit installer on 64-bit machine or vice versa). As a result, you may see the following message.
The solution is to simply switch the installer from 32-bit to 64-bit or vice versa. Uninstall the previous one to avoid any conflicts with the new installer. See the guide on Uninstallation to uninstall.
Issues with wheel During Installation
This might occur to some of you as the wheel
package is not installed by default.
Some issues that fall into this category include getting one of these messages when trying to install the packages.
- "
Could not build wheels for <package name> since package wheel is not installed
"- This is an error and will stop the installation process before it finishes properly.
- "
Using legacy 'setup.py install' for <package name>, since package 'wheel' is not installed
"- This is NOT an error but will very likely slow down the installation process.
The solution is to simply modify the installation command with the wheel
package installed first.
Run the following command.
Uninstalling Python
Note
This process is optional. However, one might easily confuse between different instances of Python that co-exist inside the computer. An example issue would be unable to import the packages on IDLE although the installation process ran smoothly, only to find out that the IDLE version does not match.
Open your settings and type "uninstall" on the search bar. Click on "Add or remove programs".
Find the Python program that is to be uninstalled. The programs are arranged alphabetically. Click on "..." and click "Uninstall". For instance, the image below shows how to uninstall Python 3.11 since there are both Python 3.11 and Python 3.12.
Reinstalling Python
Reinstalling Python may be needed if you have two or more instances of Python (e.g., Anaconda or Microsoft Store) and many errors appear during package installation due to certain clashes, which forces you to uninstall them. Here is what to do. Suppose you have to reinstall Python 3.12.1 again.
-
Reopen the installation file from Python website. This should be displayed the moment you open it. Note that you may also uninstall Python as seen in the last option below.
-
Click "Modify" and check everything. The last option is unchecked by default, so ensure that this is also checked.
-
Click "Next" and checked the the first three checkboxes that can be checked. Do not check the first checkbox. It is optional to check the last three options.
-
Click "Install".
Macintosh
Step 1: Download Python
Head over to Python website. You should see something similar to the following. Choose Python 3.12.X where X is the latest patch version.
Then choose macOS 64-bit universal2 installer.
This will initialize the download of the installer. Choose the directory to store the installer. Alternatively, choose the default directory which is the Downloads directory.
Step 2: Run Installer
Open the directory containing the installer. If you choose the default directory, it should be in the Downloads directory. Run the installer.
Click "Continue" to install.
Step 3: Installing Packages
Open the Terminal app. You can find the Terminal app by clicking on Finder on the dock and choosing: Utilities > Terminal.
Enter the following command and press enter.
Note
You may need to add python -m
or py -m
in front of the command above if it fails to run.
This will install the packages stated which is pillow
.
It will also install all other packages that are not stated but required by pillow
.
Note
You may ignore the yellow text (if any). However, if you see a bunch of red text, the packages may not be installed properly.
Troubleshooting
Important
Use the following troubleshooting guide if you encounter problem during installation.
Some Packages Cannot Compile Correctly
Attempting to run the installation command might sometimes result in compilation errors for M1 devices. Here are som epossible errors that fall under this category.
-
Incompatible Architecture
-
Python Quitting Unexpectedly
-
Subprocess Error While Installing Dependencies
If you encounter any of the problems above, follow the steps below for an alternative installation method.
Alternative Installation Method
-
Install Homebrew by running the following command.
If you receive a "
Connection refused
" error, consider switching your internet connection provider. -
You will be prompted to input your sudo password. This is your Mac password. It will NOT be shown when you are typing it, so make sure you are typing the correct password.
-
Read and follow any additional instructions that you need to do on the terminal windows.
-
Once finished, run the following command to install
openblas
andgfortran
from Homebrew. -
Run the following command to uninstall your previously installed Python packages because we will be using a new one.
-
If you receive a prompt to upgrade your
pip
version, please do so by running the command shown on the screen. -
Quit the Terminal app.
Important
Quitting the Terminal app is different from merely closing the Terminal window. Make sure you do not have any Terminal windows open. Then you can use the command Cmd+Q. Alternatively, you can follow the image below.
-
Locate Terminal in your Finder (User > Applications > Utilities). The image below will guide you as you navigate yourself through the files.
-
Select Terminal and press Cmd+I OR right click and select "Get Info" as shown above.
-
This will open the information window about Terminal. Check the "Open using Rosetta" option as seen in the image below.
-
Reopen Terminal app.
-
Run the following command.
-
Run the following command.
-
Locate Terminal in your Finder (User > Applications > Utilities). The image below will guide you as you navigate yourself through the files.
-
Select Terminal and press Cmd+I OR right click and select "Get Info" as shown above.
-
This will open the information window about Terminal. Uncheck the "Open using Rosetta" option as seen in the image below.
-
Quit the IDLE application if it is still open.
-
Locate IDLE in your Finder (User > Applications > Python3.12). The image below will guide you as you navigate yourself through the files.
-
Select IDLE and press Cmd+I OR right click and select "Get Info" as shown above.
-
This will open the information window about IDLE. Check the "Open using Rosetta" option as seen in the image below.
-
Try and run the
import
statement again.
Packages Installed Correctly But Unable to Import
See below...
Previously Installed Anaconda/Conda
Sometimes due to multiple versions of Python installation present, IDLE will not import packages from the correct version. This issue is especially pertinent to users who have previously installed Anaconda.
If you are unsure about whether you have Anaconda installed, open a new Terminal window and look our for (base)
on the left.
Follow the guide linked to remove Anaconda.
Warning
Beware of the rm -rf
command.
Incorrect usage may result in PERMANENT and IRRECOVERABLE loss of data.
Additionally, open .zshrc
or .bashrc
and remove all traces of Anaconda.
Check if the removal process is under control by closing ALL Terminal instances and reopening them.
However, if for any reason you want to keep Anaconda around, you do not have to remove them.
Simply run deactivate
to exit from the Anaconda base environment every time you launch Terminal.
You can then follow the installation instructions again.
Bouncing Rocket Icon Appears in Dock
This is normal, you can ignore it.
This might happen when one tries to import cocos
which will interact with the Python graphical user interface (GUI).