Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why would you create a requirements.txt file in a virtual environment in Python?

I downloaded a Python project and it contains both a virtual environment and a requirements.txt file. Why would you need both? As far as I know, virtual environments already contain the required modules. Any idea when and why this combination would be useful?

like image 594
Engo Avatar asked Jun 29 '17 14:06

Engo


2 Answers

While it is technically possible I don't find any good reason to that. Having both is confusing because it is not clear which one is the "master". And you have to (or not?) worry about consistency between installed packages and requirements.txt file.

Also venv and installed packages in many cases depend on the underlying OS, they have binaries, different layout, etc. It is generally advised to write os-independent code.

All in all, I would stick to requirements.txt file and remove any venv folder from the project's repo.

like image 96
freakish Avatar answered Oct 01 '22 03:10

freakish


You can't distribute the virtualenv directory with your project because the contents may vary depending on the target operating system and the version of the operating system. Specifically, a virtualenv that includes libraries with compiled components installed on Ubuntu 14.04 will differ from the equivalent virtualenv installed on Ubuntu 16.04.

Instead, you should distribute your requirements.txt file (just a convention, you could use any file name you want) so the end-user will be able to recreate a virtualenv on his machine.

like image 26
bakatrouble Avatar answered Oct 01 '22 03:10

bakatrouble