Is there any way to output requirements.txt automatically?

35,326

Solution 1

Try the following command:

pip freeze > requirements.txt

Solution 2

Pigar works quite well I just tested it

https://github.com/damnever/pigar

The answer above with pip freeze will only work properly if you have set up a virtualenv before you started installing stuff with pip. Otherwise you will end up with requirements which are "surplus to requirements". It seems that pigar goes and looks at all your import statements and also looks up the versions currently being used. Anyway, if you have the virtualenv set up before you start it will all be cleaner, otherwise pigar can save you. It looks in subdirectories too.

Solution 3

open the terminal in Pycharm and type in this command:

pip freeze > requirements.txt

and the requirements.txt will be automatically created

Solution 4

Surely this post is a bit old but the same I contribute with what I learned, to generate the requirements.txt we can do it in three ways, as far as I know:

  1. using FREEZE

pip freeze > requirements.txt

Before running the command be sure that the virtual environments is activated because the command will be executed in the same folder as the project.A file requirements.txt with Python dependencies will be generated in the same folder as the execution. If you use this command all requirements will be collected from the virtual environment. If you need to get requirements used only in the current project scope then you need to check next options.


  1. using DEEPHELL

pip install --user dephell


  1. using PIPREQS

pip install pipreqs

pipreqs /path/to/project


Note

Why to use pipreqs? Because pip freeze will collect all dependencies from the environments. While pipreqs will collect requirements used only in the current project!

plus freeze only saves the packages that are installed with pip install and saves all packages in the environment.

If you like to generate requirements.txt without installing the modules use pipreqs

If there were other ways to do it always grateful to continue learning :)

Solution 5

You can do it in Pycharm by going to Settings and project interpreter. Select all the Packages with their Version and latest. Then copy all this data into a MS word document. The MS word will treat it as a table. Delete the middle column of this table. Now copy all this data into a notepad++. Search for double spaces ' ' or a tab and replace it with '=='. Save this file as a requirements.txt. It will work

Share:
35,326
chenzhongpu
Author by

chenzhongpu

Updated on July 15, 2022

Comments

  • chenzhongpu
    chenzhongpu almost 2 years

    I want to output the requirements.txt for my Python 3 project in PyCharm. Any ideas?

  • Alexander Zhak
    Alexander Zhak about 9 years
    This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post.
  • jsbueno
    jsbueno about 9 years
    Sorry - this does provide an answer to the question. It can be expanded, of course - as can be seen in the comments it is not the desired solution. But this single line typed in the prompt will "output requirements.txt automatically" as asked for.
  • Jad S
    Jad S about 7 years
    I think the author asked for a way to do it in PyCharm. This command would work, but unless it can be used within the PyCharm context than I don't think it answers the question
  • Netwave
    Netwave over 5 years
    This should be the one answer to have more votes. It is exactly as it works.
  • Anum Sheraz
    Anum Sheraz over 5 years
    trying other solutions pip freeze > requirements.txt creates a BIG requirement.txt file including every single (including default) packages. I only wanted to get custom packages which I installed later inside my vertenv. So this HACK worked for me.
  • Chris
    Chris over 4 years
    best answer hands down. for anyone who needs it fast: "pip install pigar " and then "pigar" (in the project root)
  • gented
    gented over 4 years
    Why is this answer even upvoted? pip freeze just lists all installed packages and has nothing to do with the requirements for a project. Why is this answer all over the place?
  • jsbueno
    jsbueno over 4 years
    The installed packages in a given environment are the ones your project should need. If you are working in a large, or even a medium project, and have not isolated it in a virtualenv, you are doing it wrong. You will have to separate the "development" requirements and running requirements manually, of course.
  • gented
    gented over 4 years
    I agree, but this should be specified in the answer, because it makes all the difference. The answer as it is by itself has no mention of it.
  • jsbueno
    jsbueno over 4 years
    yes. but sometimes, being able to type in a short answer that helps the OP is nice as well. Other than that, I hereby invite you to edit this answer and expand it however you like. You are welcome.
  • Roly Poly
    Roly Poly about 4 years
    using the stated command in the terminal gives me an error: "NotADirectoryError: [WinError 267] The directory name is invalid". supposedly you need a virtualenv set up before this. would be nice to see that mentioned somewhere
  • msarafzadeh
    msarafzadeh almost 4 years
    it even can be installed via conda !