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 same Python program on another new machine, we may need to install all those library packages again. This will waste time and even make error. But PIP has provide a method for us to make batch install same Python packages in different machine 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 has been installed on current machine. Open a terminal and run pip freeze command to do it.
    C:\Users\zhaosong>pip freeze
  2. From above output, we can see that two packages has been installed in current Windows OS machine.
  3. Create a text file to save above packages list in it. The text file name is requirements.txt.

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

PIP install command provide a -r argument, we can provide above requirements.txt file path as the value to the -r argument, then it will install all packages written in the requirements.txt file. Now we will do this in a new virtual Python environment use Python virtualenv module. To learn more about Python virtualenv, you can read article How To Install Python Django In Virtual Environment.

  1. Install Python virtualven library use pip.
    C:\Users\zhaosong>pip install --user virtualenv
    Collecting virtualenv
      Downloading (2.0MB)
        100% |████████████████████████████████| 2.0MB 59kB/s
    Installing collected packages: virtualenv
    Successfully installed virtualenv-16.4.0
  2. Add a virtual Python environment in current folder. After run below command successfully, there will add a folder my_env in current folder.
    C:\Users\zhaosong>python -m venv my_env
  3. 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 my_env virtual environment.
  4. Run pip freeze again in the virtual environment, you can see that no Python packages has been installed in it.
    (my_env) C:\Users\zhaosong\my_env\Scripts>pip freeze
  5. 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
    Collecting pytz==2018.9 (from -r C:\WorkSpace\requirements.txt (line 2))
      Using cached
    Installing collected packages: pytz, Django
    Successfully installed Django-2.1.5 pytz-2018.9
  6. Run pip freeze again in my_env, now it will list the two Python packages installed.
    (my_env) C:\Users\zhaosong\my_env\Scripts>pip freeze
  7. Run deactivate command to quit the Python virtual environment.
    (my_env) C:\Users\zhaosong\my_env\Scripts>deactivate
0 0 votes
Article Rating
Notify of

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

Inline Feedbacks
View all comments
Would love your thoughts, please comment.x