Python Tutorial: How to Set the Path and Switch Between Different Versions/Executables (Mac & Linux)
30:44

Python Tutorial: How to Set the Path and Switch Between Different Versions/Executables (Mac & Linux)

Corey Schafer 26.07.2018 190 081 просмотров 3 232 лайков

Machine-readable: Markdown · JSON API · Site index

Поделиться Telegram VK Бот
Транскрипт Скачать .md
Анализ с AI
Описание видео
In this Python Programming Tutorial, we will be learning how to set the PATH environment variable on the Mac & Linux 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 Windows: https://youtu.be/OdIHeg4jj2c ✅ 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

Оглавление (6 сегментов)

<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 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 you thought you've installed or maybe you're trying to use a feature that's only available in a later version of Python you know like f strings and python 3. 6 and that's just not working so maybe that means that you're not using the version of Python that you think you're using you know also maybe you're trying to import something that you thought you've 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 and 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 Mac and Linux operating systems but I also have a video on how to do this on Windows as well so if you're on Windows then I'll have a link to that video in the description section below ok so with that said let's go ahead and get started so I have my terminal opened up here so first of all maybe you've installed a newer version of Python but you're still getting the old version of Python when you type that into your machine so I have Python 2 and Python 3 installed on my Mac but when I open up my command line and I run the Python command then you can see that we are using Python 2 so let me exit out of that now on Mac & Linux you should always have some version of Python because they ship with Python 2 ready to go but even if that command didn't work then we should be able to figure that out so don't worry if that didn't work right now okay so I've installed Python 3 on this machine but when I type Python I keep getting python 2 so what's the deal here well on Mac and Linux a lot of the times when you install Python 3 it will actually install that with the command of python 3 instead of Python so if I want to run a Python 3 then I can try that Python 3 command and we can see that on my machine that works and that we're using Python 3 7 here so now let me exit out of that again so when we ran that Python 3 command that may or may not have worked for you now if it does work then it means that it found the Python 3 command on our path and if it doesn't work then it means that it couldn't find at that Python 3 command on our path and we'll discuss what the path is here in just one second so this worked for me so let me find out where this is actually located on my machine so one way to see where the actual location of this program is on Mac or Linux is to use the witch command so I can say which python 3 and hit enter and you can see that this command is located at user local bin and then the command is Python 3 now I use the witch command a lot but it's not very useful if you use aliases and I use a lot of aliases so we'll see some examples of aliases in just a second but in order for this to work with both commands and the aliases I've started using the type command so if I say type Python 3 then we can see that it says python 3 is hashed and it's at this location now yours could just say python 3 is and then have the location there without the hashed part ok so the Python 3 command inside of this user local bin directory that is actually what is being run when we run Python 3 and the only reason that works is because that user local bin directory is on our path and by path I mean that it's on our path environment variable that is where your machine looks for all of its commands in a specific order and we can actually view this path if I say echo and then do a dollar sign and all uppercase path if I run this then this gives us our current path here now your output here might be a little different than mine but these are the directories where your machine is looking for commands so each directory is separated by a colon so let me split this up on the multiple lines so that we can read this a little a bit better so I'm going to copy this and open this up in sublime text and I will split these up on a colon here so that we can see this a little better okay so this is where our machine is going to look for commands and it looks in this order so on my machine when I type a command like Python 3 into the command line then and it will go in this order and say ok do I have a Python 3 and use your local bin and that's where it was located but if it wasn't then it would have looked in user bin and then user s been then s been and then user local get bin so like I said these might be different on your machine than mine but this is my current path so if the directory to the command that you're trying to run isn't on this path environment variable then it's not going to work or if it finds a different version of the command that you want before it finds the one that you wanted to use then it will use the first one that it found so if we had a Python here and user bin but it found one and user local bin then it's going to use that one and use your local bin first now we can run these commands by using the full path to these programs also so for example another way for me to run Python 3 is just to use the full path of user local bin Python 3 so let me do that so I'll copy that directory location let me bring back up my terminal here and I will clear this out so now if I say user local bin then ford slash Python 3 and run that then we can see that works it opens up Python 3 7 so now I'm going to exit out of there so if your command isn't in a directory in that path environment variable then you can still run it but you'll just need to provide the full path to whatever you want to run just like we did here so for example I have an anaconda version of Python that currently isn't on my path it's not in my path environment variable but I know where it's installed on my machine and I can still run that version of Python if I provide the full path to that so let me do this so this won't work on your machine unless you've installed this in the same location but mine is in a directory in my home directory called anaconda so if I do an LS in that location so I'm going to say LS and that's in a directory called anaconda and it's in the bin folder within anaconda so if I do an LS there you can see that we have a lot of stuff but if I scroll up here a little bit then you can see that we have a few different commands we have Python 3 6 if I scroll down here to the bottom you can see that we also have a Python command so if I run this python using the full path then that should run that version of python so let me try that so that was in anaconda ford slash bin ford slash python so if i run that then you can see that it's running Python 3 6 but it's running the anaconda version of that so now let me exit out of that now that Python command was a command that wasn't on my path so I had to specify the entire path to where it was located in order to get that to run if I was just to type Python then it would run that Python 2 version that we saw earlier that is on my path so if I wanted that Python command to be used instead of my current Python command that points to Python 2 then I need to add that directory on my path so that it sees that directory before the others so again I would just add that anaconda bin directory to my path and we'll see what it looks like to add a directory to the path in just a second but let me clear my screen here now if your Python 3 command didn't wrk earlier or if it's not using the latest version that you think it should be then you should go back to the python website and redownload python 3 and pay attention to where it installs python on your machine so usually installations will automatically add directories to your path for you but if it's not working then you might have to do that manually so let's see how to manually add a directory to this path so I'm currently within my home folder here on my terminal now if you're not sure if you're in your home directory then you can simply do a CD and hit enter and it should take you back to your home folder and within your home folder you might have a file called dot Bash underscore profile now if you're on a Linux desktop then that might be a dot bash RC file now if you don't have either of these then you can simply create one so let's open this up in the Nano editor and see what this looks like now Nano isn't the best looking editor but it's easy for most people to use so let's go ahead and use that in this video so I'll say Nano then dot bash underscore profile and again on a Linux desktop that would be dot bash RC now I'm gonna have more in this file than you do yours could be completely empty if it is then don't worry about that okay so I'm going to scroll down to the bottom of my file here and we can see that I have a few things commented out here at the bottom where it says setting paths for Python 3 7 so this is

