Pyramid, Poetry, and Track Switching: Lessons Learned Migrating to Railway

I heard about Railway from a associate several months ago, and recently was invited to interview with them. In the process of doing my homework to make sure I had an understanding of how their stack works before I go in, I decided to migrate two of my sites to their platform. These sites were written in Python using Pyramid. It's a more obscure framework, but it's what I'm learning Python on. Now, I learned a lot tonight figuring out how to migrate my code, and I'll go ahead and share what I found with you.

setup.py is out, pyproject.toml is in

Railway uses Nixpacks to automate the build process of the container your application will eventually live in. The Python Nixpack supports three methods for handling dependencies and execution, we're going to use the easiest option, called Poetry. Poetry looks to be the future successor of setuptools, so go ahead and install it, then cd into your source directory and run poetry init. It will ask for the following bits of information:

  • Name: This should be the name of the directory your code lives in (the one with the views, templates, etc. directories)
  • Version: Whatever you want.
  • Description: Whatever you want.
  • Author: It will suck in whatever you have set in your Git config, you can change it to whatever you'd like though.
  • License: Whatever suits your fancy.
  • Compatible Python versions: Pay attention to this. You can go with the default (as long as it's 3.11 or lower), just make a note of it.

Once you have done that, it'll ask you about your dependencies. I had no issues with just typing in the dependencies from setup.py and letting it search the PyPI for results. Wrap up the wizard and it will spit out two new files: pyproject.toml and poetry.lock. Ignore the second one, and open the first. We'll need to make a few quick changes.

  1. Under [tool.poetry], edit the readme setting to match your README file, whether it be plain text (.txt) or Markdown (.md).
  2. After the [tool.poetry.dependencies] section, add the following code block:
[tool.poetry.plugins."paste.app_factory"]
main = "name_of_project:main"

Replace name_of_project with the name of the code folder (views, templates, etc.). Once you have done that, make sure to run poetry lock, commit the two new files and we'll focus on some stuff with the Railway end of things.

Choo Choo

Railway requires a couple of enviroment variables to be set in order for everything to behave, on top of whatever variables your application may require.

  • NIXPACKS_PYTHON_VERSION: This should match what you specified in the Poetry configuration wizard. If you don't set this, the Nixpack will default to Python 3.8.
  • NIXPACKS_POETRY_VERSION: This one tripped me up for a while. Make sure you set it to match the output of poetry --version on your local machine, or else it may not be able to properly read the poetry.lock file. By default it uses a much older version of Poetry.
  • PORT: This one should match your HTTP port, by default 6543.

Once everything has been set, you should be good to go! Happy Railroading! 🚂

links

social