We are now going to learn the basic commands for using Python package manager. To install a package with pip, enter the following code:
$ pip install [package-name]
On Mac and Linux, because you are installing programs outside of the user-owned folders, you might have to prepend sudo to the install commands. To install Flask, simply run the following:
$ pip install flask
Once you have done this, all of the requirements that you need for using Flask will be installed for you.
If you want to remove a package that you are no longer using, run the following:
$ pip uninstall [package-name]
If you wish to explore or find a package, but don't know its exact name, you can 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 that others can quickly install every necessary package. 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 the following command:
$ pip freeze > requirements.txt
What exactly did this command do? The pip freeze command automatically prints out a list of the installed packages and their versions. For our example, it prints the following:
click==6.7
Flask==0.12.4
itsdangerous==0.24
Jinja2==2.10
MarkupSafe==1.0
Werkzeug==0.14.1
The > operator tells Bash to take everything printed by the last command and write it to this file. If you look in your project directory, you can see a new file named requirements.txt that contains the output of pip freeze.
To install all the packages from this file, a new project maintainer would have to run this, as shown in the following code. Normally, this will also be used to deploy the production environment of your project:
$ pip install -r requirements.txt
The preceding code tells pip to read all the packages listed in requirements.txt and install them.