Created
January 10, 2024 07:35
-
-
Save kishorek/2602abe397de5f54bea89a269a923c31 to your computer and use it in GitHub Desktop.
Export poetry dependencies as requirements.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def export_poetry_dependencies(pyproject_path, requirements_path): | |
# Flag to check if we are within the dependencies section | |
in_dependencies = False | |
with open(pyproject_path, "r") as pyproject_file, open( | |
requirements_path, "w" | |
) as requirements_file: | |
for line in pyproject_file: | |
# Check for the start of the dependencies section | |
if line.strip() == "[tool.poetry.dependencies]": | |
in_dependencies = True | |
continue | |
# Check for the end of the dependencies section | |
if in_dependencies and line.strip() == "": | |
break | |
# Write the dependency to requirements.txt | |
if in_dependencies and line.strip() and not line.strip().startswith("#"): | |
# Extracting package and version, assuming format 'package = "version"' | |
parts = line.strip().split("=") | |
if len(parts) == 2: | |
package = parts[0].strip() | |
version = parts[1].strip().strip('"') | |
if package.lower() != "python": | |
requirements_file.write(f"{package}=={version}\n") | |
# Usage | |
export_poetry_dependencies("pyproject.toml", "requirements.txt") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment