# Python Tutorial: How to Set the Path and Switch Between Different Versions/Executables (Windows)

## Метаданные

- **Канал:** Corey Schafer
- **YouTube:** https://www.youtube.com/watch?v=OdIHeg4jj2c
- **Дата:** 26.07.2018
- **Длительность:** 24:17
- **Просмотры:** 220,478

## Описание

In this Python Programming Tutorial, we will be learning how to set the PATH environment variable on the Windows Operating Systems.

Be one of the first 200 people to sign up with this link and get 20% off your premium subscription. https://brilliant.org/cms

We will also learn how to modify the PATH in order to switch between different Python versions and executables. This is an essential skill that trips up many people when they first start learning Python and will definitely help you in many real-world situations. Let's get started...

How to Set the Path on Mac and Linux:
https://youtu.be/PUIE7CPANfo


✅ Support My Channel Through Patreon:
https://www.patreon.com/coreyms

✅ Become a Channel Member:
https://www.youtube.com/channel/UCCezIgC97PvUuR4_gbFUs5g/join

✅ One-Time Contribution Through PayPal:
https://goo.gl/649HFY

✅ Cryptocurrency Donations:
Bitcoin Wallet - 3MPH8oY2EAgbLVy7RBMinwcBntggi7qeG3
Ethereum Wallet - 0x151649418616068fB46C3598083817101d3bCD33
Litecoin Wallet - MPvEBY5fxGkmPQgocfJbxP6EmTo5UUXMot

✅ Corey's Public Amazon Wishlist
http://a.co/inIyro1

✅ Equipment I Use and Books I Recommend:
https://www.amazon.com/shop/coreyschafer

▶️ You Can Find Me On:
My Website - http://coreyms.com/
My Second Channel - https://www.youtube.com/c/coreymschafer
Facebook - https://www.facebook.com/CoreyMSchafer
Twitter - https://twitter.com/CoreyMSchafer
Instagram - https://www.instagram.com/coreymschafer/

#Python

## Содержание

### [0:00](https://www.youtube.com/watch?v=OdIHeg4jj2c) <Untitled Chapter 1>

hey there how's it going everybody in this video I'm going to try to explain the solution to a problem that I see a lot and also a common question that I get on my videos and the problem that I'm talking about can take a few different forms um so maybe you've installed python but when you type python in the command line it either says that it doesn't recognize the python command or maybe it gives you a different version than what you thought that you'd installed or maybe you're trying to use a feature that's only available in later versions of python like FST strings and python 36 and it's just not working for you so that likely means that you're not using the version of python that you think that you're using um or maybe you're trying to import something that you thought that you installed through pip but it keeps giving you an import error and telling you that it can't find the module so all of these problems are likely caused by your machine not using the correct python interpreter so in this video we'll be learning how to find out which python interpreter you're using and where it's located on your machine we'll also see how to switch between different python versions and environments and also how to troubleshoot Imports if they aren't working correctly so in this video we're going to be seeing how to do all of this on the Windows operating system but I also have a video on how to do this on Mac and Linux as well so if you're on one of those operating systems then I'll have a link to that video in the description section below um okay so with that said let's go ahead and get started so first of all maybe you've tried installing python but it still isn't working on your machine so I've installed python 2 and Python 3 on my Windows machine here but when I open up my command line and run the python command then we can see that it says python is not recognized as an internal or external command now the reason that this isn't working is because our machine is only looking in certain places for commands if it doesn't find a command in those locations then it will give us this response that it's not recognized as an internal or external command now we could just run python by putting the exact location of the executable so when I installed python 2 and Python 3 on this machine I installed them in two different locations so let me bring those up so python 2 is installed here I'm not sure if you can see this but it's in my C drive in a directory called python 27 and in and for Python 3 it's a bit more of an obscure location it's in users Cory app data local programs python 37 so let me go back to where I installed python 2 and that python 27 directory So within this directory we have a python. exe executable so this is what runs python so if I use this entire path to this executable then I should be able to run python in our command line so let's go ahead and try that so I'm going to grab part of this path up here from the bar and go back to our command line and now I will paste that in and within that directory I will just run python so you could do python. exe or just python I'm just going to use the python command and run that and you can see that it runs python uh 27 15 okay so now I'm going to exit out of that and clear the screen here um and now let's go to that Python 3 directory so I'll pull that up now like I said this is in a bit more of an obscure location so this is where it automatically installed when I downloaded python online now if you don't know where python was installed on your machine then I would suggest going back to the python download page uh and when you download python it allows you to install in the default directory which in my case was that long path that uh we just saw but if you don't want to install it uh there then you can use a custom location and you have that option as well so now let's go back to our file explorer here so you can see in this directory I have a python. exe as well um so just like with python 2 if I use the full path to this Python 3 executable then I can run that in my command line as well so I'm going to copy this entire path here and pull up my command line and paste that in sorry it's wrapping to another line here and within that directory I'm going to run the python command so by running the full path to that command we can see that it's running python 37 um okay so now I will exit out of there um okay so that's good but it's probably not why you're watching this video so it would be nice to simply be able to use the python command like I tried to at the beginning of the video and have it run the version of python that we want so like we saw before if we run Python and run that then it says python is not recognized as a command and again the reason that this isn't working is because our machine is only looking in certain places for that python command or executable and if it doesn't find it in those locations then it will give us this response that it's not recognized now we can add directories to the locations that our machine will look and we can do that by

