EDMON SEBIT

Full Stack Software Engineer





Home Page Laptop Photo

Let's Build Web Applications Together with Python

Hello, I'm Edmon - a Python software engineer from Denver, Colorado. I'd love to help you build a website. Have a look at my Python projects.

Poetry

Poetry is a tool for dependency management and packaging in Python. Poetry is an efficient way to create virtual environments and manage dependencies in Python. It allows you to install libraries and frameworks for your project when you create the project. Poetry will manage the installations and updates of these software for you. It keeps track of the dependencies in a lock file, similar to requirements.txt file if you have experience working with pip and virtualenv. I used to work with virtualenv and pipenv when I started with Django until I discovered the benefits of Poetry and how important Poetry is for continuous integration and continuous delivery (CI/CD). Also, you can use Poetry to publish Python packages. But Before we deep-dive into Poetry, we have to understand the virtual environment.

A virtual environment is basically a workspace that allows users to interact both with the operating and resources installed in the workspace. It’s a separate environment from the base operating system and other environments in your computer. It’s used to contain a specific Python interpreter and software libraries to support a project.

I like Poetry because it simplifies a lot of steps and processes of creating virtual environments, adding and removing dependencies. It does a lot of things automatically for you which is important when you need to maintain the application. Another benefit of using Poetry is performance and the great documentations it provides in the lock file. The lock file organizes everything that’s installed in the virtual environment with version numbers and specific details for each dependency. I personally use the lock file as a reference to make sure that I’ve everything the application needs to run properly and try to fix bugs when things go wrong. For example, Django version 5 (5==5.0.2) requires installation of devtools. I install both dependencies ( poetry add django ; poetry add devtools ) before I create a new Django project using the django-admin startproject mysite command using VSCode’s built-in Terminal. But I was still getting an error saying “devtools” was missing even-though the “poetry.lock” shows that devtools has already been installed. I learned from the Python developers community that this was a bug in VSCode’s Terminal. I have to restart VSCode, use the system terminal in my case the OS Terminal or restart the computer to fix the bug. You might not have this bug if you use PyCharm Professional. I like the fact that I can check my work in poetry.lock file when there are dependency issues for the project.

How does Poetry work? The following is instructions on how to use poetry in Mac OS. The setup for Windows and Linux should be similar. Visit Poetry’s website (https://python-poetry.org) to learn more. This is actually the steps I use to create the backend applications in Python and Django. I prepare the application with Poetry before I dockerize with Docker and docker-compose.

* Step 1: Create a new project folder and move inside that directory using the CD command.

“ mkdir full_stack_project ” and “ cd full_stack_project ”

* Step 2 Install Poetry using.

“ pip install poetry ”


* Step 3: Create a toml file (pyproject.toml) for your Project. This file contains key-value pair information about your project. You can create one yourself or use the Poetry init command.

“ poetry init ”. You will be prompted with questions and a guide that will help you create and configure a pyproject.toml.

* Step 4: Create a virtual environment for your project. This command tells poetry to create a new virtual environment within the current working directory, “full_stack_project”

“ poetry config virtualenvs.in-project true ”. Install all the requirments for creating a virtual enviorment in
# this project using the "Poetry" install command


* Step 5: Install all of the requirements and dependencies for the project using the install command.

“ poetry install ”


* Step 6: Activate the virtual environment

“ poetry shell ”

* Step 7: Install django, devtools and the PostgreSQL database adapters for Python.

“ poetry add django ”
“ poetry add devtools ”
“ poetry add psycopg2 ” # Use for production
“ poetry add psycopg2-binary ” # The binary package is for development and testing.

Note: These are the basic commands you need to use poetry. You can visit Poetry’s website and read the documentation for more commands.

* Step 8: Create a new Django project using the django-admin command

“ django-admin startproject backend_django ”

* Step 9: Run Django’s development server and visit localhost port 8000 to make sure the backend for your full stack application is setup properly. Note: Assuming you’re following this instructions to setup a backend for a full stack application. You still have to configure Django to work with PostgreSQL in “settings.py” file and React to work with Django.

“ cd backend_django ”

“ python manage.py runserver ”

Go to “ http://127.0.0.1:8000/ ” in your web browser. You should see the famous “ The install worked successfully! Congratulations! ” from Django.


* Step 10: You can close the virtual environment using the exit or deactivate command

“ exit ”

“ deactivate ”

Read the full article

Published