5

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?

Engo
  • 749
  • 1
  • 13
  • 42
  • A stanalone venv will not contain the modules for you, it has to use a list of requirements and will usually read that from requirements.txt – idjaw Jun 29 '17 at 14:16
  • 1
    @idjaw I think their said project contains the virtualenv with all the modules installed. – Moses Koledoye Jun 29 '17 at 14:18
  • 2
    This combination would never be useful. In fact keeping venv in a repo is not a good idea. That's because venv and installed packages depend on the underlying OS. While a project should be as independent on OS as possible. – freakish Jun 29 '17 at 14:18
  • @MosesKoledoye Right. Then it's a matter of explaining that is *not* a good idea to do and instead "install" the environment rather than just transferring a venv. – idjaw Jun 29 '17 at 14:20
  • 1
    @idjaw Yep, right. @ freakish already provides a pointer. They'll find this useful: https://stackoverflow.com/questions/6590688/is-it-bad-to-have-my-virtualenv-directory-inside-my-git-repository – Moses Koledoye Jun 29 '17 at 14:21
  • @freakish Explain this as an answer to help provide resolution on this question. I think in this case, it would help to just have it as one to close it off. – idjaw Jun 29 '17 at 14:22

2 Answers2

7

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.

freakish
  • 48,318
  • 8
  • 114
  • 154
  • This is exactly what happened to me. It confused me :) Awesome explanation, thanks! – Engo Jun 29 '17 at 14:24
  • Follow-up question: Do you prefer to keep your venv folders in your Python project development folder or in a different folder? – Engo Jun 29 '17 at 14:29
  • 2
    @Engo I personally use virtualenvwrapper which by default installs each venv in `~/.virtualenvs`. Works like a charm. – freakish Jun 29 '17 at 14:30
1

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.

David Cullen
  • 11,437
  • 2
  • 34
  • 55
bakatrouble
  • 1,543
  • 9
  • 18
  • Why is it bad to deliver a virtualenv that includes all required modules? This is not clear to me... – Engo Jun 29 '17 at 14:18
  • 2
    Virtualenvs on different systems have different layouts, and they may contain compiled binaries that just won't run on a different machine. – bakatrouble Jun 29 '17 at 14:21