Visual Studio Code for Python Developers Transcripts
Chapter: Managing Environments
Lecture: Using Poetry with VS Code
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
So far in this module, we've seen a few different tools that you could use to manage your Python environments.
0:07
But there are tons of others available that could really help boost your productivity. So far we've seen tools like pip,
0:12
but there's also pip tools which adds a lot of additional functionality. Pipenv and poetry are really great for not only installing
0:19
packages but also managing your virtual environments. Regardless of whichever one you choose, you'll be able to use them inside of
0:26
Visual Studio to help boost your productivity. Why don't we go ahead and take a look at one of them?
0:30
Let's go ahead and get started with using poetry inside of VS Code. Now, the first thing I got to do is install it.
0:36
Since I'm on a Mac, I'm going to use Homebrew. Now, I'll be honest, I installed it already so I'm just cheating just a little bit.
0:43
The next thing we need to do is initialize our project. So I'm going to type in poetry in it,
0:47
and now it's just going to walk me through a few prompts I need to answer. So I'm going to give it the name of my package which is Podcast API.
0:54
I'm going to accept the defaults for the version, the default description, author looks good, I'm just going to leave all that as defaults,
1:02
Python 3.11, that looks good. Would you like to define your main dependencies interactively? Yeah, sure, let's do that.
1:10
Now, the first one I'm using is TinyDB, so let's go ahead and put that in. I want to select that first option,
1:19
and I don't have any version constraints, so I'm just going to hit ""Enter"". And the next one I'm going to do is FastAPI.
1:25
And this one, I'm going to add that all parameter, so I'm going to get all the good stuff. I'm going to select the first option for that again,
1:32
no version constraints, and that should be it. Do I want to install any development dependencies?
1:36
Now, this would be good if I was installing anything for like a testing or anything I didn't want for production.
1:40
Now, I don't have any of those right now, so I'm just going to type ""No"", confirm that the generation looks good, that looks great to me,
1:48
and then now notice we have this generated PyProject.toml file. If I kind of move away our command line, it's going to say ""No, you can't do that"".
1:54
So I'm going to hit ""No"", and then I'm going to hit ""No, you can't do that"". And then I'm going to hit ""No, you can't do that"".
2:00
So if I move away our command line a little bit, you can see some of the things that it went in and set up for us.
2:04
So it has like a build system and the dependencies, and some general project information that we're going to use.
2:08
Now, I'm going to go back to the command line, let's clear it out a little bit. Now, we have our packages installed.
2:14
So the next thing I actually need to do is create a virtual environment. Now, Portrait does this really cool thing
2:20
where it can create virtual environments in like a common space, or it can create it inside of your project.
2:26
Actually, I want to create it inside of my project, so I'm going to run some configuration steps really quickly.
2:30
And so I'm going to do ""portrait-config in ""project-true"". So that means that I want to create the virtual environments inside of my project.
2:38
I don't want it to create it in some other directory or some other part of my operating system. And now the next one I'm going to do is portrait-config
2:46
and I'm going to do ""create-true"". Notice that I'm also passing that -local"". -local"" means that I want to save those
2:54
configuration settings to this local folder. configuration settings to this local folder. So these are not just the configuration settings,
3:00
these are not just the configuration settings, these are not just the configuration settings, these are specific to the project that I'm working on.
3:04
So if I move away this command line really quick, you'll notice I have a ""portrait.toml"" file and this defines, well I want
3:10
these virtual environments in the project and I want you to create them in case it doesn't exist.
3:14
and I want you to create them in case it doesn't exist. So we have our dependencies defined in pyproject.toml and we also have our ""portrait-config
3:22
inside of a ""portrait.toml"". So the only thing really left for us to do now is actually run our project.
3:28
Now what I actually want to do is activate our ""portrait"" environment and I'm going to do that by typing in ""poetry shell"".
3:32
and I'm going to do that by typing in ""poetry shell"". Now notice it's created our virtual environment and as you can expect,
3:38
since it's created within our workspace, VS Code is popping up on the right side of the screen asking, ""Hey, you want to use this virtual environment?
3:44
asking, ""Hey, you want to use this virtual environment? Sure, why not. Now I'm going to open up another tab
3:50
and notice how VS Code has gone ahead and has activated that virtual environment for me. I'm going to clear that out really quickly.
3:56
And now let's go ahead and do poetry install"". This is actually going to install those packages that are listed inside of that portrait.toml"" file.
4:06
One of the great things I love about this is not only does it install the packages that we have listed, but it also installs our current project,
4:14
which is pretty useful when we want to do things like testing with pytest a little bit later on. Now let's just go ahead and run our project.
4:20
I'm going to do ""python -m podcast-api Oops! python -m podcast-api And there you go. Our project is working. Looks pretty good.
4:32
Now you saw how we could create virtual environments and manage dependencies with another tool called ""poetry"". And it all still works great
4:38
inside of VS Code. One of the things I would want to point out though for folks that are a little bit more familiar with Python,
4:44
you might notice that this pyproject.toml"" file is a little bit non-standard in comparison to the one that's officially supported by Python.
4:52
And that's totally fine. In future versions of ""poetry"", I believe they are going to update to use that more standard version.
4:58
Right now I'm using the ""pyproject.toml"" version and using ""poetry 1.5.1"". So, still a great tool, but
5:04
just something for you to take into consideration. Thank you.