Installing Python and Python Packages
So far, you've learned to write Python using Codecademy's learning environment. In this article, you'll learn how to install and run Python code on your computer and use pip to manage Python packages.
The Codecademy environment allows you to enter Python code and receive feedback on whether or not the code you entered is correct for a given exercise. In this article, we’ll walk you through how to install Python for object-oriented programming so that you can write and run Python code on your computer!
If your needs are different and you want to use Python to analyze large sets of data, we recommend the following Codecademy resource:
Why build outside of Codecademy?
The programming world is massive, and it’s impossible to teach everything in one place. Although Codecademy excels at teaching you how to code via interactive lessons, we’d also like for you to learn how to code on your computer so that you can create personal projects (and perhaps share them with the world)!
In this article, we’ll cover the following topics:
- Which Version of Python Should I Install?
- Do I Need to Install Python?
- Installing Python
- Running Python Code
- Installing and Managing Python Packages Using
Which Version of Python Should I Install?
Today, the debate rages on over which version of Python to use. Version 2.7, released in 2010, is perhaps the most widely used of all Python versions. Version 2.7, however, is not the most recent. In 2008, Python 3.0 — often stylized as Python “3.x” to represent all incremental updates to 3.0 — was released. As of this writing, the most recent version is Python 3.6.
There are some fundamental differences between Python 2.x and Python 3.x. You can read about these differences at the following resource:
At Codecademy, you’ll learn Python 2.7. The rest of this article will discuss how to install Python version 2.7.
Do I Need to Install Python?
You may not need to install Python at all. Some computer operating systems have Python version 2.7 installed “out of the box” (meaning, pre-installed). You can check whether Python by opening the command line terminal on your computer. Once the terminal is open, enter the following command and press “Enter” (or “Return”) on your keyboard:
If the Python interpreter responds, a version number for Python will also appear. If the version is 2.7 (or 2.7.x), then you’re in luck! Otherwise, you’ll have to install version 2.7.
If you have a version other than 2.7, you should uninstall it and install 2.7.
To install Python, follow these steps:
Navigate to the Python downloads page: Python downloads.
Click on the link/button to download Python 2.7.x.
Follow the installation instructions (leave all defaults as-is).
Open your terminal again and type the command
cd. Next, type the command
python. The Python interpreter should respond with the version number. If you’re on a Windows machine, you will likely have to navigate to the folder where Python is installed (for example,
Python27, which is the default) for the
pythoncommand to function.
Running Python Code
Congrats! You should have Python 2.7 installed now. Let’s run some Python code!
- Open your terminal and type
cd(if you’re using Windows, navigate to the
- Create a file called
mycode.py(make sure it has a
program.pyusing your favorite text editor.
- Add the following code to the file and save it:
print "I'm running Python code on my own!"
5. In your terminal, type the following command and press “Enter” (or “Return”) on your keyboard to run your code (again, if you’re on Windows you will need to navigate to the folder you installed Python 2.7 in):
6. Your terminal should output the following message:
I'm running Python code on my own!
Congratulations! You just ran Python code on your computer!
Installing and Managing Python Packages Using
With Python, you can build just about anything, from simple scripts to full applications. The Python language, however, doesn’t come pre-installed with all of the fancy features you might want (or require). When you need particular functionality, you can look toward Python packages. A package structures Python modules, which contain pre-written code that other developers have created for you. Modules are handy when you are looking for specific functionality.
You can use
pip, Python’s package manager, to install and manage Python packages.
If your Python 2 version is greater than or equal to 2.7.9, no need to worry, you have
You can use
pip to install packages, like so:
pip install scrapy
In the example above,
pip will install the Scrapy package, a popular package (among many) used for scraping information from websites.
There are a multitude of Python packages, which you can find on PyPI — the Python Package Index — the official repository for third-party software for the Python programming language. PyPI is where
pip grabs Python packages from when you use
pip to install a new package on your computer.
You can use
pip for a variety of other things as well, which you can learn about through a quick search on the web.
So far, you’ve been writing Python code on Codecademy. Your learning journey, however, is not complete unless you can also write Python code outside of Codecademy, on your computer. At Codecademy, we recommend Python version 2.7 and managing your packages using
pip, Python’s package manager. Have fun coding!