I already answered this question here. Here's my solution:
Because there was no easy way for upgrading package by package, and updating the requirements.txt file, I wrote this pip-upgrader which also updates the versions in your requirements.txt file for the packages chosen (or all packages).
Installation
Copypip install pip-upgrader
Usage
Activate your virtualenv (important, because it will also install the new versions of upgraded packages in current virtualenv).
cd into your project directory, then run:
Copypip-upgrade
Advanced usage
If the requirements are placed in a non-standard location, send them as arguments:
Copypip-upgrade path/to/requirements.txt
If you already know what package you want to upgrade, simply send them as arguments:
Copypip-upgrade -p django -p celery -p dateutil
If you need to upgrade to pre-release / post-release version, add --prerelease argument to your command.
Full disclosure: I wrote this package.
Answer from Simion Agavriloaei on Stack OverflowI already answered this question here. Here's my solution:
Because there was no easy way for upgrading package by package, and updating the requirements.txt file, I wrote this pip-upgrader which also updates the versions in your requirements.txt file for the packages chosen (or all packages).
Installation
Copypip install pip-upgrader
Usage
Activate your virtualenv (important, because it will also install the new versions of upgraded packages in current virtualenv).
cd into your project directory, then run:
Copypip-upgrade
Advanced usage
If the requirements are placed in a non-standard location, send them as arguments:
Copypip-upgrade path/to/requirements.txt
If you already know what package you want to upgrade, simply send them as arguments:
Copypip-upgrade -p django -p celery -p dateutil
If you need to upgrade to pre-release / post-release version, add --prerelease argument to your command.
Full disclosure: I wrote this package.
you can try:
Copypip install --upgrade --force-reinstall -r requirements.txt
You can also ignore installed package and install the new one :
Copypip install --ignore-installed -r requirements.txt
How to upgrade all Python packages with pip - Stack Overflow
how do you upgrade all outdated pip packages?
python - How can I upgrade specific packages using pip and a requirements file? - Stack Overflow
How to keep your requirements.txt updated
Videos
Sent through a pull-request to the pip folks; in the meantime use this pip library solution I wrote:
Copyfrom operator import attrgetter
## Old solution:
# from pip import get_installed_distributions
# from pip.commands import install
## New solution:
from pkg_resources import working_set
from pip._internal.commands import install
install_cmd = install.InstallCommand()
options, args = install_cmd.parse_args(
## Old solution:
# list(map(attrgetter("project_name")
# get_installed_distributions()))
## New solution:
list(map(attrgetter("project_name"), working_set))
)
options.upgrade = True
install_cmd.run(options, args) # Chuck this in a try/except and print as wanted
This seemed to work for me...
Copypip install -U $(pip list --outdated | awk '{printf $1" "}')
I used printf with a space afterwards to properly separate the package names.
pip freeze -o > requirements.txt
output:
Usage: pip freeze [options] no such option: -o
i want to convert all the outdated pip packages to a requirements format and contain it into requirements.txt.
I ran the following command and it upgraded from 1.2.3 to 1.4.0
Copypip install Django --upgrade
Shortcut for upgrade:
Copypip install Django -U
Note: if the package you are upgrading has any requirements this command will additionally upgrade all the requirements to the latest versions available. In recent versions of pip, you can prevent this behavior by specifying --upgrade-strategy only-if-needed. With that flag, dependencies will not be upgraded unless the installed versions of the dependent packages no longer satisfy the requirements of the upgraded package.
According to pip documentation example 3:
Copypip install --upgrade django
But based on my experience, using this method will also upgrade any package related to it. Example:
Assume you want to upgrade somepackage that require Django >= 1.2.4 using this kind of method it will also upgrade somepackage and django to the newest update. Just to be safe, do:
Copy# Assume you want to keep Django 1.2.4
pip install --upgrade somepackage django==1.2.4
Doing this will upgrade somepackage and keeping Django to the 1.2.4 version.
I did some changes on my local machine and updated requirements.txt. Then I pulled to my remote machine with git and did pip install -r requirements.txt, because as far as I understand this is supposed to only install new things.. right? What I want is to upgrade what has been upgraded and install what isn't already there. All my dependencies versions are specified in requirements.txt
But for every dependency, as far as I could see, this happened - for example for Jinja2==3.0.1:
Collecting Jinja2==3.0.1 Using cached Jinja2-3.0.1-py3-none-any.whl (133 kB) INFO: This is taking longer than usual. You might need to provide the dependency resolver with stricter constraints to reduce runtime. If you want to abort this run, you can press Ctrl + C to do so. To improve how pip performs, tell us what happened here: https://pip.pypa.io/ surveys/backtracking INFO: pip is looking at multiple versions of markupsafe to determine which version is compatible with other requirements. This could take a while. INFO: pip is looking at multiple versions of itsdangerous to determine which version is compatible with other requirements. This could tak e a while.
And it could stand like that for an hour or more. I went to bed at when I woke up, Pythonanywhere had put me in the "tar pit" because I was consuming too much CPU. Fair enough. But what am I doing wrong? What's with the 'using cached' and 'looking at multiple versions' - is this what is taking so long? Eventually I stopped the process.
Noob warning, sorry if I missed something obvious.
My two requirements.txt if that helps
My current requirements.txt:
alembic==1.6.5 appdirs==1.4.4 APScheduler==3.7.0 astroid==2.3.3 autopep8==1.5 bcrypt==3.2.0 blinker==1.4 cachelib==0.1.1 cairocffi==1.2.0 CairoSVG==2.5.2 certifi==2020.4.5.1 cffi==1.14.4 chardet==3.0.4 click==8.0.1 cryptography==3.3.1 cssselect2==0.4.1 defusedxml==0.6.0 distlib==0.3.1 dnspython==1.16.0 email-validator==1.1.0 entrypoints==0.3 env==0.1.0 filelock==3.0.12 flake8==3.7.9 Flask==2.0.1 Flask-DebugToolbar==0.11.0 Flask-Login==0.5.0 Flask-Mail==0.9.1 Flask-Migrate==3.0.1 Flask-Session==0.3.2 Flask-SQLAlchemy==2.4.1 Flask-User==1.0.2.2 Flask-WeasyPrint==0.6 Flask-WTF==0.14.3 html5lib==1.1 idna==2.9 importlib-metadata==1.7.0 ipapy==0.0.9.0 isort==4.3.21 itsdangerous==2.0.1 Jinja2==3.0.1 lazy-object-proxy==1.4.3 Mako==1.1.4 MarkupSafe==2.0.1 mccabe==0.6.1 passlib==1.7.4 Pillow==8.2.0 pipdeptree==1.0.0 pycodestyle==2.5.0 pycparser==2.20 pyflakes==2.1.1 pylint==2.4.4 pyparsing==2.4.6 Pyphen==0.10.0 python-dateutil==2.8.1 python-editor==1.0.4 pytz==2019.3 PyYAML==5.4.1 requests==2.23.0 scss==0.8.73 sentry-sdk==1.1.0 six==1.13.0 SQLAlchemy==1.3.16 tinycss2==1.1.0 tzlocal==2.1 ua-parser==0.10.0 urllib3==1.26.5 user-agents==2.2.0 WeasyPrint==52.1 webencodings==0.5.1 Werkzeug==2.0.1 wrapt==1.11.2 WTForms==2.3.1 zipp==3.1.0
My previous requirements.txt
appdirs==1.4.4 astroid==2.3.3 autopep8==1.5 bcrypt==3.2.0 blinker==1.4 cairocffi==1.2.0 CairoSVG==2.5.0 certifi==2020.4.5.1 cffi==1.14.4 chardet==3.0.4 Click==7.0 cryptography==3.3.2 cssselect2==0.4.1 defusedxml==0.6.0 distlib==0.3.1 dnspython==1.16.0 email-validator==1.1.0 entrypoints==0.3 env==0.1.0 filelock==3.0.12 flake8==3.7.9 Flask==1.1.1 Flask-DebugToolbar==0.11.0 Flask-Login==0.5.0 Flask-Mail==0.9.1 Flask-Session==0.3.1 Flask-SQLAlchemy==2.4.1 Flask-User==1.0.2.2 Flask-WeasyPrint==0.6 Flask-WTF==0.14.3 html5lib==1.1 idna==2.9 importlib-metadata==1.7.0 ipapy==0.0.9.0 isort==4.3.21 itsdangerous==1.1.0 Jinja2==2.11.3 lazy-object-proxy==1.4.3 MarkupSafe==1.1.1 mccabe==0.6.1 passlib==1.7.4 Pillow==8.1.2 pipdeptree==1.0.0 pycodestyle==2.5.0 pycparser==2.20 pyflakes==2.1.1 PyJWT==2.0.1 pylint==2.4.4 pyparsing==2.4.6 Pyphen==0.10.0 pytz==2019.3 requests==2.23.0 scss==0.8.73 six==1.13.0 SQLAlchemy==1.3.16 tinycss2==1.1.0 urllib3==1.25.8 WeasyPrint==52.1 webencodings==0.5.1 Werkzeug==0.16.0 wrapt==1.11.2 WTForms==2.3.1 zipp==3.1.0