QBoard » Artificial Intelligence & ML » AI and ML - Python » How to upgrade all Python packages with pip

How to upgrade all Python packages with pip

  • Is it possible to upgrade all Python packages at one time with pip?
    Note: that there is a feature request for this on the official issue tracker.
      December 3, 2021 10:37 AM IST
    0
  • You can use the following Python code. Unlike pip freeze, this will not print warnings and FIXME errors. For pip < 10.0.1
    import pip
    from subprocess import call
    
    packages = [dist.project_name for dist in pip.get_installed_distributions()]
    call("pip install --upgrade " + ' '.join(packages), shell=True)

     

    For pip >= 10.0.1

    import pkg_resources
    from subprocess import call
    
    packages = [dist.project_name for dist in pkg_resources.working_set]
    call("pip install --upgrade " + ' '.join(packages), shell=True)

     

      December 4, 2021 1:08 PM IST
    0
  • With Python, the best practice of pinning all the packages in an environment at a specific version ensures that the environment can be reproduced months or even years later. 

    • Pinned packages in a requirements.txt file are denoted by ==. For example,  requests==2.21.0. Pinned packages should never be updated except for a very good reason, such as to fix a critical bug or vulnerability.
    • Conversely, unpinned packages are typically denoted by >=, which indicates that the package can be replaced by a later version. Unpinned packages are more common in development environments, where the latest version can offer bug fixes, security patches and even new functionality.

    As packages age, many of them are likely to have vulnerabilities and bugs logged against them. In order to maintain the security and performance of your application, you’ll need to update these packages to a newer version that fixes the issue. 

    The pip package manager can be used to update one or more packages system-wide. However, if your deployment is located in a virtual environment, you should use the Pipenv package manager to update all Python packages. 

      December 8, 2021 10:21 AM IST
    0
  • Update All Python Packages On Windows

    The easiest way to update all packages in a Windows environment is to use pip in conjunction with Windows PowerShell: 

    1. Open a command shell by typing ‘powershell’ in the Search Box of the Task bar
    2. Enter:

       

      pip freeze | %{$_.split('==')[0]} | %{pip install --upgrade $_}
      
      This will upgrade all packages system-wide to the latest version available in the Python Package Index (PyPI).
      December 9, 2021 12:54 PM IST
    0
  • The following works on Windows and should be good for others too ($ is whatever directory you're in, in the command prompt. For example, C:/Users/Username).

    Do

    $ pip freeze > requirements.txt
    

     

    Open the text file, replace the == with >=, or have sed do it for you:

    $ sed -i 's/==/>=/g' requirements.txt
    

     

    and execute:

    $ pip install -r requirements.txt --upgrade
    

     

    If you have a problem with a certain package stalling the upgrade (NumPy sometimes), just go to the directory ($), comment out the name (add a # before it) and run the upgrade again. You can later uncomment that section back. This is also great for copying Python global environments.

    Another way:

    I also like the pip-review method:

    py2
    $ pip install pip-review
    
    $ pip-review --local --interactive
    
    py3
    $ pip3 install pip-review
    
    $ py -3 -m pip-review --local --interactive

     

    You can select 'a' to upgrade all packages; if one upgrade fails, run it again and it continues at the next one.

     
      December 10, 2021 10:57 AM IST
    0