Add a Directory to Our Path Environment Variable

how we actually add a directory to our path environment variable so I'm going to uncomment out these lines here and then we will walk through this ok so first we are saying that we want to set the path equal to and then this big string value here so first of all our path is an all uppercase the second thing to notice here is that there's no spaces between the variable name and the value it's just variable equal to value with no spaces in between that's just how Bash works and then the value that we're setting here is this long string and this contains the directory that we want to add to our path here so this is kind of long but this is where Python installed my version of Python 37 whenever I downloaded it online so this big directory here forged slash library frameworks Python framework versions 3 7 bin directory that whole thing I'm adding to my path and then we have a colon here because remember that directories are separated by a colon and then we have this dollar sign and then curly braces path now what this does is it adds the current value of the path to the end of this string so what would happen if I was to leave this off was that this would just reset the entire path variable just to this one single directory and that's not what we want to add this directory on to our current path and specifically I want this to come before the other directories in the path because if I type in Python 3 I want it to be seen from this directory first before any of the other directories and my current path so I hope that makes sense it's kind of hard to explain I think that'll make more sense once we echo this back out in the terminal again compared to what it was before so also we have a line down here that just says export path and that just says a you know set path to this new variable ok so now that we've uncommented out those lines now let's save these changes by pressing since we're in nano control X and then just hit Y for yes to save and then the name is just bash underscore profile we can just hit enter and it should have saved those and now you

Restart the Terminal

