Set-up Python 2.7
Get Python setup on your own computer
Article Note: This article gives instructions for how to set up Python 2.7, which is now outdated and no longer used. However in order to match up with the Codecademy course on Python 2, we are leaving this article up as legacy instructions. Much of this guide is still relevant if you go to install a version 3 in the future, and as of this writing Python 2.7 is still available for installation.
The Codecademy site provides you with a development environment already set up for you to complete exercises and run your code. Now that you’ve learned how to code in Python, let’s go through the process of setting up your development environment on your computer so that you can write your own applications.
While you took the Python Codecademy course, you’ve been submitting code to us which we have interpreted, run, and then sent the results back to you. This article will walk you through how to set up Python and execute code on your own computer. Sounds intimidating, but there are some nice tools to help you out that we’ll introduce you to. You’ll be flying in no time.
Why build outside of Codecademy? The world of programming is massive, and it’s impossible to teach everything in one place. While Codecademy is excellent for teaching lessons, it also has limitations. For instance, it’s difficult to create large projects and share them with the world using only the tools found in Codecademy’s Python course. This guide will teach you:
- How to download and install python
- How to run python from the command line
- How to install python libraries using pip, a popular package manager for python (we’ll explain what that means!)
When you learned using the Codecademy course, you wrote the solutions in your web browser and submitted the data to us so you did not need to have Python installed on your computer. Now, you’re going to write and run actual programs on your computer.
Visit the official Python downloads page and find the most recent release of Python 2.7 that corresponds to your OS (operating system).
Make sure to download Python 2.7 and not Python 3.x. Although Python 3 has a higher version number, it isn’t completely compatible with Python 2, the version you learned in the Codecademy course.
After your download has completed, launch the installer. This may require that you unzip the file first depending on the version of the installer you downloaded. Follow the instructions provided by the installer to complete your Python installation.
What did I just install?
Computers read and execute code based on machine language which is stored in hexadecimal format. It is virtually impossible for a human to write in machine language and each processor has its own version.
To make programming simpler, human-readable languages like Python were invented. The files you just installed include a Python interpreter. This interpreter converts your human-readable Python code into instructions that the computer can act on.
Once you have downloaded Python, you should be able to pull up your computer’s terminal and start running it. On Windows, search for a program called “cmd” and then launch it If you’re on a Mac or a Linux environment look for and launch the program “terminal.” You should have a command-line prompt that looks similar to this:
If you downloaded Python properly, you can just type
python into the prompt and hit enter to get a result like the below:
From here we can type in Python code and watch it be interpreted for us on the fly. Go ahead and type some simple stuff in and watch Python work right in front of you:
The command line is useful for checking simple code, but if you’re looking to build something more involved, you’ll benefit by starting with a text editor. Let’s exit out. We’ll make our own files and return to the command line to run those.
If you do not have a good text editor for editing code, we recommend Sublime Text, for which we have a step-by-step setup guide here. Any text editor will work but may require some additional setup.
Once you’re set-up, write a simple piece of Python code like the below in your text editor. Save it, making sure the file name ends with a .py extension. We’ll name our file first.py for this example.
Now let’s run your code and see what we get. Pull up your terminal again and locate the directory in which you saved your python file. Use the cd command to get there. (If you’re rusty on how to locate and change directories, refer to our Learn the Command Line course). Then type
python first.py. Your terminal window should look something like this:
The output of your script should print in your terminal window. If you’re seeing errors or nothing at all, make sure you’re in the same directory as your code, and that your code has a print statement that gets executed. (In other words, that you’re telling your code to actually print something.)
If it printed, congratulations! You’ve now written and run a Python file all on your own! Now you’re equipped to create those projects that you’ve dreamed of.
Making big projects takes lots of work. Lots and lots of work, so much so that it’s useful to have a little help from others. Thanks to the very open culture of programming, there are many open source libraries out there for you to use. Using libraries help us work quickly, and allow us not to reinvent the wheel every time we sit down to code.
Using PIP to get and install Packages:
As you begin to build your own applications with Python, you’ll likely use a lot of packages. The best way to install and manage them is with a package manager.The most popular and most recommended package manager for Python is called pip.
Pip should already be included with your installation of Python. To check, just type
pip into your command line window and see what pops up. If you get something like below, then you have pip installed. If you get an error, install the latest version of pip here.
How does pip work?
To download and install packages, pip searches through PyPi, the Python Package Index, a large repository of registered open source python packages.
Let’s try installing a package called scrapy. It’s used to gather data from websites by “scraping” information from them. More info on Scrapy can be found on their official site. To install Scrapy, go to your terminal and enter
pip install scrapy. In my terminal below, I also used “pip list” to show all my installed packages before I installed scrapy and then after it was installed to verify that I successfully installed it.
From now on, we can use scrapy in any of our Python applications. In order to use it, type “import scrapy” in the python file that makes calls to it.
If you decide you want to uninstall scrapy, just use the command:
pip uninstall scrapy.
pip list again to make sure you uninstalled properly.
Pro Tip: If you’re working on something and wondering if there’s an easier way to do something, there’s probably already an open sourced a package that accomplishes the task. You can use a search engine or search through PyPi itself to find what you want. Can’t find what you want? Consider writing your own package and releasing it as open source so others can use it.
Retrieve Information from the Web:
There are many different packages that can fetch data from the web including scrapy. Let’s go through an example using one of Python’s built-in packages called urllib2
Since urllib2 is built into Python, we do not need to install it using pip. Let’s use it to grab data from Project Gutenberg, a massive collection of free eBooks. Follow the code shown below, save it and run it.
Woah! That’s a lot of HTML!
Let’s look at what we did.
First we requested the url at Project Gutenberg’s homepage which sent a response that we printed. If we had made the same request with a web browser, the browser would have interpreted the HTML for us and displayed a pretty picture. Instead, our application read the raw HTML file for us and printed the output. This may look messy, but it can be useful for looking through websites in order to get information from them.
Let’s say we wanted to count how many times an author uses a word in a book. Let’s use one of my favorites, Les Miserables by Victor Hugo. First, we find the url for the book on Project Gutenberg, open the url, read it, and then parse it to see how many times Hugo uses the word “sad.” The screenshots above use the base URL for Project Gutenberg. You should replace that URL with the correct URL for Les Miserables: http://www.gutenberg.org/files/135/135-h/135-h.htm.
How many times did you count the word
sad? Looks like this is a slightly depressing book! This is just one basic usage example of urllib2 and web scrapers in general, and is meant to give you a taste of what’s possible when you use Python locally.
If you’re interested, a more in-depth tutorial on urllib2 can be found in Python’s official documentation. You can refer to Python’s documentation to browse all the features of a particular package in the Python standard library.