How To Install Python Packages Using Requirements Text File

When we develop Python programs, we always need to install and use a lot of third-party library packages. But if we develop the same Python program on another new machine, we may need to install all those library packages again. This will waste time and even make errors. But PIP has provided a method for us to make batch install same Python packages in different machines easy and simple without errors, this article will tell you how to do it.

1. Collect Current Installed Python Packages.

  1. First, we need to collect all packages that have been installed on the current machine. Open a terminal and run pip freeze command to do it.
    C:\Users\zhaosong>pip freeze
    Django==2.1.5
    pytz==2018.9
  2. From the above output, we can see that two packages have been installed in the current Windows OS machine.
  3. Create a text file to save the above packages list in it. The text file name is “requirements.txt“.
    Django==2.1.5
    pytz==2018.9

2. Use PIP To Install Python Packages From Requirements.txt File.

  1. The pip install command provides a -r argument, we can provide the above “requirements.txt” file path as the value to the -r argument, then it will install all packages written in the requirements.txt file.
  2. Now we will do this in a new virtual Python environment using Python virtualenv module following the below steps. To learn more about Python virtualenv module, you can read the article How To Install Python Django In Virtual Environment.
  3. Install Python virtualven library using pip.
    C:\Users\zhaosong>pip install --user virtualenv
    Collecting virtualenv
      Downloading https://files.pythonhosted.org/packages/7e/1b/6c00d57127608793e16e8b7f813e64d58a1938505c42fe190d1386ab41e1/virtualenv-16.4.0-py2.py3-none-any.whl (2.0MB)
        100% |████████████████████████████████| 2.0MB 59kB/s
    Installing collected packages: virtualenv
    Successfully installed virtualenv-16.4.0
  4. Create a virtual Python environment in the current folder. After running the below command successfully, there will create a folder my_env in the current folder.
    C:\Users\zhaosong>python -m venv my_env
  5. Run my_env / Scripts / activate.bat file in Windows, or $ source my_env/bin/activate in Linux or macOS to activate the virtual Python environment. Now you will work in the my_env virtual environment.
    C:\Users\zhaosong\my_env\Scripts>activate.bat
  6. Run pip freeze again in the virtual environment, you can see that no Python packages have been installed in it.
    (my_env) C:\Users\zhaosong\my_env\Scripts>pip freeze
  7. Run pip install -r C:\WorkSpace\requirements.txt in the virtual environment to install all Python packages written in the requirements.txt file.
    (my_env) C:\Users\zhaosong\my_env\Scripts>pip install -r C:\WorkSpace\requirements.txt
    Collecting Django==2.1.5 (from -r C:\WorkSpace\requirements.txt (line 1))
      Using cached https://files.pythonhosted.org/packages/36/50/078a42b4e9bedb94efd3e0278c0eb71650ed9672cdc91bd5542953bec17f/Django-2.1.5-py3-none-any.whl
    Collecting pytz==2018.9 (from -r C:\WorkSpace\requirements.txt (line 2))
      Using cached https://files.pythonhosted.org/packages/61/28/1d3920e4d1d50b19bc5d24398a7cd85cc7b9a75a490570d5a30c57622d34/pytz-2018.9-py2.py3-none-any.whl
    Installing collected packages: pytz, Django
    Successfully installed Django-2.1.5 pytz-2018.9
  8. Run pip freeze again in my_env, now it will list the two Python packages that have been installed.
    (my_env) C:\Users\zhaosong\my_env\Scripts>pip freeze
    Django==2.1.5
    pytz==2018.9
  9. Run deactivate command to quit the Python virtual environment.
    (my_env) C:\Users\zhaosong\my_env\Scripts>deactivate
    C:\Users\zhaosong\my_env\Scripts>

3. Question & Answer.

3.1 How to use the pip install command to install all the requirements.txt file contained python libraries from the local directory instead of from the PyPI index.

  1. My requirements.txt file contains some python libraries, and I also have these python libraries packages files in a local folder. I just create a new and empty python virtual environment using the virtualenv module, after activating it, I run the command pip install -r /…/requirements.txt -f file://local-python-packages-stored-folder in a terminal. The output seems successful, but after the installation process is complete successfully, I find that the packages that I need can not be imported into my python source code also. And I can not find those python packages in my python virtual environment site-packages folder, how to fix it.
  2. The general reason for installing the required python module from the local directory is because there are some dependent libraries in the requiremets.txt file, if we do not provides these dependent libraries in the local folder, then the installation process may fail. So you should run the command like this $ pip install -r requirements.txt –no-index –find-links file://local-python-packages-stored-folder to resolve the error. The –no-index argument means pip will not findthe required python libraries fromthe PyPI. The –find-links argument means find the required python packages from the provided URL links ( a list of URL ), the links can be an Html URL list ( http://www.abc.com/archive.html ) or a local directory( file://local-python-packages-stored-folder ).

1 thought on “How To Install Python Packages Using Requirements Text File”

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.