### [4:53](https://www.youtube.com/watch?v=OdIHeg4jj2c&t=293s) Adding these Directories to the Path Environment Variable

adding these directories to the path environment variable so on Windows we can get to the path environment variable by opening our start menu so I'm going to do that down here at the bottom and we can search for system and we'll click here with uh within system within the control panel so click on that and within here click on Advanced system settings and then go to environment variables So within here we have our user variables and our system variables now we can simply add to our path under our user variables so I'm going to click on the path up here and then I'm going to click on edit and now we can add

### [5:31](https://www.youtube.com/watch?v=OdIHeg4jj2c&t=331s) Add Directories to that Path Environment Variable

directories to that path environment variable so to do that we can just simply click new and now we can add the path so now we can just paste in our directory that contains our python executable so I'm going to paste in that Python 3 directory that I copied just a bit ago so I'll paste that in and I'm also going to add the directory that contains our python 2 executable so I'm going to click new here then go back to our file explorer and open up that location and copy that go back to our environment variables and paste that in now notice here that we're pasting in the directories uh not the full path to the executable so we left off the uh python. exe on the end of those just the directories now the directories higher on the path have higher precedence so rearrange these so that your preferred version of python is higher in the path so for example if I have the python 2 directory at the top so if I click on this and click on move up and then I'll put Python 3 here um second so I'll move that up one so I have python 2 then Python 3 then now let's save all of this so I'm going to click okay down here at the bottom and okay down here and okay here and now let's restart our command

### [6:47](https://www.youtube.com/watch?v=OdIHeg4jj2c&t=407s) Restart Our Command Line

line so I'm going to quit out of what we had before and open that back up and maximize this again okay so now let's see what happens when we type in Python this time so if I type in Python and hit enter Then you can see that we actually get python 27 here and the reason for that is because we had python 2 above the Python 3 directory in our path environment variable now if I go back to those environment variables so I'm going to reopen our system settings here click on Advanced system settings go down here to environment variables at the bottom and click up here on our path and go to edit now I'm going to click on Python 3 and move that above python 2 and then to save this I'm just going to click okay on all of these settings here and then restart our terminal again so restart our terminal maximize that and now if we type in Python then we can see that now we're running python 3. 7 and that's simply because it finds that Python 3 executable in the path before the python 2 executable now python now gives you the option when you download python to automatically add the installation direct directory to the path automatically so you might not have to do this manually but it's still good to know how the path works and to be able to change this manually if you need to now one more tip when it comes to the path is that you'll likely want to also

### [8:15](https://www.youtube.com/watch?v=OdIHeg4jj2c&t=495s) Add Pip to the Path

add pip to the path as well so pip is what allows you to install packages so you want that command to work on the command line as well um now it's not in the same directory as python it's in the same general location as the python executable but it's inside of a scripts directory um so let's add this really quick so first of all just to see this if I pull up the python 2 location here then we can see that we have a scripts directory and if I open that then you can see that our pip executable is inside of that uh folder and if I open up our Python 3 directory then we have a scripts directory in here as well so if I open that then we can see that we have a pip inside that scripts directory as well um so now let me go back here and let's add that to our path so I am going to open up that scripts directory and copy that and go back to our system settings Advanced system settings environment variables uh click on our path and edit and now we want to go to new and paste that scripts directory in and then I'm going to move that up underneath our Python 3 directory and I'm going to do the same thing for this python 2 directory as well so I'll click on new and paste that in and I'll just manually type this in so that is scripts inside that python 27 directory and I'm going to move that up underneath the python 27 directory and like I said doing that is going to allow you to use the PIP command with those python versions um so just to show this I haven't saved that yet so if I go back here to the command line and exit that and run pip then we can see that it says pip is not a recognized command just like it did with python before um but now we can close down our command line and we can just click okay to save these environment variables so I will save all of those reopen our uh command line again and now if we type pip then we can see that works but it's looking for a pip command so I'm just going to go ahead and clear the screen there and again like I said before if you told python to add everything to your path when you first installed it then that should have already been taken care of in Terms of adding the scripts directory and your python directory uh to your path environment variable but it's still good to know how to add it if you need to um okay so now let's look at a couple more tips um so let's say that your python command is working but you have no idea which python executable you're using or where it's located now this can definitely happen if you're using virtual environments or have a lot of different versions of python installed um so let's say that you type in Python and it says that you're running python 37 but that doesn't really do you much good if you have 10 different virtual environments that are all using python 37 so how can we actually see exactly which python we're running so to find this out we can use the CIS module and this is a built-in module um So within python we can say import CIS and like I said that's a built-in module so we don't need to install anything uh and now we can simply look at c. x executable to see what executable we're currently using so that is sis. executable and if I run that then we can see that we get this long path here to this python executable and we can see that long path is what we added to our path environment variable before that python 37 directory um so if that value is what you expect it to be then that's great but if it's not then using the value returned from cy. executable can help you debug what's going on if it's not the directory that you think it should be so if something else then you'll have to check your path environment variable again and try to figure out why that executable is being seen before the executable that you think it should be so there might need to be some rearrangement like we saw a second ago

