Skip to content

Instantly share code, notes, and snippets.

@NoahCardoza
Created June 2, 2024 04:13
Show Gist options
  • Save NoahCardoza/fa20b8ecd6c06001a947116ec86bd322 to your computer and use it in GitHub Desktop.
Save NoahCardoza/fa20b8ecd6c06001a947116ec86bd322 to your computer and use it in GitHub Desktop.

This script helps migrate older projects from Pipenv to Poetry without installing new versions of your libraries.

The pipenv-poetry-migrate utility is great, but it doesn't migrate the lock file. This script reads the lock file and pins the versions so you can safely poetry install without getting newer versions of the libraries.

Once you've run the script, verify the contents of pyproject.toml.tmp and if it checks out, replace the original pyproject.toml with mv pyproject.toml.tmp pyproject.toml.

Make sure to install the toml library before running this script.

import json
import toml
# Load Pipfile.lock dependencies
with open('Pipfile.lock') as pipfile:
piplock_data = json.load(pipfile)
piplock_deps = {**piplock_data['default'], **piplock_data['develop']}
# Load pyproject.toml dependencies
with open('pyproject.toml') as pyproject_file:
pyproject_data = toml.load(pyproject_file)
pyproject_deps = pyproject_data['tool']['poetry']['dependencies']
project_dev_deps = pyproject_data['tool']['poetry']['group']['dev']['dependencies']
# Update pyproject.toml dependencies with Pipfile.lock versions
for library in pyproject_deps.keys():
if library in piplock_deps:
pyproject_deps[library] = piplock_deps[library]['version']
for library in project_dev_deps.keys():
if library in piplock_deps:
project_dev_deps[library] = piplock_deps[library]['version']
# Write the updated dependencies back to pyproject.toml
with open('pyproject.toml.tmp', 'w') as pyproject_file:
toml.dump(pyproject_data, pyproject_file)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment