简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM