Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Integrating Poetry Into Existing Projects

00:00 In the previous lessons, you’ve seen how to create new Poetry projects and manage dependencies with it. In this lesson, I’ll show you how to integrate Poetry into existing Python projects.

00:11 If you have an existing project, you can have Poetry generate the configuration files there instead of creating a new parent project directory. You can pause the video now and create a simple directory with a Python script to follow along.

00:26 This script can be any Python script. In my case, it’s just a script that runs a print("Hello, World!") program. But the same principles will apply with large existing projects that Poetry doesn’t initially know about.

00:39 If you now have an existing project folder called existing -project in my case, or whatever you have, instead of creating a new project from scratch, you can use the poetry init command to convert your existing folder into a Poetry project.

00:54 First, cd into the existing project and then run poetry init.

01:01 This command will guide you through creating the pyproject.toml file interactively. Poetry will ask for key information about your project, such as the package name, the version, the description, and authors, things that we have seen when we explored the pyproject.toml file when creating a new project.

01:20 It even gives you sensible defaults so you can simply press Enter to accept them.

01:27 And if you need to enter a value, just enter the value.

01:36 And then it will ask if you’d like to define your main dependencies interactively. But now I’ll choose no. Also, if you’d like to define your development dependencies interactively also, I’ll pick no.

01:49 You have a final confirmation asking if you’d like to proceed to generate the pyproject.toml file. And here the default is yes, so I just press Enter.

02:00 After answering the questions, Poetry will generate a pyproject.toml file for you. Here’s an example. And now that you have your pyproject.toml file, you can start using all of Poetry’s features.

02:14 For instance, you can run your script within your new isolated environment. To do so from your terminal, you can run poetry run python hello.py, which is my existing script.

02:28 Poetry will now automatically create a new virtual environment if one doesn’t exist. Then, it executes your script. In my case, I just see the display, Hello, World, which is the expected outcome for my program.

02:42 By following these steps, you can convert any existing Python projects into a Poetry managed project, allowing you to take full advantage of Poetry’s powerful dependency management.

02:53 In the next lesson, you put together all we’ve learned and wrap things up.

Become a Member to join the conversation.