27

I have to install python packages from requirements files that's provided to me. However, when I use pip install -r requirements.txt command I get an error saying ERROR: Invalid requirement (from line 3 in requirements.txt. And when I comment the third line the error just continues to be there for the next lines. What does that mean and how can I install packages from the file?

Here's how the file contents look like:

# Name                    Version                   Build  Channel
alabaster                 0.7.12                   py36_0
altgraph                  0.17                     pypi_0    pypi
appdirs                   1.4.4                      py_0
argh                      0.26.2                   py36_0
astroid                   2.4.2                    py36_0
async_generator           1.10             py36h28b3542_0
atomicwrites              1.4.0                      py_0
attrs                     20.3.0             pyhd3eb1b0_0
auto-py-to-exe            2.7.11                   pypi_0    pypi
autopep8                  1.5.4                      py_0
babel                     2.9.0              pyhd3eb1b0_0
backcall                  0.2.0                      py_0
bcrypt                    3.2.0            py36he774522_0
black                     19.10b0                    py_0
bleach                    3.2.2              pyhd3eb1b0_0
bottle                    0.12.19                  pypi_0    pypi
... So on

I am using new environment in Anaconda with python version 3.6.12.

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
harry r
  • 786
  • 2
  • 6
  • 19
  • 4
    That's not a valid requirements file. To create one use `pip freeze` not `pip list`. – Klaus D. Apr 01 '21 at 06:56
  • oh thats unfortunate. So what should I do now if I want to install the packages? Is there anyway to convert in right format? Actually I am not the creator of this I was provided the software with requirements. – harry r Apr 01 '21 at 06:59

3 Answers3

47

First, freeze all of your pip packages in the requirements.txt file using the command

pip freeze > requirements.txt

This should create the requirements.txt file in the correct format. Then try installing using the command

pip install -r requirements.txt

Make sure you're in the same folder as the file when running this command.

If you get some path name instead of the version number in the requirements.txt file, use this pip command to work around it.

pip list --format=freeze > requirements.txt
revmatcher
  • 757
  • 8
  • 17
  • Hey I used your solution and now the new txt file has packages that look like ```absl-py @ file:///D:/bld/absl-py_1606235847480/work aiohttp @ file:///D:/bld/aiohttp_1614281175709/work alabaster==0.7.12 appdirs @ file:///home/conda/feedstock_root/build_artifacts/appdirs_1603108395799/work argh @ file:///home/conda/feedstock_root/build_artifacts/argh_1595627874344/work argon2-cffi @ file:///D:/bld/argon2-cffi_1605217285838/work ``` There should be version instead its giving me some file location which I am not aware of. – harry r Apr 01 '21 at 07:11
  • 3
    Apparently this is an open issue on `pip`. Use this `pip list --format=freeze > requirements.txt` to work around it. More info on this available here https://stackoverflow.com/questions/62885911/pip-freeze-creates-some-weird-path-instead-of-the-package-version – revmatcher Apr 01 '21 at 07:14
  • This get all the os pythons dependencies, not the project itself – JRichardsz Jan 21 '23 at 17:20
5

Change your requirements.txt content as below and try pip install -r requirements.txt again.

alabaster==0.7.12
altgraph==0.17
appdirs== 1.4.4
argh==0.26.2
astroid== 2.4.2
async_generator==1.10
atomicwrites==1.4.0
attrs==20.3.0
auto-py-to-exe==2.7.11
autopep8==1.5.4
babel==2.9.0
backcall==0.2.0
bcrypt==3.2.0
black==19.10b0
bleach==3.2.2
bottle==0.12.19
Rabindra
  • 369
  • 4
  • 14
  • I know thats the right format. I would have done it myself but the list is very long. It doesn't seem practical to do this manually. How do I make changes? Is there any way to convert the file using python command/package? – harry r Apr 01 '21 at 07:02
  • You shouldn't have to manually change the content of the file, right? Why not use `pip freeze > requirements.txt`? – revmatcher Apr 01 '21 at 07:02
  • 1
    @harryr Yes, I suggested this manual option assuming that your conda environment is not installed with the packages. However, if the packages are already installed in the environment, then you can follow the steps as revmatcher has suggested. – Rabindra Apr 01 '21 at 07:06
1

If you use Anaconda for environment management you most likely created requirements.txt file via:

conda list --explicit > requirements.txt

To recreate the environment with all your listed packages use:

conda env create --file requirements.txt

See CONDA CHEAT SHEET.

BoB
  • 129
  • 2
  • 9