### [12:17](https://www.youtube.com/watch?v=OdIHeg4jj2c&t=737s) View Your Entire Path from within the Command Line

so if you ever want to view your entire path from within the command line then let me exit out of python here and let's Echo our entire path to see what we should beting so to do this on Windows it is Echo and then parentheses or uh percent signs path percent signs so I will run that now there's going to be a lot more here than what we actually added in but our additions are going to be at here at the very end so if we look here then I can highlight this okay so our additions start right here and we can see that we have our Python 3 directory and then after scripts 27 directory and then our python 27 scripts directory so if you ever run Python and you import CIS and then run sis. executable and it gives you a location that uh you weren't expecting then you can Echo out your path and do a little debugging there to see why it's running that version of python above another version that you may have expected and you might need to do some rearranging there in order to get it set up how you'd like so for example if I wanted python 27 to be the uh default python then I could come in here to advance system settings environment variables uh edit our path and then I could move python 27 up uh to the top which would make that the default python but I'm not going to do that now I'm just going to leave that where it was and cancel out of those and go back here to my command line so now let me uh clear this out here okay so I also get a lot of questions from people who tell me that they've pip installed a package but when they try to import it doesn't work so this can also be a path issue so Python and pip are two separate commands and therefore could be located in two different places and we've already seen that because uh python was in our python directory and the PIP command was in that scripts directory so if your path is messed up then it's entirely possible that uh what you're installing with Pip isn't installing that package for the version of python that you think it's installing it for um so for example let's say that I wanted to install D Jango so to do that I could just say pip install Django and if I run that then it says that the requirement is already satisfied so we already had that installed but it also gives us the location where it tried to install this and in that python uh 37 directory in this lib site packages directory now another way to get the location of a package is to use PIP show so let me clear the screen here real quick and now let's use PIP show so I'll say PIP show Django and if I run that then it gives us a lot of uh information about the installed package but also one of those is a location variable down here and it says that the location is in this lib site packages directory of that python 37 folder that we saw before okay so that's important because uh if you're trying to import a package that you've installed and it says that it can't find the package then make sure that the location of that package is actually in the lib site packages directory of the version of python that you're using so again if I was to open python here and import CIS and then check s sis. executable then the location that it shows us here uh needs to be the same as the location where it's telling us that package is installed now since those are the same right now then it means that I should be able to import D Jango with no problems and if I import that then we can see that I don't get any problems but if that was telling me that it can't find a package called D Jango or that there's no module named Jango then I might want to check those locations to see if those are off and if they are then that's your problem it means that your pip command is installing Jango for uh python in a different location than the python that you're actually using so if those locations didn't match then that would be your problem and you would need to fix your path in the ways that we talked about before by rearranging those okay so now let me exit out of here and clear my screen now I think that's everything that I wanted to cover in terms of figuring out how to set your python path and see which executable you're using but another thing I should mention is that we mainly covered the command line in this video but sometimes I get questions from people who are using an editor like Sublime text or something similar and they tell me that their command line is using the correct version of python but that their editor isn't so for example sometimes will uh people will tell me that you know F strings aren't working for them uh but they have python 36 installed and it's working on their command line well in that case you know most likely that means that your editor just isn't using python 36 even though you've installed it and a good way to determine that would be to compare that cy. executable value that we saw before so some editors might have a different way of determining which uh version of python they're using instead of just looking at the path so in Sublime Text they use something called build systems and I have a completely separate video on how to set up sublime Tech build systems but understanding what we covered in this video here uh should help you better figure out which directories you you'll need to add to those build systems in order to get those set up properly and you may need to do something similar with other editors and idees as well uh depending on what you're using but really quick let me show you a fast

