Modern Python Projects Transcripts
Chapter: Python versions and packages
Lecture: Bonus: how pyenv works?

Login or purchase this course to watch this video and the rest of the course contents.
0:01 In this bonus lesson, I will show you how pyenv of works.
0:04 If you're not interested, you can just skip it.
0:08 So, to understand how it works,
0:09 you first have to understand how the path variable works.
0:15 Path is an environment variable, and it exists on pretty much any operating system.
0:21 It specifies where you're operating systems should look for programs and commands that you run.
0:27 When you display the content of a path variable,
0:29 you will see that it contains different folders.
0:32 In this case, pyenv is the first directory.
0:34 But if we remove it, we go back to the default path variable that we have
0:41 on our operating system. So,
0:44 for example, if I run Python in the terminal,
0:46 my operating system will check for python binary file in all those locations specified in the
0:52 path variable. So, first it will check user/local/bin.
0:57 If it's not there, then user/bin,
1:00 then bin user sbin sbin,
1:03 and so on and so on.
1:05 If it can't find python in any of,
1:07 those directories is going to throw an error.
1:10 If we run which python,
1:11 you can see in which directory this binary is located, in my case,
1:16 it was in the /usr/bin.
1:19 When we use pyenv, if it modifies the path variable,
1:23 it adds the path to the current python version installed with pyenv.
1:27 At the beginning of that variable now,
1:30 when you run python, your operating system will first check inside this pyenv/shims
1:35 directory, and it will find Python binary there, so it will run this one
1:40 instead of using the system python.
1:43 We can confirm this by running which python and,
1:45 as you can see it found python binary in the first directory,
1:49 so, it's not checking the other directories.
1:53 So, that's why it's important to put the pyenv folder at the beginning of your
1:56 path variable. To better reflect how pyenv of works the folder with python binary
2:02 is called Shim. It means a small tool that lies between the python command in
2:07 your terminal and the python binary that is executed.
2:11 Thanks to that, pyenv is completely separated from any python version that existed on your
2:16 computer before you install it. If you ever mess up something and pyenv stops working
2:21 all you have to do is to remove it and install it again.
2:25 This is very convenient because, for example,
2:28 if you ever mess up something with the system Python version,
2:31 your computer will probably stop working,
2:33 and that's much harder to fix.