Modern Python Projects Transcripts
Chapter: Python versions and packages
Lecture: 3 levels of pyenv

Login or purchase this course to watch this video and the rest of the course contents.
0:00 When we use pyenv, we can choose one of three different levels at which we want to change by Python version. First,
0:08 we have the global command, that we used in the previous lesson. This is the most common usage of pyenv.
0:14 It changes the Python version globally on your computer. This is an equivalent of installing a different Python version.
0:21 Next, we have local command, running pyenv local will set a specific Python version for a current folder. And for all the sub folders,
0:31 you would commonly used this command if you're working on different projects and each of them requires different Python version. For example,
0:38 one is using Python 3.8 and another is using Python 3.6. Instead of changing the global Python version back and forth,
0:47 you can just call pyenv local 3.6 in one folder and pyenv local 3.8 or 3.9 in another folder. And you are all set. pyenv full automatically switch
0:59 Python versions when you go inside, Either of those folders, let's see a short demo, as you can see my global
1:09 Python version Python version is Python 3.9 and let's say I have another project that requires Python 3.8
1:16 If I go inside this directory and I run pyenv local and I specified version of Python, you can see that now we're using 3.8.6,
1:36 and if we run pyenv list, and if we run pyenv versions, you can see that this version comes actually from this directory,
1:46 not from a global one. How does pyenv keep track of what version it should use.
1:51 Well, it simply creates a file called Python -version and puts the number inside. Later When you run Python Command pyenv checks,
2:02 is there a Python -version file in the current folder? If yes, use that version of Python.
2:10 If not, check the parent folder and the grand parent and So on.. until it gets to the top Most directory. If it doesn't find the Python version file,
2:20 it uses the global Python version, So, the pyenv local command overrides the pyenv global If we go out from this directory,
2:29 you can see that our global Python version is still Python 3.9. We didn't change anything, and finally we have pyenv shell.
2:41 This changes Python version for the current shell session. You might want to use it in a situation where you want to temporarily change which
2:50 Python version you are using. For example, maybe you want to run some code under Python 2. So, here we are using Python 3.9 as a global Python.
3:01 But I can temporarly change the shell to 3.8, or even to a system Python, which in my case, Python 2.7,
3:20 the Python Shell command overrides the Python versions set by pyenv global and pyenv local. So, if we go to our Python 3.8 project,
3:33 you can see that we are still using Python 2.7 that we said with pyenv
3:37 shell, unless you are working on multiple projects that use different Python versions.
40:04 pyenv global is the command that you will be using most often.


Talk Python's Mastodon Michael Kennedy's Mastodon