7

Right now my requirement.txt contains following package list:

asgiref==3.2.3
beautifulsoup4==4.8.2
certifi==2019.11.28
chardet==3.0.4
Click==7.0
Django==3.0.2
idna==2.8
pytz==2019.3
requests==2.22.0
six==1.14.0
soupsieve==1.9.5
sqlparse==0.3.0
urllib3==1.25.7

I just want the package name only, so that pip3 always installs the latest version.

Hafnernuss
  • 2,659
  • 2
  • 29
  • 41
Devang Hingu
  • 578
  • 1
  • 6
  • 20
  • 5
    Version locking your dependencies is good. You'll still be able to `pip3 install` to install these packages. – Jaskaran Singh Jan 22 '20 at 07:17
  • Generally sometime when we are creating new project, we need to install latest packages(modules) so it good idea to keep all package names(`without version number`) only in `requirement.txt` and run it with pip and it will install all package with latest version. i added package name only in `requirement.txt` and it **worked**. – Devang Hingu Jan 22 '20 at 12:30
  • 2
    Updating packages to latest version can introduces breaking changes to your application and break you application. There can be changes like removing the API or changing the API you are using. Always read the changelogs before updating the packages. – Jaskaran Singh Jan 22 '20 at 12:44

6 Answers6

8

you can parse the requirements.txt file with python itself and get your package names and split it manually like below:

with open("requirements.txt") as myFile:
  pkgs = myFile.read()
  pkgs = pkgs.splitlines()

  for pkg in pkgs:
      print(pkg.split('==')[0])

this will give you something like below:

enter image description here

amdev
  • 6,703
  • 6
  • 42
  • 64
3

Consider if you had written your app in Django 2.x a few months ago and just had Django as dependency listed in your requirements.txt. Now that Django 3 is out, installing it now would cause a major version jump from Django 2 to 3 and potentially break half your app. And that’s just one of your dependencies.

No, versions are fixed on purpose. Upgrading versions should be a conscious effort involving testing to confirm compatibility. You want to be running software in a known good state, not in a random unpredictable state. Version incompatibilities are not to be underestimated. pip can give you a list of all outdated dependencies easily, and upgrading them is just another command.

Anecdote time: openpyxl contained a bug in version 3.0.1 recently which broke an important feature in our app. Do you know why we weren't affected by it? Because our requirements.txt uses fixed versions, and I found the issue by running our unit tests while upgrading dependencies manually, and choosing to not upgrade openpyxl because of it. Without fixed versions, our app would have been broken for a few weeks while openpyxl 3.0.1 was the most current version.

deceze
  • 510,633
  • 85
  • 743
  • 889
  • you're right. but when we setup new project we need some default module installed automatically with latest version so it good idea to install directly without mentioning its version. just only package name. now i checked added package name only in file and now it worked. – Devang Hingu Jan 22 '20 at 12:17
  • 1
    When you start a project, you should create a new virtual environment, run `pip install django` to install whatever version is current of your desired package, and then create your `requirements.txt` file from that with `pip freeze > requirements.txt`. You don't need a version-less `requirements.txt` to start this process. – deceze Jan 22 '20 at 12:26
  • i know this question meaningless but it just my information purpose.... Is it cause any problem if i install `**requirement.txt**` with version-less in newly created environment when we are creating new project? – Devang Hingu Jan 22 '20 at 12:39
  • Again, why do you have a `requirements.txt` *before* you have a project? If you're keeping one around as a sort of template because you often need all the same packages for many different projects, the same consideration about compatibility is important: you can't guarantee all the packages will work together without problems, so you should stick to versions which you know work together. – deceze Jan 22 '20 at 12:45
  • Ok. now i got logic. you're right. `Requirement.txt` meaningless before project. some smartness make fool sometime. thanks aLot man. – Devang Hingu Jan 22 '20 at 12:52
2

Use sed to remove version info from your requirements.txt file. e.g.

sed 's/==.*$//' requirements.txt

will give output

asgiref
beautifulsoup4
certifi
chardet
Click
Django
idna
pytz
requests
six
soupsieve
sqlparse
urllib3

and this can be piped into pip to do the install

sed 's/==.*$//' requirements.txt | xargs pip install
bobbobbins
  • 21
  • 1
1

You can also look at this question. From this question I took one of the answers which I think can solve your problem. It will not remove the package version but whenever you will install the requiremwnts.txt it will upgrade your packages to the latest versions.

pip install pip-upgrader

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:

pip-upgrade

If the requirements are placed in a non-standard location, send them as arguments:

pip-upgrade path/to/requirements.txt

If you already know what package you want to upgrade, simply send them as arguments:

pip-upgrade -p django -p celery -p dateutil

Mohit Chandel
  • 1,838
  • 10
  • 31
1

change your requirements.txt to this (without specifying the versions, so that pip will install latest versions)

asgiref
beautifulsoup4
certifi
chardet
Click
Django
idna
pytz
requests
six
soupsieve
sqlparse
urllib3
Firenze
  • 365
  • 2
  • 11
1

You need to change the requirement.txt file to remove all the version dependencies. you can parse the file for that. Or use regex.

==\w+.+.+

This will select all the element after symbol == including the symbol.

Replace with null empty string.

Open the requirements.txt in some editor that support regex (for example vs code).

Then use find and replace. (ctrl + f in vs code)

choose regex option. (click on .* in find and replace context menu in vs code)

in find put ==\w+.+.+ in replace put nothing. (keep it empty)

then replace all.

Then pip install requirements.txt and you are good to go.

fanbyprinciple
  • 554
  • 7
  • 14