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
0:05 want to change by python version. First,
0:07 we have the global command, that we used in the previous lesson.
0:11 This is the most common usage of pyenv.
0:13 It changes the python version globally on your computer.
0:16 This is an equivalent of installing a different python version.
0:20 Next, we have local command, running pyenv local will set a specific python version for
0:27 a current folder. And for all the sub folders,
0:30 you would commonly used this command if you're working on different projects and each of them
0:35 requires different python version. For example,
0:37 one is using Python 3.8 and another is using Python 3.6.
0:43 Instead of changing the global python version back and forth,
0:46 you can just call pyenv local 3.6 in one folder and pyenv local 3.8
0:52 or 3.9 in another folder. And you are all set. pyenv full automatically switch
0:58 python versions when you go inside,
1:00 Either of those folders, let's see a short demo, as you can see my global
1:08 Python version python version is python 3.9 and let's say I have another project that requires python 3.8
1:15 If I go inside this directory and I run pyenv local and I specified
1:27 version of python, you can see that now we're using 3.8.6,
1:35 and if we run pyenv list,
1:38 and if we run pyenv versions,
1:40 you can see that this version comes actually from this directory,
1:45 not from a global one. How does pyenv keep track of what version it should use.
1:50 Well, it simply creates a file called python -version and puts the number
1:56 inside. Later When you run Python Command pyenv checks,
2:01 is there a python -version file in the current folder?
2:06 If yes, use that version of python.
2:09 If not, check the parent folder and the grand parent and So on.. until it
2:14 gets to the top Most directory.
2:16 If it doesn't find the Python version file,
2:19 it uses the global python version, So, the pyenv local command overrides the pyenv global
2:25 If we go out from this directory,
2:28 you can see that our global python version is still Python 3.9.
2:33 We didn't change anything, and finally we have pyenv shell.
2:40 This changes python version for the current shell session.
2:44 You might want to use it in a situation where you want to temporarily change which
2:49 python version you are using.
2:51 For example, maybe you want to run some code under python 2. So, here we
2:57 are using Python 3.9 as a global python.
3:00 But I can temporarly change the shell to 3.8,
3:05 or even to a system python,
3:07 which in my case, python 2.7,
3:19 the Python Shell command overrides the python versions set by pyenv global and pyenv
3:24 local. So, if we go to our Python 3.8 project,
3:32 you can see that we are still using Python 2.7 that we said with pyenv
3:36 shell, unless you are working on multiple projects that use different python versions.
40:03 pyenv global is the command that you will be using most often.