Setup VS Code for Python with Pyenv and Poetry

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] welcome everyone before we dive into lots of python programming and automation i want to make sure that you have a solid setup for your python development when starting out it's probably okay to just install python from python.org but as you get into more advanced projects and ultimately creating projects used by many this simple installation actually gets in the way so i want to show you how to set up your machine for professional python development using pym poetry and vs code all right so step one is to install pyemv now i'll include this url inside of the notes in the video but this is where you want to get to github.com if we scroll down here you can see that it has everything that you need to know about installing setting it up and even goes into detail about how things work under the hood if you're on a mac like i am then all you need to do is brew install pyenv and that will install pym to your machine then to install any version of python you say pym install and then the version you'd like to install let's say you want to do 3.8.6 or you want to do 3.7.3 or 3.9 you get the point you can install any one of these now once that's installed you can check which versions you have on your machine so check this out when i print out all of my versions i have my system version which would be the 2.7 that mac comes with but then i also have three others a 3.7.3 3.8.6 and 3.9.0 so i have three different interpreters installed here however you do see this little star next to my 3.8.6 that's because i set it as my global interpreter just like this now instead of my machine or global interpreter being 2.7 when i do python-version i now get 3.8.6 which is the one that i set globally some of you may be familiar with having to do something like python python3-version but with pym i no longer need to do this it's as easy as me saying pi m global let me set my 3.9 and then when i do pi inversions again check it out now 3.9 is my global version and it's just as easy to set it back to what i had before look how simple this is oh yeah so my recommendation is to go to this url github.com slash pym pym and follow the instructions here but if you're on a mac then it's as easy as this brew install payment is the first thing you do and then you can add this snippet right here to your shell configuration in my case i use z shell so i would add this snippet to my zshrc file and just so you know this snippet is posted during the installation process so once you install pym they will actually show you this again so no need to like memorize this or copy it from the video they'll actually post a snippet right after the installation all right next up step number two is to install poetry poetry is going to be the package manager that we're going to use so you just go here to pythonpoetry.org and you'll get this really cool looking landing site it goes over quite a bit as well as all the installation guides and everything that you would probably look for in the documentation to do things like setup configuration and how they do a bunch of other stuff but we're not really going to focus on this part really what we're going to do is just install it but instead of using brew to do it please do not use broom if you use brew it's going to install it to your machine again and the problem with that is we're not taking advantage of pi m's ability to switch between interpreters so instead of using brew to install poetry let's just use pip to install it so we'll say pip install poetry because we used pym to set the global interpreter we don't need to say pip 3 install poetry we can just say pip install and that's going to be installed to the global reset interpreter that was a pretty simple one but let's go over it anyway so step two is to install poetry using pip install poetry then we'll add this line or this code snippet to your shell config just like you did in step one if you're not using unix then make sure to check out poetry's documentation to make sure you install it correctly for your os all right with that the very last thing we need to do is set up vs code to use this as you can see i created an empty project here in my dev folder called pi m poetry example and we're going to be building a project from scratch in here so you can see how everything gets set up to get started i'm going to use poetry init press enter and poetry will start with this wizard or this guide to create everything for you so what's my package name the default is in square brackets and i'm okay with that the version yep that's all right don't need a description right now i am the author no license right now and yes any version above 3.8 will be compatible with this project would you like to define your main dependencies interactively for now i'm going to say yes or i just press enter as well for the default this part of the wizard is asking what my main libraries are in this case because i'm going to be doing some web and api automation with python i'm going to use pylenium io it's then going to query pi pi and ask which packages match my search of polynomial there's only one so i'm going to press 0 and then enter but you'll see that there's going to be more listed later on for now 0 enter let's add polynomial to our main one do you want to add any version constraints so we could say i need pylinium io to be a specific version or greater than a specific version in this case i'm just going to press enter get me the latest if i have any other packages i want to add i can keep going but in this case piloting my ow is the only one i care about so i'm just going to press enter to skip all right next question is would you like to define your development dependencies interactively and i'm also going to say yes here so enter for the default the first package i want is let's do auto pep 8 auto pepe is a style guide and a formatter so i'm going to install this one there's a lot of hits this time but the one i care about is position 0 the very first one so 0 and enter any version constraints nope just give me the latest so i'll press enter again for the next package i'm going to use flake 8. flightgate is essentially a linter so let's do that one flake8 and i also want the first one here so zero for the first option and add that skip the version constraint and i don't have any more packages i'm going to press enter here you can actually see what it's going to generate here this is the file that's going to happen i like everything i'm seeing so i'm just going to press enter for yes and check it out we got a pi project automo file created in our project if we open it up sure enough we see everything that we specified in here and that happened automatically very cool right now that we've specified everything that we want in our project we can now install all these packages and get this project going so inside my terminal i'll say poetry install and it's going to look for this pi project.tamil file to install everything from it step one you can see that it's creating a virtual environment for me and then resolving the dependencies and look how fast it goes poetry does things asynchronously instead of doing it one by one so it's a lot faster and that was it over here on the right hand side it took about 25 seconds if i was doing this with pip it'd probably take a little bit longer but anyway what we want to do next is tell vs code which interpreter we're going to be using for this project as you saw before poetry already created a virtual environment and is managing it for us we can check this out with poetry and info and get the path i want to show everything in info so you can see just how much this is keeping track of for you but for now what we really care about is this path so if you ever wanted just the path you could say m info dash dash path and grab this the same one you saw above right here all right so let's grab this let's copy it because we're gonna need this for this next step at the very top here click on view and then command palette there's also a keyboard shortcut for that but open the command palette and then let's configure python or select the python interpreter this first one here so click on that i hope this goes without saying but in order for you to python vs code you will need to have the python extension installed so if you didn't see that select item in the list that means that you don't have the python extension so make sure you go to the extension marketplace find python and also find pylance and install those too then try finding the select interpreter from there none of these are the ones that we want we actually want to do enter interpreter path click on that and then paste in the path that we copied from poetry and press enter hopefully you noticed the change there a few things happened in the bottom left corner the interpreter actually changed to my virtual environment also i got this vs code folder because we actually have a setting now in our project for vs code to know hey use this virtual environment and this python interpreter for this project sweet so next thing i'm going to do is inside of my project i'm going to make a test folder or not a test folder just tests there we go and inside of here let's make our very first test and i'm just gonna call it test example.pi let's make a super easy example let's just say like test sum and we're just going to assert that one plus one is equal to two but wait i don't get anything up here saying to run or debug my test that's because we haven't told vs code which testing framework we're going to be using so just like we did with the python interpreter we're going to configure the tests so let's open up the command palette again and this time let's find if i can spell right configure tests and for me it's this first option python configure tests pilonium io comes with the pi test framework already built in so we're going to use pi test and i'm going to select the test directory that's going to hold all of my tests boom lots of things happened a lot stuff got generated let's check out settings.json again and yep sure enough we got a bunch of new stuff in here showing that our pi test is enabled i'm also going to use some exclude files here because i don't like seeing that pi test cache and pi cache everywhere so let's exclude some stuff files dot exclude and let's get rid of dot pi test cache and now it's easy for us to click right here in our test we have run test and debug test hopefully you're familiar with these already let me put a break point here and then debug the test we'll see it stop right here we can play and continue or step into or all the good stuff is all in here now so we can debug the test or we can also just run the test and mer one plus one is not equal zero it caught me because i had a two before and it went back to a zero when i was trying to delete that but good job test it caught my mistake so we now easily change this to a two about a being badaboom run the test again let's make sure this thing passes some check marks let's go amazing see not bad at all right so we now have our interpreter set up for the project we have our test framework set up for the project let's see what else we got to set up ah yes how about linter for a linter we were using flake 8. so let's click flake 8 here let's set that up back to our settings.json you can see that these got enabled as well so yes you can install your own linters your own formatters if you want to just leave it all to pylance which is microsoft's language server for python you can also leave it there as well and just have pylons do all the heavy duty lifting but some people like to have or specify their own linters and formatters but that part's up to you at least you know how to do it now right the very last thing i can think of to do without getting into more like personal preferences and configurations is to set up pilonium that way we can write a really simple ui test i'm going to kill this process completely and reopen it oh and i want you to see what just happened via's code is now very very aware of the virtual environment that we created with poetry right so it sourced it activated the environment and let me clear this so it's prettier you can actually see that we're inside of the virtual environment now i'm using powerline 10k that's why i have these pipes and all this fancy stuff but for you you might see something as simple as them on the left-hand side of your shell that also means you're inside of the activated virtual environment and that's a good thing so if you're already inside of the virtual environment then all you got to say is pilenium so for now let's make sure we have the right thing pilenium dash dash version there you go we have version 1.12.1 and to initialize pilonium all it is is pilonium in init however i also want to show let me deactivate and get out of the virtual environment i also want to show that you can use poetry to run these commands as well this is really helpful when you're not inside of the virtual environment you may be inside of a terminal or maybe you're using vim or something like that uh you don't have to be inside of the virtual environment you can actually run commands directly into the virtual environment so instead of saying pilenium version directly in the virtual environment we can actually send commands using poetry run so poetry run pilonium version we get the same result but we're outside of the virtual environment there are pros and cons to each one of these approaches so i just wanted to make sure that you are aware that you could do either one if you're inside of the virtual environment already awesome run just pilenium init or pi test dash k this thing just run commands like you normally would if you're outside of the virtual environment you can just say poetry run followed by the command you want to send to that virtual environment okay so poetry run pilenium init perfect we get some files created for us a pylonium.json a conf test and a pi test any this right here this polynomial json is just our configuration file right now you can see it's defaulted to chrome let me write a simple test using this def test google let's say i'm sure this is something you've all seen me do already from pilonium.driver import pilonium the one with the big p we don't want the little p there we go if you're familiar with pycharm you usually don't have to import things like this because you would automatically get intellisense because pycharm has pi test support so it would know exactly what pi is it knows it's a pilonium object in via's code it doesn't so to give it some help we're being very explicit here saying the pi fixture is of type pilonium so then we get that intellisense so now i can say things like pi.visit and you see that i get this awesome auto suggestion let's go to our google website that we love so much let's search for i don't know i like doing puppies so let's let's do that let's keep let's keep searching for our cute little puppies or the peepees we're going to type in puppies to the search field and then let's get the btnk element that way we can submit said search dot submit there we go and then one quick assertion let's make sure that uh the title pie should contain title of the puppies not too bad right i mean i did this all from memory so let's see how good i actually did let's click run test let's check it out huzzah we searched for puppies and there you go so i hope with that you were able to see everything it takes in order to set up your machine for professional python development and also set up vs code to use these things so i would like to recap one last time what we went over we checked out pi m to manage our interpreters and make it easy to switch between them we installed poetry as well which is a package and dependency management tool to make it really easy for us to create virtual environments manage them automatically create these pipeproject.tamil files all that good stuff and then the last thing we did was show you how to set up vs code to use this interpreter use your test framework as well as any formatters or linters that you would like to use as well if you found this video helpful please leave a like or comment on anything else you would like me to cover whether it's with python or any other tool or language or anything like that please let me know in the comments below and share it with your friends y'all let's do some python development and some test automation with python thanks for watching and have a good day bye [Music] you
Info
Channel: QA at the Point | Carlos Kidman
Views: 5,890
Rating: 5 out of 5
Keywords: qa, quality assurance, software testing, automation, qa automation, selenium, testing, testing framework, carlos kidman, qa on point, api testing, katalon studio, robot framework, devops
Id: 547Jr26duHQ
Channel Id: undefined
Length: 20min 57sec (1257 seconds)
Published: Wed Jan 27 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.