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.
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.
The latest version of python is python3 and the latest release is python3.9.0.
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.
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.
The purpose of virtual environments is to have a separate space where you can install packages which 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:
-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:
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.
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.
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:
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 will install other dependent packages as well along with python3 like pip etc
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
Add this line in the file called .bash_profile present in 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 the 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
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 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.
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
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
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 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.
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.
This article will help you with stepwise instructions on the installation of python on mac.
Your email address will not be published. Required fields are marked *
Statistics is a science concerned with collection,... Read More