With machine learning and automation becoming a hot topic, you may have thought about getting started with learning a programming language, like Python. Seeing really cool case studies at virtual conferences or finding discounts on coding courses during the pandemic could be driving that desire too. Before you can start running scripts, you should ensure you have the essentials down first! While this post will focus on setting up a Python environment, many of these tips can be applied to setting up other programming environments as well.
- What tools you need to start programming
- How to install Python
- Packages and how to install them
- How to edit and run a Python script
What tools you need to start programming
To get started, you’ll need a couple of tools, a command line interface (CLI) and a text editor.
Command Line Interface
Access the CLI
Your PC or Mac will come with a built in CLI. On Mac this is called the Terminal, and on Windows it is called the Command Prompt.
- On Linux/MacOS, go to Finder > Application > Utilities > Terminal.
- On Windows, hit the Windows button + “S” to open search and search for “command prompt.”
These CLIs will serve basic needs, but other shells for the CLI are available, such as Bash for Mac (or specially configured for Windows) and Windows PowerShell. These different shells take commands differently, which you’ll see as you continue reading. Once Python is installed you’ll also have a Python shell, which will then interpret Python.
Understand the CLI
Before you start installing Python, you will want to get comfortable with the CLI. When you first open it you will see a prompt. The prompt begins with a computer-generated tag that usually ends in %, $, or > to indicate that you can start your command.
On Windows, the computer-generated prompt indicates the path of your current directory, which helps you see where (in what folder, aka directory) you are running commands. Knowing where you are is important, because you have to be within the directory in which a script is saved in order to run it. Once we get to moving around the directories, the same will happen on a Mac. Let’s confirm our current directory and see the difference between a command prompt and the output.
- On Linux/MacOS, type in “pwd” which stands for “print working directory”. The output should likely be “/Users/{your username}”. As you’ll see the output does not end with a “%” like our command prompt line does.
- On Windows, type in “cd” which stands for “change directory.” Since we aren’t adding the name of a directory to change, the output will be our current directory. The output should be the same as your path in your prompt “C:\Users\{your username}” As you’ll see the output does not end with a “>” like our command prompt line does.
Since it can be difficult to find applications loaded at the user level, we should move into a folder/directory that we can more easily check with our File Explorer (on Windows) or Finder (on Mac). So enter the command:
cd desktop
which means you are now at a directory with the directory path of Users > {your username} > Desktop.
Your command prompt should be updated to indicate you are in the “Desktop” directory as well. If you want to be sure, type “ls” in Linux/MacOS and Windows PowerShell or “dir” in Windows command prompt to see a listing of all of the files and folders within your current directory.
Let’s create a new directory inside of “Desktop” called “python.” Type the command:
mkdir python
When you see your new command prompt line, the directory should have been created.
Now you can use your Finder or File Explorer (or just go to your Desktop) to visually confirm that your CLI command generated a new empty folder/directory on your Desktop called “python.”
Let’s move into that directory in the CLI. Type in:
cd python
Again, your prompt should update to indicate you are in the “python” directory now.
Below is just a sampling of some of the basic commands you need to understand in order to install Python and run Python scripts. See a full list of MacOS commands or Windows commands for more. However, you should probably stick to these more basic commands as you get started, so you can avoid running into any dangerous commands that can remove directories or run malicious scripts.
Action | Windows Command | MacOS/Linux Command |
Find your current path/directory | cd (does not output properly in PowerShell, so use the Mac command instead) | pwd |
Change folder/directory | cd [name of folder/folder path] | cd [name of folder/folder path] |
Go back a directory | cd .. | cd ..(if you know a directory/directory path off the “home” directory you can use ~/[directory name/path]) |
Make a new directory | mkdir [directory name] | mkdir [directory name] |
List all files in the directory you are currently in | dir | ls(can be used in Windows PowerShell too) |
Clear the screen | cls | clear (can be used in Windows PowerShell too) |
Output text to the terminal | echo [a string] | echo [a string] |
Text Editor
Unless you want to create files and write all of your code inside of the CLI (if you think you do, check out Vim and godspeed!), you should get a text editor in which to write and edit your Python scripts. TextEdit and Pages, which come installed on MacOS, will not allow you to save a file with the “.py” extension (which is the file type used for Python scripts and modules), so you will have to download another text editor. I use Atom, but there are many others to choose from like Notepad++ for Windows, Sublime, and Brackets. Make sure your text editor works on your operating system!
How to install Python
Now that we have the tools, we need to make sure we have the updated version of Python installed, especially since many MacOS versions have Python 2 included. At the time of writing, the latest version is 3.8.5. (I’m still using 3.7.5 on my PC which you may see noted in screen shots throughout).
MacOS Installation
Because I am using a Windows device, I asked a team member to walk through the steps outlined in this guide for installing Python 3 on a Mac. While the guide did the trick, a couple of additional prompts weren’t noted.
When checking for Python 3 by using the command of “python3 –version” in the Terminal, you may receive a notification/alert to install the Xcode command line developer tools.
Once those are installed, you then have to go into Xcode to agree to the terms and conditions before you can move on to installing homebrew. Then while installing homebrew, you may be required to input your device credentials.
Finally, the really big caveat here is that you’ve installed “python3” and without changing your system path (which is NOT recommended on MacOS), you will now have to run all commands with “python3” versus “python.” If you want to see this in action, type in “python –version” in your terminal and then “python3 –version” to note the difference. Remembering to start your commands with “python3” might not be a huge issue, but it can easily be resolved by using virtual environments, which is what we recommend as we tackle using Python packages in another section.
Windows Installation
For the Windows install, simply download the latest version from https://www.python.org/downloads/ and run through the Windows installer.
Be sure to check the box that says “Add Python X.X to PATH” as that will update your Windows’ executable path, so the system knows that “python” commands should use this latest version. If for some reason you find yourself having trouble accessing the latest version in your command prompt, you can add or update your environment variables.
Check your install
Once you’ve completed your respective install, type in “python”, or “python3” if you are using MacOS, into your CLI. If it takes you into a new Python shell, which is indicated by a prompt of “>>>” only now, your install worked!
I also wanted to take this opportunity to drive home the differences in “shells.” Now you are in a Python shell, so your commands should be written in Python’s language. Type:
print('Hello World!')
and note that we did not include “python” or “python3” at the beginning of the command — we don’t have to, now that we’re in the Python shell.
You wrote your first Python script!
To get out of subshells (which will have a different prompt than your usual command line prompt), type “exit”. In this example, Python indicates some additional steps to take to exit, by typing “exit()” (Related tip: if you have something running in the CLI that needs to be stopped [an infinite loop perhaps], you can try “CTRL + C” to interrupt the keyboard).
Go ahead and exit out of the Python shell.
After you’ve exited the Python shell and are back in your command prompt, if you tried typing “print(‘Hello World!’) you’d end up with an error message (or maybe a piece of paper with “Hello World” printed on it if your Windows printer is set up!).
Without indicating we are using “python” or “python3” at the beginning of that command, our shell couldn’t interpret it. This is why I tend to use “python” for all my commands even if I am in a Python virtual environment.
Instead of print(), the CLI uses the “echo” command to display a message on screen. Try “echo Hello World!” instead.
Always pay attention to the prompts to understand what shell you are using and what commands it can interpret!
Packages and how to install them
Although your Python install can get you started with some basic scripts, you will likely need to add packages to help with more advanced needs.
One very popular package is Requests, which allows you to request URLs in your Python script by simply calling requests.get(). When using Requests, you may also want to use the BeautifulSoup library to help you traverse and extract HTML elements. New packages can be created by anyone from anywhere in the world! To find many of them, you can utilize the Python Package Index (PyPi).
When selecting a project on PyPi, at the top of the screen you will see the name of the project and then a command that begins with “pip install [the name of the package/project].” Pip is a package installer for Python that is already included in your Python installation! If you are familiar with JavaScript, PyPi and pip are to Python as npm is to JavaScript.
Let’s do our first “pip install” command to install Pipenv. In your command prompt, type:
pip install pipenv
Your command prompt line will disappear while the command is being run, and eventually you’ll see some outputs to show progress is being made on the install.
Using a virtual environment
Python and pip install globally on your operating system, which is why environment paths can become an issue. If you were to load all of your packages globally, you’d eventually start overwriting old versions of packages in projects that may not work with a newer version of that package. To stop from introducing breaking changes into your projects, you can run a virtual environment for each project that loads your packages just within that project. This also helps you to explicitly run Python2 or Python3, which helps resolve the path issue on MacOS.
To get your virtual environment set up, you will want to be in your specific project directory. In that case, you may want to create a new directory within your Desktop > python directory. Let’s make one called “my-first-project”. Make sure your command prompt shows you are in your “python” directory and type in:
mkdir my-first-project
Now move into that directory so we can create a virtual environment for it. Type in:
cd my-first-project
Let’s use pipenv to install Requests in this directory. Type in the command:
pipenv install requests
Then you will see a series of outputs. You’ll know it is complete when you see your command prompt line again.
The output line that begins with “Using” (highlighted in yellow below) indicates which version of Python the virtual environment will use. This is important for MacOS, because when your virtual environment is using Python 3, you will then be able to run “python” commands instead of “python3” as long as your virtual environment is running.
Towards the end of the outputs there is some guidance on how to activate your project’s virtual environment by typing “pipenv shell”. Let’s do that to see what happens.
pipenv shell
My output indicates that a “subshell” has been created, which means I am in a new shell within my Command Prompt. As you start filling up your screen, you’ll be able to tell you are still in a virtual environment because your command prompts include the project name in parentheses, and is the same on MacOS as well.
If you are running MacOS, type in “python –version” to see if your virtual environment is running Python3 as “python.”
To exit your virtual environment shell, type “exit”. You’ll see the parentheses are no longer on your command prompt once you’ve exited.
If you are on MacOS, type in “python –version” again to see if you are back to the OS’s default Python2 version.
Now that we have a Python environment that includes Requests, we can import this library into our Python script to use it.
How to edit and run a Python script
It is finally time to jump into your text editor! Open a new file to get started.
We installed the Requests library in our “my-first-project” virtual environment so that we can more easily handle HTTP requests. In order to use them in our script, we first must import that library into our code. To find out how to properly import different libraries, you will need to refer to the individual library or projects’ documentation. As you dig further into PyPi, you’ll see that poor documentation can make it difficult to utilize some packages/projects.
Start your new file out with “import requests”.
Now we are going to have Requests “get” the response from “https://www.google.com.” In order to work with the response data, we need to declare the response, so we will declare it as “r”
Add “r = requests.get(‘https://www.google.com’)” to your file.
Now “r” is an object that we can work with based on Requests’ documentation. For this quick example, we will just get the status code of the webpage using “r.status_code”. In Python, we can have that output to the shell with “print()”, so add “print(r.status_code)” as another line in your file.
import requests
r = requests.get('https://www.google.com')
print(r.status_code)
How and where you save your file is critical to your project environment setup as well. Let’s “Save As” into our “my-first-project” folder.
When naming your directories/folders and files, make sure you are not using spaces. If you do use spaces or other special characters, you may have to use the “\” as an escape character or put your file or directory name in double quotes, ie: Desktop/python/”my first project”, in the command line. Python has naming conventions for your variables, classes, modules, and packages. Keeping those naming conventions in mind for this module, we should use all lowercase with underscores for improved readability. (Coming from doing JavaScript development, it has been very hard for me to let go of using mixedCase!) Let’s name this check_status_code.py
Now that your text editor understands you are using Python (the .py file extensions do that!), it should update your code with different color schemes to indicate variables (and the type of variable) versus functions versus statements (like “import”). Atom also shows your files at your project level, which helps you visualize your project files.
Now that we have a Python script saved, we can go back to our shell to run it.
Make sure your prompt is at your “my-first-project” directory. If you have closed your CLI, you can quickly jump to it by typing:
cd Desktop/python/my-first-project
That is the full path from your CLI’s root directory.
If your virtual environment is not running, you need to start that again with the command “pipenv shell”.
If you are in your “my-first-project” directory and the virtual environment is running (you will see your project name in parentheses at the beginning of your prompt), you can now run the command:
python check_status_code.py
Remember our script should “print” to the shell the status code of “https://www.google.com,” which should hopefully be “200.” You should see “200” (highlighted in yellow below) in your output once the script has run.
Success! You’ve got a fully functioning Python environment and ran your first script. In order to really nail down this whole idea of directories, though, let’s add a little bit more complexity by creating a urls.py file/module in our project.
Open a new file in your text editor and Save As “urls.py” into your my-first-project folder. We now have a Python module called “urls” in our project. Within this urls.py file, create a list called “url_list”. We will use “https://www.google.com” again and an invalid URL of “https://www.google.com/search-support”. Those need to be set as strings inside of the list. The entire file will be:
url_list = [
"https://www.google.com",
"https://www.google.com/search-support"
]
Be sure to save! Your text editor will likely add a dot to the file’s tab to indicate it is unsaved.
Now go back to the check_status_code.py file to import this list and loop through it.
To get this list, we can use the statement “from [module] import [object]”, so below our import of Requests, add:
from urls import url_list
We need to update our script so that it will loop through the list instead of just getting the static string of “https://www.google.com”, as it currently does.
First we need to create a “for” loop of our url_list where we also declare the item in the list as a variable. Since that item is a URL, we will call the variable “url.” This is completed with one line of code to put after our import lines.
for url in url_list:
Whatever needs to happen inside of this loop should be indented, so tab our next two lines in.
Finally, we need to remove the static URL string in our get request and use our “url” variable instead. Change ‘https://www.google.com’ to “url” without any quotes, and save.
Now let’s go back to our shell, where we should still be running our virtual environment on our “my-first-project” directory. Type in “python check_status_code.py” again. (Tip: in some shells, you can hit the up arrow key to show previous commands you’ve run). Since we have two URLs to check, the output will have two lines: “200” for the first and “404” for that search-support URL I made up.
Super cool, right?
See how quickly we can break this by going into your Finder or File Explorer and physically moving your “urls.py” file up a path to your “python” folder.
Now try running your check_status_code.py script again. You’ll get an error message that the “urls” module cannot be found.
Move that file back to your project directory!
A similar error is provided if you move up a directory (using the command “cd ..”) and attempt to run the check_status_code.py script.
While these provide you with some examples of Python error messaging (which is actually pretty sophisticated, if you started with JavaScript), this again shows how important saving and naming (watch for misspellings between the filename and your imports!) your project files and knowing how to navigate to those files in the CLI is to working in Python.
With your environment set up, you are ready to start coding! Don’t already have a project in mind? Maybe these will get you started.
- My GitHub repo: I’ve got some very simple scripts in my GitHub repo that help me automate everyday tasks, like having a browser automatically open a long list of URLs or search queries so I can just push the “back” button to review them. Be sure to install the Selenium package, download the Chrome webdriver, and update the path to your webdriver file in these scripts. I’ve also got a more complicated script that mimics a mobile device to capture Interesting Finds feature data and take screenshots of the mobile SERP.
- Spacy: This is a package I want to play around with more. I’ve only used it so far for some entity extraction, but it looks like it has some very powerful natural language processing capabilities.
- Paul Shapiro’s Free Tools & Resources: My first go with Python was to run one of Paul’s old scripts (it has since been removed) testing different text summarizing libraries to help with meta descriptions. He has several other Python scripts still, including one for HREFlang sitemap generation.
Happy coding!