Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do multiple, related Python projects need their own virtual environment?

I have two, related Python projects:

../src/project_a
../src/project_a/requirements.txt
../src/project_a/project_a.py

../src/project_b
../src/project_b/requirements.txt
../src/project_b/project_b.py

Both projects use the same version of Python. The respective requirements.txt files are similar but not identical.

Do I create a separate virtual environment for each project or can I create a "global" virtual environment at the ../src level? Note: I'm obviously new to using virtual environments.

like image 754
RobertJoseph Avatar asked Oct 16 '22 09:10

RobertJoseph


1 Answers

Virtual environments are meant to keep things isolated from each other.

  • If one project is a dependency of the other one, then they have to be installed in the same environment.
  • If two projects have dependencies that conflict with each other, then they have to be installed in different environments.
  • If two projects are meant to be run on different versions of the Python interpreter, then they have to be installed in different environments.

That's basically the only rules (I can think of). To me the rest is just a mix of best practices, personal opinions, common sense, technical limitations, and so on.

One could think of the pet vs cattle analogy (again) for example. Virtual environments can be seen as throw away things, that are created on demand (automatically with tools such as tox for example), which is easy once the dependencies are clearly written down (in requirements.txt for example).

In your case, I would probably start with a single Python virtual environment, and only start creating more when the need arises. Most likely this will happen once the projects grow in size. And eventually it could become an absolute necessity once a project requires specific versions of dependencies that conflict with the dependencies of the other.

like image 147
sinoroc Avatar answered Oct 19 '22 10:10

sinoroc