Asteroid098
Asteroid098

Reputation: 2825

File 'requirements.txt' in a Python package

I'm studying packages, and I have a question on creating a requirements.txt file.

If a module is imported from Python, does it need to be mentioned in the requirements.txt file or can be left out?

I know that for separate modules, they need to be pip installed. For those modules that need to be pip installed, should they be written in the requirements.txt file as 'pip install modulename'?

Upvotes: 6

Views: 34869

Answers (1)

Shubham Paliwal
Shubham Paliwal

Reputation: 101

The requirements.txt is a file where one specifies dependencies. For example, your program can have dependency on Django or we can say the requirements.txt file in a Django project describes the packages that needs to be installed for successfully running a project.

Talking about the sub-modules: Suppose you have specified Django in the requirements.txt file, now you don't need to specify any sub-modules (like 'django.shortcuts') in the requirements.txt file. Because they are already included in the Django module.

Now one can easily use

from django.shortcuts import path

in your .py files.

Now for separate modules (like pillow) that need to be specified in the requirements.txt file, you don't need to write

pip install pillow

You can just write pillow in a separate line with other modules like:

django

pillow

You can also use the below command to do so:

pip freeze > file_name

In your case file_name will be requirements.txt. This will add all the third-party module names in the requirements.txt file.

Upvotes: 4

Related Questions