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. If you're not interested, you can just skip it. So, to understand how it works,
0:10 you first have to understand how the path variable works. Path is an environment variable, and it exists on pretty much any operating system.
0:22 It specifies where you're operating systems should look for programs and commands that you run. When you display the content of a path variable,
0:30 you will see that it contains different folders. In this case, pyenv is the first directory.
0:35 But if we remove it, we go back to the default path variable that we have on our operating system. So, for example, if I run Python in the terminal,
0:47 my operating system will check for Python binary file in all those locations specified in the path variable. So, first it will check user/local/bin.
0:58 If it's not there, then user/bin, then bin user sbin sbin, and so on and so on. If it can't find Python in any of,
1:08 those directories is going to throw an error. If we run which Python, you can see in which directory this binary is located, in my case,
1:17 it was in the /usr/bin. When we use pyenv, if it modifies the path variable, it adds the path to the current Python version installed with pyenv.
1:28 At the beginning of that variable now, when you run Python, your operating system will first check inside this pyenv/shims
1:36 directory, and it will find Python binary there, so it will run this one instead of using the system Python.
1:44 We can confirm this by running which Python and, as you can see it found Python binary in the first directory,
1:50 so, it's not checking the other directories. So, that's why it's important to put the pyenv folder at the beginning of your
1:57 path variable. To better reflect how pyenv of works the folder with Python binary
2:03 is called Shim. It means a small tool that lies between the Python command in your terminal and the Python binary that is executed.
2:12 Thanks to that, pyenv is completely separated from any Python version that existed on your
2:17 computer before you install it. If you ever mess up something and pyenv stops working all you have to do is to remove it and install it again.
2:26 This is very convenient because, for example, if you ever mess up something with the system Python version, your computer will probably stop working,
2:34 and that's much harder to fix.


Talk Python's Mastodon Michael Kennedy's Mastodon