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
    Django==2.1.5
    pytz==2018.9
  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.
    Django==2.1.5
    pytz==2018.9

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 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
  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.
    C:\Users\zhaosong\my_env\Scripts>activate.bat
  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 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
  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
    Django==2.1.5
    pytz==2018.9
  7. Run deactivate command to quit the Python virtual environment.
    (my_env) C:\Users\zhaosong\my_env\Scripts>deactivate
    C:\Users\zhaosong\my_env\Scripts>
READ :   Python 3 Unittest Html And Xml Report Example

Leave a Reply

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.