actually need to restart the terminal before those changes take effect but before we do let's go ahead and see what this is going to do so one more time let me just run that type Python 3 command or you can use which and you can see that it says that python 3 is currently at this location and now if we echo out our path then it says that it's at that location because it found that Python 3 command in this user local bin directory which is the first directory on our path so now if we restart our terminal so let me re start this and make this large again so now that I've restarted our terminal the changes that we made to our bash profile should now take effect so now if I type of python 3 and hit enter now we can see that it says python 3 is at this location and that's the location that we added to our path so now if I go to echo and then dollar sign all uppercase path then we can see that we have this big long directory here at the beginning of our path and then what our path was before last time we looked at it so this big long section that I have highlighted here that is what I meant whenever I said that it adds the current value of the path onto the end of that string now that Python three command is within this directory and also within this user local bin directory but since it found it in this directory first and its first on our path then that is the one that is going to use so if you've been having trouble getting the correct Python version or environment to run then hopefully that clears that up a bit in terms of how you'd add those directories to your path environment variable in order to specify which commands you want to prioritize first now a lot of people want to have it set up so that you can use the Python command to open up Python 3 instead of using the Python 3 command and to make that work we can simply use an alias and I mentioned this earlier so to create an alias we can simply say alias Python is equal to Python 3 and again you want to make sure that you don't have any spaces between the variable and the values there so if I hit enter then now if I type Python then we can see that instead of python 2 7 like it was using before now it's using this python 3. 7 and the reason for that is because the Python command is now pointing to the Python 3 command so we can see this if I type exit and then let me clear the screen here now if I use that type command again so if I say type Python then we can see that it prints out Python is alias to Python 3 so then if you see that it's an alias if you want to find the location then you can just say type of python 3 just like we did before and then get the full location to that now the way that we've set this alias right now that will go away as soon as we restart our terminal so in order to make the alias permanent we're going to need to add that to our Bash underscore profile as well which is the same place that we just edited our path environment variable so I'm going to open that back up so that was nano dot Bash underscore profile and you have to be in your home directory then I'll just do this at the bottom so I'll go to the bottom and I will say alias Python is equal to Python 3 now if you install python 3 and it actually and stalled this as the command Python three then most likely it's using pip three for pip as well so we're also going to want to alias pip here also now if you're on a linux instead of a Mac then it may not have installed Pip with Python at all and you might have to install that through either the apt or YUM package manager depending on which version of Linux you're running but once you're sure that you have pip installed if it installed it as pip 3 then you can simply create an alias to that as well by saying alias pip is equal to pip 3 ok so now we can save these changes again by hitting ctrl X and then a Y for yes and then just an inner to keep the same file name and again we'll need to restart the terminal for those changes to take effect so I will restart that and maximize it there ok so first let's make sure pip is working so I will say type of pip we can see that it's alias to pip 3 so type of pip 3 and it's in this location here which is the same location as my Python 3 command but that might not always be the case so you definitely want to watch out for that but now if we type in pip list then we can see that works fine ok so now let's look at a couple more tips 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 on your machine now this can definitely happen if you're using virtual environments or you have a lot of different versions of Python installed so let's say you type in Python and it says that you are running Python 3 7 now that doesn't really do you much good if you have 10 different virtual environments that are all using Python 3 7 so how can we actually see exactly which Python we're running so to find this out we can use the system the standard library so if we import sis now like I said that's a built-in module so you don't need to install anything so now we can simply look at sis dot executable and we can see there where our current executable is located 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 sis not executable can help you to bug 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 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 with the path variable like we saw a second ago and again if you want to view your entire path from within the command line then you can simply do that with the echo command so again I will exit that and say echo dollar sign uppercase path then you could compare this executable location here with your path and if you're having any issues with that then echoing that out might make it obvious why it's running one Python command instead of another okay so now let me clear my screen out here so I also get a lot of questions from people who tell me that they've and pip installed a package but when they try to import that package it doesn't work so this can also be a path issue so Python and pip are two separate commands and therefore it could be located in two different places so if your path is messed up then it's entirely possible that what you're installing with pip isn't installing that package for the version of python that you think it's installing it for so for example let's say that I wanted to install Jango so I could just simply say pip install Jango and when I run that we can see that we already have this installed and it also gives me the location where Jango is installed it says that it's already satisfied and it's in this location here so that is in the cite packages folder of that Python 3 location that we saw before now another way to get this location of a package it's use the pip show command so if I do pip show Django and run that then it gives us a lot of information here but one of these is the location so we can see here it's in this site packages directory so 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 the package is actually in that site packages directory of the version of Python that you're using so if I open up Python here and I do you know import Django then we can see that works now if that didn't work then you might be confused because maybe you did a pip install and it looked like everything went fine but in that case you want to do that pip show of the package and make sure that this location here matches what you get from that sis not executable so I'll say import sis then pronounce sis not executable and we can see that we have this location here and that's also where it's looking for our Django package in our site packages directory so we know that that's going to work if those are in two different locations then that gives you your answer to why your package isn't importing correctly because that would mean that your pip is actually installing that package to a different version of Python and again just like with the other problems that we've talked about in this video so far that would be a path environment variable issue and you would have to debug that and figure out you know why those commands are not running like you think they should and you might need to rearrange those directories in your path environment variable ok so now let me exit this and clear the screen now I think that that's everything 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 that 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 is not so for example sometimes people will tell me that F strings aren't working for them and that they have Python 3. 6 installed well most likely that means that your editor just isn't using Python at 3:6 even though you installed it and a good way to determine that would be to compare that sis not executable value that we saw before so some editors might have a different way of determining which version a Python that they're using instead of just looking at your path now in sublime text they use something called build

