Pragmatic Python IV

Installation

Shuvo Saha
intelligentmachines

--

This is part 4 of the series. To go back to part 1, click here.

Setting up Python

A very quick and easy way to set up Python is to use a toolkit such as Anaconda. Mainly intended for data science, Anaconda has a lot of useful features that we will leverage in the course.

I also recommend using the Miniconda version (a barebones version of Anaconda) since it has all the features we need and is much smaller. You can download Miniconda from here.

At the time of making this tutorial, Miniconda’s latest version was Python version 3.7. Therefore, this tutorial will focus on Python 3.7, but all of it will still work on 3.8.

Note: Unless explicitly written, instructions are the same for Mac and Windows.

For Windows users

You’re most likely using a fairly recent PC (2010 or later), so click on the 64-bit installer. In case you’re unsure, a quick Google search or this article can help to see if you have a 32-bit CPU.

After you’re done downloading the installer, the setup is fairly straightforward. I’d recommend you install in the default location, select “Just Me” (so that permission annoyances can be avoided) and deselect the “Add Anaconda to my PATH environment variable” option. This PATH option would allow you to use Anaconda commands on the windows default command prompt, which is unnecessary as Anaconda adds its command prompt, innovatively named the Anaconda Prompt.

For Mac users

Make sure to download the file ending in “pkg” not “bash”. This will start up the graphical installer. You’ll only need to select the ‘Install for me only’ option on Mac.

For Linux users

Don’t be fooled, Linux doesn’t even have Microsoft Word. I’m kidding, it’s not that bad. Oh, did I mention it doesn’t have your favorite games either?

This tutorial will not cater to Linux users, since these users are more than likely to be adept at using computers, know how to code or have a CS degree. All of these people fall outside the target group of the tutorial. This isn’t because I hate both Linux and my CS friends who ask me to try Linux.

Code Editors

While it is completely possible to write code on a text file, I recommend using one of the popular Python code editors such as Visual Studio Code, Atom, Sublime Text, or PyCharm. For this tutorial, we will go with Visual Studio Code or VS Code since it’s lightweight, fast, and has the features we need. For a PyCharm installation tutorial, I recommend this website.

VS Code can be downloaded from their website. Similar to the Anaconda installation, make sure to select the right operating system.

The first two options will allow you to right-click on files to open up a Python file in VS Code directly, the 3rd will automatically open Python files using VS Code, and the 4th will allow you to call VS Code from your command prompt (we’ll get to the command prompt later in the tutorial).

We can now start up VS Code and write our first script. Note that “scripts” and “programs” aren’t the same but are used interchangeably in this article.

Setting up VS Code

You can do this using the shortcut on your desktop or start menu but I recommend the following to get accustomed to the command prompt:

For Windows, go to your start menu and find Anaconda Prompt and do the following:

  • Typecodeand press Enter on the window. If you made sure to click on the “Add to PATH” option when setting up VS Code, this command should start up VS Code.
  • Once VS Code starts up, you can click on the New File option below the Start header.

For macOS users, a few extra steps are required:

  • For Mac users, launch VS Code normally. Now open the Command Palette (⇧+⌘+P) and type shell command to find and select the Shell Command: Install 'code' command in PATH command. This is a one-time setup.
  • Start the terminal, type codeand press Enter on the window.
  • With VS Code on, click on the New File option below the Start header.
  • With a new file opened up, type the following:
name = input("Please type in your name: ")print(f"This is your first program in Python, congrats {name}!")
  • Now press CTRL+S to save the file. This should open up a window asking you what you want to save this file as. Give it the name of your choice ending with “.py” at the end like below. You can also toggle the Save as type to the Python file extension. Make sure you remember the exact place where you saved this file.
  • Once you’ve saved the file, VS Code should throw up this error:
  • Install Python either by clicking on the error or searching for Python on the extensions tab.
The extensions tab should be on the sidebar to the left.
  • VS Code will probably ask you to install pylint so go right ahead and do that.
  • We now need to select the Python interpreter. This is basically the Python environment we installed using Anaconda or Miniconda, to be exact. Click on the error banner on the bottom left or press F1 to bring up the command panel and search for Python: Select Interpreter.
  • Select the base environment, it should be named something like this:

