Devang Hingu
Devang Hingu

Reputation: 628

How to create requirement.txt without all package versions

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.

Upvotes: 11

Views: 25467

Answers (8)

Muthumanickam V
Muthumanickam V

Reputation: 1

Pass no-pin in --mode option.

Example:

pipreqs --force --mode no-pin

Upvotes: 0

Charles Lee
Charles Lee

Reputation: 1

If you are using pip, You can generate requirements.txt like this:

pip list --format=freeze | cut -d'=' -f1 > requirements.txt

Upvotes: 0

bobbobbins
bobbobbins

Reputation: 106

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

Upvotes: 9

fanbyprinciple
fanbyprinciple

Reputation: 780

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.

Upvotes: 1

Firenze
Firenze

Reputation: 367

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

Upvotes: 2

Mohit Chandel
Mohit Chandel

Reputation: 1916

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

Upvotes: 1

deceze
deceze

Reputation: 522135

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.

Upvotes: 4

amdev
amdev

Reputation: 7442

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

Upvotes: 8

Related Questions