Hi could someone explain how virtual environments work, what happens exactly under the hood (internally)? there aren’t many resources online that explain it clearly.I read Artem Golubin’s blog but cannot understand it.I would be extremely grateful if someone could help ;)
Asked
Active
Viewed 316 times
0
-
2Does this answer your question? [How does virtualenv work?](https://stackoverflow.com/questions/8427709/how-does-virtualenv-work) – Jul 24 '21 at 13:56
-
you can consider it as, a seperate python environment is installed in the virtualenv directory, which work when you activate/deactivate that python env or your program access that environment. internally it is same as python env installed in your system. – sahasrara62 Jul 24 '21 at 14:08
1 Answers
2
A virtual environment is just another installation of Python. Your "main" environment might be /usr/bin/python
and /usr/lib/python3.7
, and your virtual environment might be installed under ~/venv/bin/python
and ~/venv/lib/python3.9
. You can use either one as long as you specify the correct Python executable.
You can activate a virtual environment by setting your PATH
and environment variable to prefer the virtual environment over the main environment. That's virtually all ~/venv/bin/activate
does. It also defines a deactivate
shell function that lets you deactivate your virtual environment, restoring your PATH
to the value it had when you sourced activate
.

chepner
- 497,756
- 71
- 530
- 681