Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Mastering Flask

You're reading from   Mastering Flask Gain expertise in Flask to create dynamic and powerful web applications

Arrow left icon
Product type Paperback
Published in Sep 2015
Publisher Packt
ISBN-13 9781784393656
Length 288 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Jack Stouffer Jack Stouffer
Author Profile Icon Jack Stouffer
Jack Stouffer
Arrow right icon
View More author details
Toc

Table of Contents (15) Chapters Close

Preface 1. Getting Started 2. Creating Models with SQLAlchemy FREE CHAPTER 3. Creating Views with Templates 4. Creating Controllers with Blueprints 5. Advanced Application Structure 6. Securing Your App 7. Using NoSQL with Flask 8. Building RESTful APIs 9. Creating Asynchronous Tasks with Celery 10. Useful Flask Extensions 11. Building Your Own Extension 12. Testing Flask Apps 13. Deploying Flask Apps Index

Python package management with pip

In Python, programmers can download libraries from other programmers that extend the functionality of the standard Python library. As you already know from using Flask, a lot of Python's power comes from its large amount of community-created libraries.

However, installing third-party libraries can be a huge pain to do correctly. Say there is a package X that you wish to install. Simple enough, download the Zip file and run setup.py, right? Not quite. Package X relies on package Y, which in turn relies on Z and Q. None of this information was listed on package X's website, but they are required to be installed for X to work at all. You then have to find all of the packages one by one and install them, in the hope that the packages you are installing don't require any extra packages themselves.

In order to automate this process, we use pip, the Python package manager.

Installing the pip Python package manager on Windows

If you are on Windows, and your installed Python the current version, you already have pip! If your Python installation is not the most recent, the easiest thing to do is to simply reinstall it. Download the Python Windows installer at https://www.python.org/downloads/.

In Windows, the variable that controls which programs are accessible from the command line is path. To modify your path to include Python and pip, we have to add C:\Python27 and C:\Python27\Tools. Edit the Windows path by opening the Windows menu, right-clicking on Computer and clicking on Properties. Under Advanced system settings, click Environment Variables.... Scroll down until you find Path, double-click it, and add ;C:\Python27;C:\Python27\Tools to the end.

To make sure you have modified your path correctly, close and reopen your terminal and type the following into the command line:

pip --help

Tip

Downloading the example code

You can download the example code files from your account at http://www.packtpub.com for all the Packt Publishing books you have purchased. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

pip should have printed its usage message as shown in the following screenshot:

Installing the pip Python package manager on Windows

Installing the pip Python package manager on Mac OS X and Linux

Some Python installations of Linux do not come with pip, and Mac OS X installations don't come with pip by default. To install it, download the get-pip.py file from https://raw.githubusercontent.com/pypa/pip/master/contrib/get-pip.py.

Once you have downloaded it, run it with elevated privileges using the following:

$ sudo python get-pip.py

Then pip will be installed automatically.

pip basics

To install a package with pip, follow this simple step:

$ pip install [package-name]

On Mac and Linux, because you are installing programs outside the user-owned folders, you might have to prepend sudo to the install commands. To install Flask, simply run this:

$ pip install flask

Then, all requirements of Flask will be installed for you.

If you want to remove a package that you are no longer using, run this:

$ pip uninstall [package-name]

If you wish to explore or find a package but don't know its exact name, you may use the search command:

$ pip search [search-term]

Now that we have a couple of packages installed, it is common courtesy in the Python community to create a list of packages that are required to run the project, so others can quickly install every thing required. This also has the added benefit that any new member of your project will be able to run your code quickly.

This list can be created with pip by running this:

$ pip freeze > requirements.txt

What exactly did this command do? pip freeze run by itself prints out a list of the installed packages and their versions as follows:

Flask==0.10.1
itsdangerous==0.24
Jinja2==2.7.3
MarkupSafe==0.23
Werkzeug==0.10.4
wheel==0.24.0

The > operator tells Bash to take everything printed by the last command and write it to this file. If you look into your project directory, you will see the new file named requirements.txt that contains the output of pip freeze.

To install all the packages from this file, a new project maintainer will have to run this:

$ pip install -r requirements.txt

This tells pip to read all the packages listed in requirements.txt and install them.

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime