Is there any way to output requirements.txt automatically?
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:
- 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.
- using DEEPHELL
pip install --user dephell
- 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
![chenzhongpu](https://i.stack.imgur.com/0WXuV.jpg?s=256&g=1)
chenzhongpu
Updated on July 15, 2022Comments
-
chenzhongpu almost 2 years
I want to output the
requirements.txt
for my Python 3 project in PyCharm. Any ideas? -
Alexander Zhak about 9 yearsThis does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post.
-
jsbueno about 9 yearsSorry - 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 about 7 yearsI 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 over 5 yearsThis should be the one answer to have more votes. It is exactly as it works.
-
Anum Sheraz over 5 yearstrying 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 over 4 yearsbest answer hands down. for anyone who needs it fast: "pip install pigar " and then "pigar" (in the project root)
-
gented over 4 yearsWhy 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 over 4 yearsThe 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 over 4 yearsI 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 over 4 yearsyes. 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 about 4 yearsusing 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 almost 4 yearsit even can be installed via conda !