Part 9 - How does a virtual environment work? - Structures and Files, and Prefix-Finding

Part 9 - How does a virtual environment work? - Structures and Files, and Prefix-Finding

It copies structure and files

Do I need to know all of this stuff? And will it help be to debug my virtual environment when it breaks? It copies structure and files. Okay, so it copies structure and files.

So what? Will it help me to fix the thing? "When you create a virtual environment using venv, the module re-rcreates the file and folder structure of a standard Python installation on your OS."

"Python also copies or symlinks into that folder structure the Python executable with which you've called venv." EH? Oh I think it's talking about the Python interpreter here omg. I am finding this stressful and so, so, so confusing.

"If you locate your system-wide Python installation on your operating system and inspect the folder structure there, then you'll see that your virtual environment resembles that structure." Um okay then yes. Thanks. Whatever. Thanks.

pink technology-themed images. text reads 'virtual environments - part 9'

Okay I think I got it now - thanks. Anyway - yes. "You can find the base Python installation that your virtual environment is based on my navigating to the path under the home key in pyvenv.cfg." Okay yes sure.

I don't want to go so deep. But I can understand nothing unless I go this deep. There might be some extra files and folders in the base Python installation, but the standard structure will be the same. I get it - Python venvs are replicas of the file structure of the base Python.

It Adapts the Prefix-Finding Process

EH? "With the standard folder structure in place, the Python interpreter in your virtual environment can understand where all relevant files are located. It does this with only minor adaptations to its prefix-finding process according to the venv specification."

Okay, hmmm.

"Instead of looking for the os module to determine the location of the standard library, the Python interpreter first looks for a pyvenv.cfg file."

"IF THE INTERPRETER FINDS THIS FILE AND IT CONTAINS A HOME KEY, THEN THE INTERPRETER" USES THAT KEY TO SET THE VALUE FOR TWO VARIABLES.

THOSE TWO VARIABLES ARE SOMETHING TO DO WITH PREFIXES.
  • There is a prefix that will hold the path to the Python executable use to create this virtual environment, which you can find at the path defined under the home key in pyevenv.cfg
    • This is called `sys.base_prefix`
  • The other one is called `sys.prefix` and will point the the directory containing `pyvenv.cfg`. Why is this so difficult and so complicated to understand nooo.
OKAY NOW THERE IS AN INTERACTIVE REAL EXERCISE OMG.
  • I ran the two above commands in a Python shell
  • I saw that when the venv was active, they pointed to two separate python paths
  • I saw that when the venv was deactivated, they pointed to the same path
Great. Now I just need to activate my virtual environment again. 

OOPS - I ACTIVATED MY WORK ONE - LUCKILY I FIXED IT IN TIME - HAHA. Okay so what's next then?
  • If these two variables have different values then 
    • PYTHON ADAPTS WHERE IT WILL LOOK FOR MODULES
    • OH OKAY YES I GET THIS YES WOW
  • Allows the Python interpreter in your venv to use
    • THE STANDARD-LIBRARY MODULES FROM YOUR BASE PYTHON INSTALLATION
    • WHILE POINTING TO ITS INTERNAL SITE-ACKAGES DIRECTIORY
      • TO INSTALL EXTERNAL PACKAGES
      • AND ACCESS EXTERNAL PACKAGES
ARRRRGH CHAT GEEPEETEE PLEASE HELP ME PLEASE EXPLAIN

I am so lost omg. Even with MR GPT 

Comments

Popular posts from this blog

Hello World

“But yesterday, I heard God say, you were born to be the one…”

In the Water, I find Fire