My Python Development Environment Setup - Full Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
foreign I will share with you how to set up your development environment for writing python code I myself have been writing python code for over 10 years I've been teaching python code for about five and I have messed around with pretty much every tool that you can imagine so what I'm going to show you here is personally my environment this is what I recommend and use on a daily basis and that's going to include all of my tools for virtual environments my code editor my formatters my autocomplete my extensions pretty much everything I use on a daily basis so with that said let me show you how to set this up after I set up the Segway to our sponsor before we get started I need to thank lenode for sponsoring this video I've been using and working with lenode for nearly three years now and they've consistently been my go-to choice for hosting my applications and servers in fact right now I'm using the node to host the official Tech with Tim Discord bot a website multiple of my domains and a kubernetes cluster now lenode was just recently acquired by Akamai and as a part of this acquisition they're going to be adding more than a dozen new data centers around the world in the next year lenode makes it super easy to spin up servers with their one-click app Marketplace and have tons of written guides and YouTube videos on their channel to help you get started now if you're having any issues you can reach out to the 24 7 support team and actually talk with a real human being now the nodes pricing is simple with no hidden fees and full transparency and you can test out the node services for free by using the link in the description and claiming in a 100.60 day credit for any new accounts it's been an absolute pleasure to work with lenode over the past few years and one last thank you to them for sponsoring this video alright so let's go ahead and dive into this now just as a quick table of contents I'm going to show you what I use for virtual environments what I use for my code editor what I use for my formatter for my autocomplete for my terminal and then all of the extensions and actually a bonus tool at the end that you guys can hopefully find some value in so let's get started here the first thing we need to set up for our development environment is something that can handle virtual environments now if you're not familiar with virtual environments this allows us to have multiple python versions installed on our computer and different versions of those versions separate environments for each project that we work on this way we can separate out the packages that we need and kind of keep our dependencies separate and nice and clean it is not a great idea to only have one version of python and just dump all of your dependencies into that oftentimes you need to switch between different environments that's we'll use a virtual environment for so the virtual environment software I use is called Anaconda there is a built-in one called ven V or vnvv I don't know exactly how you pronounce that anyways I like Anaconda this is the one I've been using for a very long time and I'm going to recommend that you download it now I'll leave a link to the download in the description however there is a few notes to keep in mind while you're installing this software specifically I'm going to bring up a tab here you want to make sure that when you go through the installation process right here is where it is you click this button let me zoom in that says add Anaconda to my path environment variable now it tells you it's not recommended to do this but I guarantee that for 99 of you you want this on your path and having it on your path allows you to use this command directly from your normal terminal rather than having to open up the Anaconda terminal so make sure you add this to your path if you don't add it to your path or you already have it installed I will leave this link in the description that shows you how to add this to your path um right here see add Anaconda to path if you don't already have it installed if you're not sure if you have it on your path or not then what you can do is open up any random terminal so it doesn't matter if your terminal command prompt and you can simply type conda and if condo works you have it on your path if it doesn't then you should add it before going through the rest of this video alright so at this point in time I'm going to assume that you have Anaconda stult regardless though listen to this next minute or two because this is going to be important to make sure you don't mix up your different python versions so many of you very likely have multiple python versions on your computer and you have that because you've gone to the python website and you've downloaded multiple versions of it so maybe you downloaded 3.9 a year ago and then you downloaded 3.11 now where you downloaded 3.7 five years ago or whatever it was either way chances are you have another version of python on your computer now the way you can check that at least on Windows is you can search for idle in your windows kind of search bar here if you're on Mac you can go to the spotlight search and Linux probably a similar thing you can see here though that I have idle which is kind of the default code editor that comes with python from the python website and I have two of them here right 3.8 and 3.10 now what this is telling me that is that I actually have two python interpreters on my system that are different versions and this means that I may be installing dependencies into the wrong version so I would highly recommend that you uninstall all of these python versions unless you know what you're doing in my situation I know what I'm doing with these so you don't mix them up in the future just keep that in mind you may have multiple python versions and you may be installing packages into the wrong version now if you don't have any of these idle packages and you added conda to path chances are that the python version you're going to be using when you type the command python or python3 depending on your operating system is going to be the python provided by Anaconda now Anaconda that has something known as a base environment that base environment is what you use by default no matter what now if you change the environment then you can use a custom environment but for any of you that don't have additional python compilers or sorry interpreters the python version you're going to be using is the base version or the base environment from Anaconda I know this is a little bit confusing but I just want to make this clear because this is the biggest mistake everyone makes when they set up their python development environment really what you need to understand is which one you're using at any point in time so I'll show you how to set that up but first let's look at how to create different virtual environments using Anaconda so the first thing I want to type here is condom again at this point it should be installed and added to your path once we've typed conda and we know that this command is working we can create a virtual environment so let me make this full screen so to do that we're going to type conda create dash dash name and then give our virtual environment a name in my case I'm going to give mine the name of programming expert which is the best platform to use if you want to learn how to code it more about python check it out from the link in the description and then I can alternatively specify what the version of python is that I want to use for this virtual environment so I can say python is equal to and then give it a version 3.9 3.8 any valid version so I'm going to go 3.9 here when I do 3.9 it is going to download this python version for me and install this in a new virtual environment now keep in mind these virtual environments can be fairly large so you don't want to create too many if you don't actually need them so I'm just running through the installation steps here once it's done I will be right back all right so now that this has been created what I can do is use this virtual environment right now notice when I type python I'm using python version 3.10 you can see that from right here now that's not the version that I use to set up my virtual environment I use 3.9 so if I want to use this virtual environment I need to type the command that's right here conda activate and then I'm going to type programming expert okay so that's the name of my virtual environment and then you're going to see kind of a parenthesized name of your environment and now this terminal is going to be using the python version from this new virtual environment so now if I type python notice I have my version 3.9 now this also means that if I want to install any packages and I use a command like pip or pip3 which should just be working by default in this environment it's going to install them in 3.9 not in my 3.10 version so hopefully that makes a bit of sense but this is where you would kind of separate out your different dependencies for different projects and create different virtual environments for any different project that you're working on that has well different dependencies okay so that is kind of the basics here there's a lot more you can do with conda but I imagine for many of you all you're going to need to do is set up a python version install some dependencies and then you need to know how to use it and stop using it so if I want to stop using this I'm going to type conda deactivate and that's going to remove me now out of this environment now let's say that I want to now get into the base environment for Anaconda again for any of you that don't have other python versions installed that's what you're going to be using by default otherwise though for me if I want to be in the base version of Anaconda I need to type conda activate now this again brings me into the kind of standard python version for Anaconda and now if I type python I'm using python 3.8 which is what came with the Anaconda I installed a very long time ago right back in 2020. anyways I'm going to quit python here and I'm going to type deactivate so it's just important that you understand the difference here you have your base environment you have any custom environments that you create and you have any other python interpreters on your system and you want to know which one you're using and don't get mixed up with your dependencies all right so sorry for the quick cut here but I'm just going to run you through a few useful conda commands you may want to use so first one is conda info dash dash envs this is going to list out all of the different environments that you have on your system pretty useful to know and then another one you may want to use is deleting an environment so to do that you're going to use condo or condo sorry EnV and then remove and then dash n for name and then the name of the environment you want to remove so let's say I want to remove say programming expert and then that will delete the environment okay that's really all you need to know about Anaconda there is also a graphical user tool you can use that kind of shows you some information so you can open that up if you want but for me I always just use it from the terminal now let's head over to vs code and see how we can use these conda environments all right so I've opened up visual studio code this is the code editor that I recommend for python development yes there is a million other ones that you can use but the one that I always use is Visual Studio code it's also just good to get practice with this because if you ever work in another language chances are you're going to be using visual studio code so I would highly recommend just use this you know get away from pie charm get away from those other ones use vs code that's my recommendation after 10 years of Python Programming anyways how do we set up vs code it's actually very very simple to set it up for writing python code once you install vs code there's a little uh kind of extensions button here so click on extensions and you can see I have a bunch right I've already installed a bunch of extensions for you guys what you're going to want to install is the python extension this is provided by Microsoft fun fact I actually worked on this extension uh when I worked at Microsoft has almost 70 million downloads now this is a very good extension and this pretty much sets everything up for you automatically so install this python extension you might have to like restart vs code and now we can actually create a python file and I will show you how to run it and change The Interpreter that you're using so what I've done here is I've just opened up vs code notice I'm not in a folder or anything like that and if I get out of full screen mode which you can get into by pressing f11 on your keyboard I can go file and then I can open a folder for example if I want to start working in some kind of project so I'm going to type open folder I'm going to go to my desktop and let's just make a folder and call this tutorial okay uh of course I have a tutorial folder uh let's rename this to python EnV okay so this is typically how I start a new project I open up a folder inside of this folder I can write anything that I need okay now I have a folder open if I want to create a new file I can press this button and I can do something like test dot pi all right I've got a python file open let's type print hello world and what you're going to see now just zoom in a little bit is that we have this button up here in the top right hand corner that will run our python code for us we also can run and debug I'm not going to talk about that right now okay so now the question becomes when I press this run button what interpreter is going to be used am I using 3.9 am I using 3.11 am I using the conda base interpreter and that's going to be different for all of you the way to change this once you have the python extensionist installed sorry is to hit control shift and the letter P on your keyboard and then to type the following Python and then select interpreter so I'm going to hit select interpreter and you're going to see here that it gives me every single one of the virtual environments that I have this is done by the python extension and it shows me every single interpreter here so this is where you select the python interpreter you want to use now notice the default one is 3.10 you can actually set this by kind of clicking on this button here and changing what the default interpreter path is anyways I'm not going to do that here but you can see that you can change which one you want so I can use for example 3.10 and then I can run this and it will run this using python version 3.10 now of course you could also just go in the terminal and you could activate the virtual environment you want so I can say activate uh I think I had one called say Robert and then I could say Python and then test dot pi and I can just run it myself in that environment however I know a lot of you want to use this button so that's how you change The Interpreter there you go that's all there is to it that's how you use vs code for python font development alright so now for the next part of this video I'm going to show you how to set up python formatting now to do this from vs code you can hit control s and it should actually just give you a um kind of pop-up here when you try to save a file that requires formatting like mine does right now and it should say do you want to install black or do you want to install Auto Pep 8 or one of these python formatters now this command never works for me but for you guys it might and what you can do is hit yes or change it to use one of the different formatters personally I like using black as my formatter but it doesn't really matter Auto Pepe is going to be the same and I've never used this Yap F1 so I don't know use whatever one you see fit I'm sure they all do a very similar thing okay so this pop-up though for some reason you don't get it showing up right when you're inside of a python file and you're saving hit Ctrl shift p on your keyboard and then type format and then document with then hit Python and if you don't have a formatter installed it should pop you up here with this pop-up and then you can press this button now for me because I have kind of of a messed up environment because of all the tutorials I do you can see this doesn't work for me so I actually get a mistake now I'll show you how to fix this error if this pops up for you so I'm just going to clear this here first I'm going to change into an environment I want this formatter for so I'm going to say let's go conda activate and let's actually activate our base environment then inside of here I'm going to type pip install black and that will install the formatter for me okay so I'm going to install that inside of here notice it's already installed in my base environment and that's fine now if you're on Mac or Linux you may need to use the PIP 3 command just keep that in mind okay so now that black is installed I'm going to change my python interpreter I can do that by hitting down here as well by the way uh to my base environment which is this one now that I'm in my base environment if I save I should get Auto formatting and you can see that I do it automatically formats my data here for me alright so that is formatting one last thing you should have a look at is the formatting kind of configurations so I'm going to go to the settings in vs code and I'm going to type format here and you can change a few of the options here so that you format when you say paste something in I hate that that really annoys me so I turn that off or you can format on Save I put this on so every time I hit save it automatically formats the file you also can manually invoke a format by doing Ctrl shift p and then you can say format document or you can save without formatting the document which is what I do quite a bit alright let's move on to the next topic all right so the next topic is autocomplete now this is very important especially if you're writing a lot of code and by default the python extension does have an autocomplete which I believe is called Jedi it might be different now either way there is a default autocomplete for almost every programming language based on the extension you install in Visual Studio code now I don't like the default one so instead I use tab 9. now this is not a sponsorship for top nine this is a paid product I pay for this they do have a free version as well but this is personally what I recommend for an autocomplete especially if you code a lot I think it's like 20 bucks a month or something anyways I use this that's what I use you don't have to use this okay so if you want to use that you can download it um I'll even let you guys find the link on your own so you don't think I'm giving you an affiliate link or anything like that and to see tab 9 you can see that I have it activated here in Visual Studio code and if I click on this it gives me kind of like some stats and stuff about this all right anyways there you go that is tab 9 now if you don't want to use tab9 which most of you will not be using because you don't want to use a paid version you of course can use GitHub copilot there's a little button here that you can click on for kind of activating copilot now I don't have copilot hence why I'm not using it otherwise I probably would other than that if you want to set up your autocomplete you can go to settings and it's actually called intellisense so if I type intellisense here something should be popping up all right so the option here is actually under language server so if you go to Python language server you can change the language server which is what provides your intellisense or the autocomplete for you you have Jedi pylance which is a more advanced version actually now that I remember hearing about it so you can change it to pylance and then you'll be using that by default for your um what do you call it your python autocomplete anyways you shouldn't really need to configure this it should be set up for you but I just wanted to let you guys know I do use a special autocomplete I think it's worth it but it's up to you all right so now we're moving on to the last section which is kind of extra extensions or let's call it like a bonus now for visual studio code you can see when I go to this extensions pane here I have a lot of extensions now I work in a lot of different programming languages so that's why I have so many so some of the key ones I have if I kind of go through them here is code spell checker I love this one because I make a ton of spelling mistakes and this really saves me quite a bit from pushing in kind of mixed up variable names or comments or something along those lines so I would highly recommend you download the code spell checker all of these you can just search and you'll find them yourselves I have the Django extension this gives you some Snippets and some more syntax and kind of code completions for Django and then I'm trying to find any other python related ones I have a Jinga now if you ever worked in Django then you probably want the Jinga extension which makes writing your Jenga HTML a little bit more readable and I think that's about it um I have this prettier extension this works for other code files unfortunately not for python this will automatically format say JavaScript code or Json code so it's nice because oftentimes I'm working with Json files or other types of code of course I have the tab 9 extension and then I have a WSL as well which actually a lot of you will probably like so that you can have some Linux like commands inside of Windows and I believe if you use Powershell which is one of the terminal options you can use in vs code here by just going here and selecting Powershell so where it says new terminal just hit Powershell uh you'll get access to a lot of WSL commands I think that's by default now in Windows but don't quote me on that all right so with that said guys I think I'm gonna wrap up the video here this was really all I need to show you for my python environment setup the most important part being the interpreters once you understand the interpreters it's very simple to you know kind of mess with your python environment change things around but it's definitely a big headache if you don't understand that you have multiple versions on your system and you're using the PIP command and you have no idea where you're installing packages so with that said I'm going to wrap it up here I hope you guys found this helpful and I look forward to seeing you in another YouTube video [Music] foreign [Music]
Info
Channel: Tech With Tim
Views: 184,374
Rating: undefined out of 5
Keywords: tech with tim, learn python, python, python tutorial, python projects, python programming, learn python programming, python for beginners, python tutorial for beginners, python course, how to learn python, python tutorial project, getting started with python, python beginner projects, python projects for beginners, python programming tutorial, python crash course, python development, code editor, code formatting
Id: qI3P7zMMsgY
Channel Id: undefined
Length: 20min 38sec (1238 seconds)
Published: Mon Nov 28 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.