Build Systems

systems and I have a completely separate video on how to set up sublime text build systems but understanding what we covered in this video here should help you better figure out which directories you'll need to add to those built systems in order to get them properly set up and you may need to do something similar with other editors or IDs as well depending on what you're using but really quick let me show you a fast example using sublime text so I'm going to pull up my sublime text here where I just have a simple file now I'm not gonna go into too much detail here since this isn't exactly a sublime text tutorial but you know this is something that you could use to debug other editors and IDs as well if it's not using the version of Python that you think it should be using so really quick I'll just show you how I've set this up on mine to switch between Python 2 and Python 3 so I have a demo file here where I'm just printing out the system and the Cystic you table so if I run this right now then you can see that right now it's running Python 3 7 and it's using the executable that we saw before from that library frameworks 3. 7 bin folder but what if instead I wanted to use a different version of Python or Python from a virtual environment so to do that I would need

Create a New Build System

to create a new build system so if I come up here in sublime text and go to tools and then build system then at the bottom here we can see that I can go to new build system now if you create a new build system then it's basically just going to give you a JSON document where there are a couple of things that you need to set now one of those will be which Python executable that you want to use so I am NOT going to do a new build system now I already have one created here you can say I have a Python a27 here so let's say I wanted to use Python 2 7 well I have that build system opened up and we can see that I have a couple of different things set here now again I'm not gonna go into this fully since I have a completely different video on this but you can see here that within my command I there is a place here where I'm passing in the full path to my Python 2:7 command here and the user bin directory and that is what runs Python at 2. 7 so now if I go back to that same file that gave us Python 3 7 before and I changed this build system to be Python 2 7 instead if I run that then you can see that now our version says 2 7 and our executable says user bin Python and technically we should change the syntax of that print statement to be Python 2 7 at this point but that's ok as is for now it's just a demonstration so in this example we set this up to where the executable points to you know Python 2 but you could also set that up to set to you know a virtual environment or anything like that speaking of which lastly I would also briefly like to talk about virtual

Virtual Environments

environments now if you've never used a virtual environment it's basically a way to have separate Python environments per project and that also includes separate Python executables and packages now you might be wondering if you'd have to go through this whole path process every time you activate and deactivate a virtual environment but in the command line you won't have to do that when you activate and deactivate a virtual environment and the command line there is usually code included that automatically adds and removes those directories to the beginning of your path for you so you shouldn't have to worry about it so for example let me pull back up my command line here and let me activate one of my virtual environments this is a condo virtual environment and this is what I used in my flask series so to activate this environment I can do source anaconda and that is with within a bin - activate and the name of that environment was flask blog so if I run that then we can see that now we get this blast blog in parentheses so we can kind of tell we're in a virtual environment but now if I say which Python then you can see that my Python command is now pointed to this directory here - this executable it's within this anaconda and var mence flask blog bin directory so that means that directory was automatically added to my path so just to test that let me echo out my path so I'll do echo path and we can see that bin directory for that flask blog environment is at the beginning of my path now so that is the Python command that it sees now actually my Python command should still be aliased to Python 3 and it is but that directory also here in this virtual environment also has a Python 3 command so that is why that is working for both of those now again if I wanted to be absolutely sure that it was using the correct executable then I could run Python and then import sis and look at that sis not executable like we saw before now when it comes to using virtual environments with different IDs or different editors so for example using an editor like sublime text or anything else then you'll just have to do a search online and see exactly how to set those up in order to use the environment within that editor so in sublime text it's the build systems that we saw here so you just have to go in and create a build system using the Python executable for that environment but I'm sure that it's different within 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 ok so let me clear out my screen here ok so in this video we've gone over one of the most common forms of 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 these same places now when it comes to getting started out in computer science in general that same concept applies the most common questions I see there have to do with the fundamentals 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 problem-solving 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 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 there are 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 they're 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 support my channel and learn more about brilliant you can go to brilliant org ford slash 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 /c m/s okay so I think that is going to do it for this video 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 versions and 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 ways 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 a description section below be sure to subscribe for future videos and thank you all for watching you

Другие видео автора — Corey Schafer

Ctrl+V

Экстракт Знаний в Telegram

Экстракты и дистилляты из лучших YouTube-каналов — сразу после публикации.

Подписаться

Дайджест Экстрактов

Лучшие методички за неделю — каждый понедельник