Congrats, you’ve installed python and a code editor. Let’s start our first program then, shall we?

Running Scripts through VS Code

We can run the script we prepared through VS Code by pressing F5 or the green arrow on the top right:

Note: If you run into any errors, do the following — on the top left, go to File>Preferences>Settings and type python conda path to find the Python Conda Path setting. Set that to your Anaconda location. You can find this by typing where python in the Anaconda Prompt or terminal. For me, the path looks like this:

C:\Users\shuvo\miniconda3\python.exe

Another fix is to close VS Code and start it through the terminal or Anaconda Prompt by typing code every time.

You should be asked to type in your name in the terminal when you start the program we made.

Note that VS Code will automatically tell you if you’re making errors by highlighting them (linting). You’ll be spending most of your time fixing errors when you’re coding, so we’ll talk more about debugging, the process of finding errors in your programs, in a future section of the tutorial.

Running Scripts through the Command-Line

The Command-Line is a very useful tool used to send commands to your computer. Running commands on the command line are as simple as typing out the instructions and pressing enter. To see a list of comprehensive commands that you should at least gloss over, go to this website for Mac users and here for Windows users. For a longer list go here.

For Windows Users

The first command you’ll learn is cd or change directory. This command is used to change the working directory. Very simply, point it to the folder you saved your Python file in.

To use the command line, open Anaconda Prompt and point it to the correct drive by typing in the drive name followed by a colon such as E:

Here, I changed my working drive to E:

Next, change the working directory to the file location. Here, my Python script is stored in my E: drive under the Tutorial folder. Type cd E:\Tutorialmaking sure to replace the folder name with the folder you want Python to go to:

Hot Tip: You can copy the whole destination by going to the file in Windows Explorer and putting your cursor on the location bar.

Now type in pythonfollowed by the name of the python script like so:

For Mac Users

To use the command line, open the Terminal.

We need to locate where we saved the python file on VS Code. You can see filepaths on Mac by just right-clicking and copying the path like below:

Filepaths have forward slashes ( /) on Mac instead of the backward slash on Windows.

Once we have the filepath copied, we can open it in the Terminal by typing in cd filepath. For us, that’s cd tutorial/tutorial code.

Next, type in python followed by the name of the file. For us, that’s python first_program.py.

Hot tip for both Operating Systems: You can drop a file on the command line to reveal its path on both macOS and Windows. You can also do the same operations (using cd and dropping the file) on the built-in VS Code terminal and it’ll work in the same way!

The Workflow

I swear programming humor is just hating on Java.

Most programs you’ll make will run in a text environment such as the command line as opposed to the graphical interface we all associate with computer programs. You can wrap this script in a nice Graphical User Interface (GUI or gooey) to make it look like a normal application, but that extra effort might be unnecessary. Form over function is what my dad said when I was born. Unfortunately, I lost my good looks too.

Your programming workflow should be pretty straightforward now:

  • Start VS Code using the code command or the app shortcut.
  • Write some code and save it as a python file in a directory. Remember the filepath.
  • Try running it inside VS Code first to check if it works.
  • Anytime you need to use your program and you don’t need to change the code itself, run it from the anaconda prompt or terminal.
cd filepath
python filename.py

No, that’s too much work

What if I just want to do something really quickly and only a few times, at max? Is there any way I can breakdown my code and run it bit by bit?

The answer to both of these questions is a resounding yes. A bunch of people realized that when we’re carrying out exploratory data analysis, we have to mess around, do a lot of things, see what sticks and look at the intermediate results.

Enter Jupyter Notebook. It’s built right into Anaconda and VS Code has a well-built extension for it too. It allows you to run code block by block and see the results instantly. We’ll dive into how to use this is in a future section.

I’m not kidding, programming humor is all just hating on Java.

Congrats! You can now write and run python code locally. In the next few parts of the tutorial, we’re going to build on the knowledge we gained to make some nifty programs. Some things we’ll look at include but are not limited to:

  • An Excel file merger.
  • A file organizer.
  • Debugging & theft 😉.
  • Jupyter Notebook and its uses.
  • An automated email program for mass, customizable, human-like emails.
  • A spreadsheet data cleaner.

--

--

Shuvo Saha
intelligentmachines

A business grad who spends way too much time on his computer. Currently a Marketing Analyst at Wise!