Skip to content

Instantly share code, notes, and snippets.

@sobolevn
Created February 18, 2019 15:34
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save sobolevn/665695b82e4259e9875875167b111e35 to your computer and use it in GitHub Desktop.
Save sobolevn/665695b82e4259e9875875167b111e35 to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
import json
import toml
def read_pipenv_lock():
with open('Pipfile.lock') as lock:
return json.loads(lock.read())
def read_pipenv():
with open('Pipfile') as pipfile:
return toml.loads(pipfile.read())
def convert_version(version):
return version.replace('==', '^')
def to_name_version_notation(pipfile, lock, section):
category = 'packages' if section == 'default' else 'dev-packages'
notations = {}
dependencies = lock[section]
for dep in dependencies:
if dep not in pipfile[category]:
continue
notations.update({
dep: convert_version(dependencies[dep]['version']),
})
return notations
pipfile = read_pipenv()
lock = read_pipenv_lock()
for section in ['default', 'develop']:
print('Section:', section)
current = to_name_version_notation(pipfile, lock, section)
for dep, version in current.items():
print(dep, '=', '"' + version + '"')
print()
@tony
Copy link

tony commented Dec 16, 2019

Nice!

A bit of an issue I have:

django-redis {'hashes': ['sha256:af0b393864e91228dd30d8c85b5c44d670b5524cb161b7f9e41acc98b6e5ace7', 'sha256:f46115577063d00a890867c6964ba096057f07cb756e78e0503b89cd18e4e083'], 'index': 'pypi', 'version': '==4.10.0'}
django-webpack-loader {'editable': True, 'git': 'https://github.com/peergradeio/django-webpack-loader', 'ref': '72065565dc57ad7e7a19a8d933eeb2feb5f17195'}
Traceback (most recent call last):
  File "./pipfile_to_poetry.py", line 39, in <module>
    current = to_name_version_notation(pipfile, lock, section)
  File "./pipfile_to_poetry.py", line 30, in to_name_version_notation
    notations.update({dep: convert_version(dependencies[dep]['version'])})
KeyError: 'version'

@sobolevn
Copy link
Author

@tony I recommend to try dephell instead: https://github.com/dephell/dephell

It will do the same thing.

@tony
Copy link

tony commented Dec 16, 2019

@sobolevn : Thank you!

So it'll be able to convert Pipfile to poetry's pyproject.toml ?

Would it require everyone on the team installing dephell, or just poetry?

@sobolevn
Copy link
Author

You can convert your Pipfile once. And then use just poetry.

@orsinium
Copy link

dephell deps convert --from=Pipfile --to-format poetry --to-path pyproject.toml

And then you have a valid poetry file 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment