7

I created a project in PyCharm that uses flask (among a few other modules) installed in a PyCharm-created virutal environment of Python 3.6.1, and the app itself works great in PyCharm. When I went to set up a requirements.txt file for other virtual requirements, however, I noticed that only virtualenv was listed in the output file. (To create the file, I went to "console" and did "pip freeze > requirements.txt".)

After testing some things out, I noticed that pip list only gave me three modules installed to begin with. I know this is not true because when I go into the settings of my interpreter, PyCharm says there are a lot of other modules installed, but it doesn't seem like they're actually installed.

How can I create a requirements file in PyCharm effectively? Do I just have to list all the modules I installed in my readme and have my users figure out how to install them all themselves?

Screenshots below:

Project Settings dialog: Project Settings

Pip list output: pip list

2 Answers 2

9

Use pipreqs

$ pip install pipreqs
$ pipreqs /home/project/location
Successfully saved requirements file in /home/project/location/requirements.txt

This will export the packages used in your current project directory into requirements.txt

See pipreqs

Why not pip freeze?

  • pip freeze only saves the packages that are installed with pip install in your environment.
  • pip freeze saves all packages in the environment including those that you don't use in your current project. (if you don't have virtualenv)
  • Sometimes you just need to create requirements.txt for a new project without installing modules.
2
  • pipreqs doesn't seem to work. It just throws an error about being unable to decode a character. Aug 3, 2017 at 14:46
  • @BenSchwabe Can you try with --encoding=utf-8 option, and see if the error persists Aug 3, 2017 at 15:33
0

It's certainly strange, the only thing I can think of is that it's a problem with virtualenv on Windows.

Anyways it wouldn't be best practice to create your requirements.txt from pip freeze, because you have more packages installed than the ones that your project requires.

E.g. lets say that your project only requires Flask:

$ pip install Flask
$ pip freeze
click==6.7
Flask==0.12.2
itsdangerous==0.24
Jinja2==2.9.6
MarkupSafe==1.0
Werkzeug==0.12.2

As you can see by installing Flask many more packages were installed, but you don't have to list those in your requirements.txt, as they are not your projects requirements, they are Flask's requirements.

Therefore you should construct your requirements.txt manually. What I usually do is pip install Flask; pip freeze |grep Flask and copy the line Flask==0.12.2 to my requirements.txt, doing this every time I install something with pip.

2
  • But what if Flask itself needs to use those modules? For example, in my code I never explicitly import Jinja2, but I know for a fact that I am using it. I feel like manually excluding files simply because I'm not using them is a risky decision. Aug 3, 2017 at 14:49
  • 1
    When you do pip install -r requirements.txt it will see Flask and start to install Flask, in which process Flask will list its own requirements, therefore also installing those. I.e. you need Flask, Flask requires Jinja2.
    – Paco H.
    Aug 3, 2017 at 17:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.