Installing Python 3.7 and PyCharm on Mac

Background:


This article will instruct you on how to install the newest version of Python and PyCharm.

I have a video: https://youtu.be/oyzH4M6X6F4 on YouTube that shows you this material as well.

Python is the most popular new programming language, and as of this writing is at version 3.7

PyCharm (Pie-Charm) is a great IDE for python similar to Visual Studio code, but more mature in its features. It is made by JetBrains that also developed Android Studio for Google apps.

PyCharm comes in a free 'community edition' or a paid pro edition. The paid pro edition is free to educators and students that provide proof of such.

The macOS comes with python2.7 already installed. You should leave it alone and install the newer version of python so both will be on your system when you are done.

Existing python2.7


The macOS comes with python2.7 already installed. You should leave it alone and install the newer version of python so both will be on your system when you are done. There may be apps that depend on the older 2.7 being present.

You can invoke python and idle from the terminal application as show here:











Here is a view of my invoking python --version to see the version installed and starting idle shown in the background. In idle I have type the version 2.7 print statement followed by a comment and it ran and did the output.

Installing Python:

When you install python, you are installing several tools and a directory of a 'framework' of libraries to run python.

The most important tools you will be using are  python3, idle3, and pip3, and these are the names of the commands for these tools in the terminal application.

python3 will run python programs.
idle3 allows you to type in python code and run it interactively.
pip3 allows you to add packages or modules to python to do more things, there are thousands of python modules.

1. Open a browser and go to python.org:


Just click on downloads in the menu, and then click on the Python 3.7.0 button, or a later version if it has changed since this post. This will start the download to your downloads area called python-3.7.0-macosx10.9.pkg where the version number 3.7.0 may be more advanced.

Find the file in finder and open it to start the process:


Through most of the process we will accept the defaults and continue:

Click continue on the "Introduction" panel.
Click continue on the "Read Me" panel.
Click continue on the "License" panel, then agree with the License Agreement.
Click install on the "Destination', and then enter your password to continue.
The Summary panel and a documentation page will open. Click the close button the panel.
The last dialog asks if you want to move the installer to the Trash. You should choose to do so.

Verify it is installed

Now open the Terminal application and type python3 and hit enter.
You should see the version information as show below and then a  >>> prompt
Type 2 + 2 and hit enter, and it will evaluate this as a python statement and type the correct result
Type exit() and hit enter and it will exit the python interpreter.


Close the terminal
At this point you should have python3, idle3 and pip3 commands available from the terminal
The location of python3.7 will be at the folder  /usr/local/bin    You will refer this path later for PyCharm. 

Also for your information,  all the files for this new python will be located at

/Library/Frameworks/Python.framework/Versions/3.7/  

Installing PyCharm

Open a browser and click on:  PyCharm Download


Click on the Download button for the Community Version which is free.
When download is complete open the pycharm-community-2018.2.dmg file.

Drag the PyCharm CE. app file to the applications folder to install.

Go to your applications folder and open PyCharm CE, choose open in the resulting security dialog.

If you see the Customize PyCharm dialog, just choose the Skip Remaining and Set Defaults button in the lower left.

You should see:


Pull down the Configure Menu at bottom right and choose Preferences.

You will see:


Choose the Project Interpreter from the left menu.

Click the gear icon on far right of Project interpreter box and choose Add...

Click on the System Interpreter on left menu.  Now, in the interpreter box you should see

/usr/local/bin/python3.7

This is the location of where you just installed python, if it does not list this, use the arrow to pull down a list of python versions installed on your machine and choose the one you just installed.

Click OK, and then when the Project Interpreter dialog shows, click OK to that.

PyCharm will be busy for a while, just wait till it is done and is back to the window that has the choices: +Create a new Project, Open, and Check out from Version Control.

PyCharm is now ready to use. You could close it now. We will write a small program and point out some features.


Write a program in PyCharm


If PyCharm is not open, then open it. and select +Create a new Project

You see:

The untitled1 is already selected, just type hello to replace it, this is our new project name and it will create a folder at:    /Users/name/PycharmProjects/Hello  where name is your mac user name.

Click Create.

Your new Hello project will open this main window:

The small Tip of the Day show up to teach you about using PyCharm, just click close for now. But remember that they may be useful later, You can always get it back from the Help menu.

Right click on the Hello folder listed in the 'Project' panel on the left side and choose
New --- Python File

Type hello for the file name, PyCharm will add the python extension .py to it automatically.

Your text cursor will now be in the large editing area ready for you to write a program.  Type the following program in:

name = input('name? ')
print(f'Hello {name}') 

Then right click anywhere in the editing area and choose Run 'hello' or you can just use the keyboard shortcut  ctrl+Shift R

This will open a Run panel at the bottom that should now show:

/Users/gjenkins/Hello/bin/python /Users/gjenkins/PycharmProjects/Hello/hello.py
name?
                                                      


Go ahead and type your name in and hit enter.
it should now complete the program by outputting hello and your name:

/Users/gjenkins/Hello/bin/python /Users/gjenkins/PycharmProjects/Hello/hello.py
name? Gerry                                                                                                            
Hello Gerry                                                                                                              


You window should look like this when you have run your program:


Note: the first line before your program runs is the command that PyCharm executes to run your project. You can actually copy and paste it into terminal to run your project. There are two parts:

/usr/local/bin/python3.7  is the location of the python you installed.


The second part is the path to the actual py file in your project folder to execute
/Users/gjenkins/PycharmProjects/Hello/hello.py 

Trick to see all the installed terminal commands for python 3.7:

Open terminal and type this command:

ls -l /usr/local/bin | grep "3\.7"

That's It,

Happy coding, and check out my video and subscrib to my YouTube Channel: gjenkinslbcc 

3 comments:

  1. This comment has been removed by a blog administrator.

    ReplyDelete
  2. Thank you so much, Gerry! Your are doing a great job! So many teachers around and so few can explain well.

    ReplyDelete

  3. Really nice and interesting post. I was looking for this kind of information and enjoyed reading this one. Keep posting. Thanks for sharing DevOps Training in Chennai | DevOps Training in anna nagar | DevOps Training in omr | DevOps Training in porur | DevOps Training in tambaram | DevOps Training in velachery

    ReplyDelete

Please comment or give suggestions for follow up articles