For enquiries call:

Phone

+1-469-442-0620

HomeBlogData ScienceHow to Install Python on Mac

How to Install Python on Mac

Published
02nd May, 2024
Views
view count loader
Read it in
5 Mins
In this article
    How to Install Python on Mac

    This article will help you in the installation of Python 3 on macOS. You will learn the basics of configuring the environment to get started with Python. The Python learning program on Knowledgehut is the best advanced python course online that deals with the installation of Python on different operating systems along with a strong curriculum. Let us get started with a brief introduction to Python in this tutorial.

    Brief introduction to Python

    Python is an interpreted programming language that is very popular these days due to its easy learning curve and simple syntax. Python finds use in many applications and for programming the backend code of websites. It is also very popular for data analysis across industries ranging from medical/scientific research purposes to retail, finances, entertainment, media and so on.

    When writing a python program or program in any other language, people usually use something called an IDE or Integrated Development Environment that includes everything you need to write a program. It has an inbuilt text editor to write the program and a debugger to debug the programs as well. PyCharm is a well-known IDE for writing python programs.  

    Latest version of python

    The latest version of python is python3 and the latest release is python3.10.1.

    Installation links

    For downloading python and the documentation for macOS, visit the official website and go to the downloads section, from where you can download the latest python version for macOS.

    How to Install Python on Mac

    Key terms (pip, virtual environment, path etc.)

    pip:

    pip is a package manager to simplify the installation of python packages. To install pip, run the below command on the terminal:
    curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py.
    If you install python by using brew which is a package management to simplify installation of software on macOS, it installs other dependent packages as well along with python3  like pip etc.

    virtual environment:

    The purpose of virtual environments is to have a separate space where you can install packages that are specific to a certain project. For example if you have a lot of flask or Django-based applications and not all the applications are using the same version, we use virtual env wherein each project will have its own version.

    In order to use a virtual environment you need to be on the python 3.x version. Let’s understand how to create the virtual environment. You do not need any library as it comes along with standard python installation.

    So to create a new virtual env, run the below command:

    python3 -m venv demo

    -m expects a module name which is venv in this case, so with this python searches your sys.path and executes the module as the main module.

    Venv expects an environment name that you must create.

    Now you should have a new environment called demo. Let’s activate this virtual env by running the below command:

    source demo/bin/activate

    After running this, the environment is activated and you can see the environment name in the terminal. Another way to check if the env is activated is by running which python. You will see the python that you are using with this project env, and the version that it will use is the same that you used to create the environment.

    Getting and Installing MacPython:

    For MacOS, python usually comes pre-installed, so to check if python is installed open the terminal in the mac and use `python --version` to confirm the same. You can also see what is the default python version installed, which is usually python2.x by default. However, Python2.x is going to get deprecated soon, and with everyone moving to python3.x ,we will go with the latest python3 installation.

    Knowledgehut is known to offer the best python programming course online and deals with the entire installation process in greater detail. Here are the steps you can follow to install Python on Mac.

    Installation steps

    For downloading python, visit the official website and go to the downloads section. You can download the latest python version for MacOS as shown below:

    How to Install Python on Mac

    It will download a pkg file. Click on that file to start the installation wizard. You can continue with default settings. If you want to change the install location, however,  you can change it, and then continue and finish the installation with the rest of the default settings.

    Once the installation is finished it would have created the python 3.x directory in the application folder. Just open the application folder and verify the same.

    Now you have python3.x installed.

    To verify it from the terminal, go to the terminal and check the version of python by using `python --version` command. So you will still see it is showing the old default python version, Now instead if you use python3 explicitly like `python3 –version, you can see the version that you have installed with python3 version.

    Once the installation is finished it would have created a python3.x directory in the application folder. Open the application folder and verify the same.

    You can also install python3 on mac by using brew which is a package management to simplify installation of software on macOS.

    brew install python3

    brew will install other dependent  packages as well along with python3  like pip etc

    Setting path

    Suppose you have installed a new python 3  version but when you type python it still shows the default python2 version which comes by default in mac os. To solve this, add an alias by running

    alias python=python3

    Add this line in the file called .bash_profile present in the home directory. In case this file is not present, you can create it, save the changes and restart the terminal by closing it. Next, open the terminal and run python, and hit enter. You should see the latest python3 that you have installed.

    Sometimes when you type python or python3 explicitly, it does not work even if you have installed Python. You get the message, “command is not found”. This means the command is not present in the directories used by the machine for lookup. Let’s  check the directories where the machine is looking for commands by running

    echo $PATH

    It will list all your directories where the machine looks for commands. This will vary from machine to machine. If the command that you are trying is not under the directory path listed by echo, that command will not work. It will throw an error saying the command is not present until you provide the full path of the directory where it's installed.

    Now let’s open the file  .bash_profile and add the directory path where python is installed to the current path env variable

    For example, let’s add the following lines in that bash_profile file which will add the below directory to the current env variable. This can vary from machine to machine based on the installed location.

    PATH=”/Library/Frameworks/Python.framework/Versions/3.7/bin:${PATH}”
    export PATH

    Save the changes and restart the terminal. Open the terminal now and run echo $PATH again and see the above path that you added for python3. When you now type python3 command, you should see it working.  

    Also, if you are trying to import a package that you have installed and it says that it cannot find that package, this means pip install is installing the packages in the different version of python directory. Make sure the location of the package is in the site-packages directory of the version of the python that you are using. You can see the location of the package that you are trying to import by running  

    pip show <packagename>

    The above command will have a location field in which you can see and cross verify the path.

    9. How to run python code

    To run python code just run the command
    python <pythonfile.py>

    Installing Additional Python Packages:

    If you want to see what all packages are installed in the env, run the command pip3 list which will list down the current packages installed. Let’s say you want to install the request library. You can just install it by running pip3 install requests. Now try running pip3 list again, to see this requests lib installed in this env.

    Directory as a package for distribution:

    Inside the python project or directory, you should have a file called __init__.py. You can create this file by a simple touch command, and this file does not need to have any data inside it, All it has to do is to exist inside the directory, for that to work as a package.

    Documentation links for python

    Python Doc

    Conclusion

    This article will help you with stepwise instructions on the installation of python on mac. In case you are wondering which python IDE you should use, here is our recommendation for the best Python IDE and code editors for Linux, Windows, and macOS. 

    Once you narrow down your choices start running Python scripts or apply advanced concepts like exception handling in your projects. You can also enroll in Knowledgehut best python programming course to move a step higher and start working on real Python projects and shape your career in different fields like web application development & data science.

    Profile

    Ashish Gulati

    Data Science Expert

    Ashish is a techology consultant with 13+ years of experience and specializes in Data Science, the Python ecosystem and Django, DevOps and automation. He specializes in the design and delivery of key, impactful programs.

    Share This Article
    Ready to Master the Skills that Drive Your Career?

    Avail your free 1:1 mentorship session.

    Select
    Your Message (Optional)

    Upcoming Data Science Batches & Dates

    NameDateFeeKnow more
    Course advisor icon
    Course Advisor
    Whatsapp/Chat icon