### [18:01](https://www.youtube.com/watch?v=OdIHeg4jj2c&t=1081s) Sublime Text Example

Sublime Text example uh just to show you what this looks like now I'm not going to go into too much detail here since uh this isn't exactly a Sublime Text tutorial but I'll just show you how I've set this up uh for me so that I can switch between Python 2 and Python 3 so I have a demo file here where I'm just printing out the uh cy. version and cy. executable so if I run this right now then we can see that this is running python 37 and the executable is what we saw before it is in that uh long directory and that python 37 directory okay so that might be what you want but what if you wanted to use a different version of python or what if you wanted to use the environment from a virtual environment uh so to do that uh we would have to create a new build system so if you come up here to tools and then go to build system then at the bottom you have the option to create a new build system now if you were to create a new build system then it would basically give you an empty Json file and you would have to fill in certain values um so let me show you what this looks like so we can see here I've created a build system called python 27 now let me show you what mine looks like so I've got that pulled up here I'm going to uh close down that output for now so this is what I've filled in for my python 27 build system now again I'm not going to go into this fully since I have a completely different video on this uh but you can see here that I'm passing in the full path to my python 27 directory that we saw before and also the python executable there and also on Windows you can see that we need to escape our backslashes here within the string um but you can kind of see the concept now if this was a python uh 3 build system then I would have the uh path to the Python 3 executable there if this was a virtual environment then I would have the executable for the virtual environment here um so whatever the path to that to whichever executable you want to use goes here okay so that's the python 27 build system so let me open up my uh sample file here again and I will go to tools build system and I'll actually select that python 27 build system so if I select that and rerun this then we can see that now when we print out that cy. verion and cy. executable now it's printing out python 27 and the executable is that python 2 7 directory that we've uh seen throughout the video now technically since we're using python 2 we should probably change these print statements so that they the syntax matches python 2 syntax but that's okay uh as is for now it's just a demo okay and lastly I also briefly

### [20:44](https://www.youtube.com/watch?v=OdIHeg4jj2c&t=1244s) Virtual Environments

mentioned virtual environments now if you've never used a virtual environment it's basically a way uh to have a separate python environment per project and that also includes separate Python executables and packages now you might be wondering if you'd have to go through that whole process every time you activate and deactivate a virtual environment but in the command line you won't have to do that so when you activate and deactivate a virtual environment there is code included that automatically adds and removes those directories to the beginning uh of your path for you so you shouldn't have to worry about that but when you're using an editor like Sublime Text or anything else then you'll just have to do an online search to see exactly how to use that environment Within that editor so in Sublime Text it's the build systems that we saw here in this video but I'm sure it's different in other editors and in the next couple of weeks I'm going to be doing a video on the newest official way to handle virtual environments using pip EnV so if you're interested in learning more about virtual environments then be on the lookout for that video okay so in this video we went over one of the most common questions that I get among people who are just getting started out with python so it's common when people are first getting started out to occasionally get stuck in the same places now when it comes to getting started out in computer science in general uh that same concept applies now the most common questions that I see there have to do with the fundamentals you know such as data structures or algorithms now a great resource for Learning and practicing those common fundamentals would be our sponsor for this video brilliant. org brilliant is a problemsolving website that helps you understand underlying concepts by actively working through guided lessons they have computer science courses ranging from algorithms and data structures to machine learning and neural networks they even have a coding environment built into their website so that you can run code directly in the browser and that's a great way to complement watching my tutorials because you can apply what you've learned in their active problem solving environment and that helps to solidify that knowledge their guided lessons will challenge you but you also have the ability to get hints or even Solutions if you need them it's really tailored towards understanding that material so their computer science material is fantastic and I really like what they're doing they also have plenty of courses depending on what you're most interested in so they have courses in different fields of mathematics or astronomy solar energy computational biology and all kinds of other great content so to

### [23:08](https://www.youtube.com/watch?v=OdIHeg4jj2c&t=1388s) Support My Channel

support my channel and learn more about brilliant you can go to brilliant. org CMS to sign up for free and also the first 200 people that go to that link will get 20% off the annual premium subscription and you can find that link in the description section below and again that's brilliant. org CMS okay so I think that is going to do it for this video uh hopefully now you have a good idea for how you can manually set the python path if you need to and also how you can switch between different executables but if you do have any questions about what we covered in this video then feel free to ask in the comment section below and I'll do my best to answer those and if you enjoy these tutorials and would like to support them then there are several ways you can do that the easiest way is to Simply like the video and give it a thumbs up and also it's a huge help to share these videos with anyone who you think would find them useful and if you have the means you can contribute through patreon and there's a link to that page in the description section below be sure to subscribe for future videos and thank you all for watching

---
*Источник: https://ekstraktznaniy.ru/video/12236*