Installing packages with the requirements.txt file to work with PostgreSQL

Make sure you quit Django development server. You just need to press Ctrl + C in the Terminal or Command Prompt window in which it is running.

Now we will install an additional package. Make sure you have activated the virtual environment we have created in the previous chapter and we named Django01. After you activate the virtual environment, it is time to run many commands, which will be the same for either macOS, Linux, or Windows.

Now we will edit the existing requirements.txt file to specify the additional package that our application requires to be installed on any supported platform. This way, it will be extremely easy to repeat the installation of the specified packages with their versions in any new virtual environment.

Use your favorite editor to edit the existing text file named requirements.txt within the root folder for the virtual environment. Add the following lines after the last line to declare the additional package and the version that our new version of the API requires: psycopg2 version 2.7.5. The code file for the sample is included in the restful_python_2_06_01 folder, in the Django01/requirements.txt file:

psycopg2==2.7.5 

Psycopg 2 (psycopg2) is a Python-PostgreSQL database adapter and Django's integrated ORM will use it to interact with our recently created PostgreSQL database. Again, it is very important to make sure that the PostgreSQL bin folder is included in the PATH environmental variable before we run the installation for this package.

Now we must run the following command on macOS, Linux, or Windows to install the additional packages and the versions explained in the previous table with pip by using the recently edited requirements.txt file. Make sure you are in the folder that has the requirements.txt file (Django01) before running the command:

pip install -r requirements.txt 

The last lines for the output will indicate the new package has been successfully installed. If you downloaded the source code for the example and you didn't work with the previous version of the API, pip will also install the other packages included in the requirements.txt file:

    Installing collected packages: psycopg2
    Successfully installed psycopg2-2.7.5
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.218.48.62