PIP(PIP Installs Packages) is Python’s recommended tool for package managing. Most modern operating systems come with Python and PIP installed. You can check if Python and PIP are installed using the which command:
1 2 which pip which python
If PIP is not installed you can follow the documentation to install it.
By default PIP will look for packages in Python’s Package Index. You can search for packages from the command line:
1 pip search flask
Installing them is also straight forward:
1 sudo pip install flask
Depending on your configuration you might not need to use sudo. Try not using it first and if it doesn’t work then add sudo.
You can see detailed information about what was installed by using pip show:
1 pip show --files flask
If you are curious about what packages are currently installed on your system you can use pip list:
1 pip list
Finally, if flask bores you, you can uninstall it:
1 sudo pip uninstall flask
If you have a project with multiple dependencies it would be inefficient to run pip install for each of the dependencies. For this reason PIP allows you to specify dependencies in a file called requirements.txt. In this file you can specify a list of dependencies as you would pass them to pip install. Here is an example of a very simple requirements.txt file:
1 2 3 # This is a comment flask flask-redistore
To install the dependencies from the file:
1 pip install -r requirements.txt
There are two things you should keep in mind to make the use of requirements.txt safer:
1 – All dependencies specify a specific version number.
2 – Use –no-deps when running pip install. This way you are sure that you explicitly specified all your dependencies.
You can have a requirements.txt file automatically generated for you by using pip freeze:
1 pip freeze > requirements.txt
This will generate a file called requirements.txt with all the packages and versions currently installed on your system. This is a safe way to reproduce the current environment but might include packages you don’t really need for your application.
A constraints file allows you to specify a version of a package you want to install, but it does not install it. This is useful when for some reason you need to fork a package and you want to use your fork every time you depend on the package. To read the constraints file just do:
1 pip install -c constraints.txt
PIP has some advanced functionality that allow you to solve dependency conflicts that you might find in the wild. Hopefully you will never need the advanced functionality, but if you do, be relieved that the solution might be in the documentation.