Code Monkey home page Code Monkey logo

poetry-auto-export's Introduction

poetry-auto-export

Automatically export dependencies to requirements.txt on every poetry lock

This plugin enables you to keep a requirements.txt file always up to date with your poetry.lock file. This is helpful if you want to use pip to install your dependencies somewhere, e.g.

  • in CI/CD environments
  • in docker containers

Usage

Add plugin configuration to your pyproject.toml. The options follow arguments for poetry export.

[tool.poetry-auto-export]
output = "requirements.txt"
without_hashes = true
without = ["dev"]

Then, run poetry commands as usual:

poetry lock

The requirements.txt file will be updated automatically.

The supported commands are:

  • lock
  • update
  • add
  • remove

Creating multiple export files

If you need to create multiple requirements files, e.g. dev-requirements.txt and prod-requirements.txt, use the following syntax:

[[tool.poetry-auto-export.exports]]
output = "dev-requirements.txt"
without = ["prod"]

[[tool.poetry-auto-export.exports]]
output = "prod-requirements.txt"
without = ["dev"]

Installation

This is a poetry plugin, so it's meant to be installed inside the global poetry environment, not your project environment like regular pacakges. See poetry's docs.

There are three ways of doing so.

The pipx way

If you are using pipx already, that's easy:

pipx inject poetry poetry-auto-export

The pip way

Otherwise, you can use the pip that comes with poetry. The difficulty is finding that pip! It's not the same one you get with poetry run pip. Here is a best effort guess:

On Linux:

~/.local/share/pypoetry/venv/bin/pip install poetry-auto-export

On Windows:

%APPDATA%\pypoetry\venv\bin\pip install poetry-auto-export

The poetry way

For some reason, this is discouraged by poetry.

poetry self add poetry-auto-export

Roadmap and contributing

The primary goal of the project is to make it more convenient to work with poetry projects in CI/CD and docker. Contributions towards this goal are welcome!

Roadmap:

  • more unit tests for the plugin
  • integration tests for the plugin
  • proper configuration parsing (note: typed dict and dataclasses can't support the with option, since it's a python keyword)
  • schema or exhaustive documentation of the supported configuration options

poetry-auto-export's People

Contributors

hubert-springbok avatar ddedalus avatar dependabot[bot] avatar

Stargazers

永格天 avatar  avatar Peter avatar  avatar  avatar Manuel Zimmermann avatar Roan Song avatar Juan A. S. avatar Sean avatar William Hayes avatar  avatar

Watchers

 avatar

poetry-auto-export's Issues

Poetry can't run in directories without `pyproject.toml` after installing plugin

I started to use this plugin midway in my previous project and it worked very well.
But when I tried to create a new project, an error poped up.

Poetry could not find a pyproject.toml file in <CURRENT DIRECTORY> or its parents

Before installing the plugin, this only occorred when I typed somthing like poetry version where pyproject.toml was necessary.
In this case I simply typed the poetry command, just to see the help messages.

Stack trace (-vvv):

 Stack trace:
  8  ~/.local/pipx/venvs/poetry/lib/python3.12/site-packages/cleo/application.py:327 in run
      325│ 
      326│             try:
    → 327│                 exit_code = self._run(io)
      328│             except BrokenPipeError:
      329│                 # If we are piped to another process, it may close early and send a
  7  ~/.local/pipx/venvs/poetry/lib/python3.12/site-packages/poetry/console/application.py:188 in _run
      186│         self._disable_cache = io.input.has_parameter_option("--no-cache")
      187│ 
    → 188│         self._load_plugins(io)
      189│ 
      190│         exit_code: int = super()._run(io)
  6  ~/.local/pipx/venvs/poetry/lib/python3.12/site-packages/poetry/console/application.py:355 in _load_plugins
      353│             manager = PluginManager(ApplicationPlugin.group)
      354│             manager.load_plugins()
    → 355│             manager.activate(self)
      356│ 
      357│             # We have to override the command from poetry-plugin-export
  5  ~/.local/pipx/venvs/poetry/lib/python3.12/site-packages/poetry/plugins/plugin_manager.py:71 in activate
       69│     def activate(self, *args: Any, **kwargs: Any) -> None:
       70│         for plugin in self._plugins:
    →  71│             plugin.activate(*args, **kwargs)
       72│ 
       73│     def _load_plugin_entry_point(self, ep: metadata.EntryPoint) -> None:
  4  ~/.local/pipx/venvs/poetry/lib/python3.12/site-packages/poetry_auto_export/plugin.py:22 in activate
       20│         if not application.event_dispatcher:
       21│             return
    →  22│         self.configs = self._parse_pyproject(application.poetry.pyproject.data)
       23│         application.event_dispatcher.add_listener(TERMINATE, self.run_exports)
       24│         return super().activate(application)
  3  ~/.local/pipx/venvs/poetry/lib/python3.12/site-packages/poetry/console/application.py:129 in poetry
      127│             project_path = self._io.input.option("directory")
      128│ 
    → 129│         self._poetry = Factory().create_poetry(
      130│             cwd=project_path,
      131│             io=self._io,
  2  ~/.local/pipx/venvs/poetry/lib/python3.12/site-packages/poetry/factory.py:58 in create_poetry
       56│             io = NullIO()
       57│ 
    →  58│         base_poetry = super().create_poetry(cwd=cwd, with_groups=with_groups)
       59│ 
       60│         poetry_file = base_poetry.pyproject_path
  1  ~/.local/pipx/venvs/poetry/lib/python3.12/site-packages/poetry/core/factory.py:50 in create_poetry
       48│         from poetry.core.pyproject.toml import PyProjectTOML
       49│ 
    →  50│         poetry_file = self.locate(cwd)
       51│         local_config = PyProjectTOML(path=poetry_file).poetry_config
       52│ 
  RuntimeError
  Poetry could not find a pyproject.toml file in <CURRENT DIRECTORY> or its parents
  at ~/.local/pipx/venvs/poetry/lib/python3.12/site-packages/poetry/core/factory.py:507 in locate
      503│             if poetry_file.exists():
      504│                 return poetry_file
      505│ 
      506│         else:
    → 507│             raise RuntimeError(
      508│                 f"Poetry could not find a pyproject.toml file in {cwd} or its parents"
      509│             )
      510│ 

Possible solution

It seem like the problem is related to parsing the config in activate()
If the parsing of config is delayed (ex: parse the config in run_exports), the problem sould be solved :)

Not creating the requirements.txt file

Summary

requirements.txt file was not being auto-generated when running poetry lock after installing the plugin

Background:

  1. Setup the export in the pyproject.toml file like so:
[[tool.poetry-auto-export.exports]]
output = "requirements-dev.txt"
without_hashes = true


[[tool.poetry-auto-export.exports]]
output = "requirements.txt"
without = ["dev"]
without_hashes = true
  1. Installed the plugin via pipx inject poetry poetry-auto-export

  2. Checked that the plugin was installed using poetry self show plugins

Result:

Was not getting the requirements.txt and requirements-dev.txt files being generated when running poetry lock

Potential fix:

When I commented out the following lines in the plugin, it worked fine. I'm not sure why that check exists.

https://github.com/Ddedalus/poetry-auto-export/blob/ce6593567a617ec88878442134b9d330310b82db/poetry_auto_export/plugin.py#L31C1-L32C27

PS - thank you so much for creating this plugin - I'd finally gotten fed up after forgetting to update this way too many times when working with docker and your plugin magically showed up when I did a search for something like this.

Allow multiple exports

Hi, first of all, thanks for this plugin, I have been long waiting for this. I would suggest to add multiple export configurations. In our case, we have different requirements for regular building (no dev dependencies) and for testing (that includes dev dependencies).

This is our current structure, that could be similar to other projects:

.
├── README.md
├── poetry.lock
├── pyproject.toml
├── src
│   ├── my_package
│   │   ├── __init__.py
│   │   ├── package_1
│   │   │   ├── __init__.py
│   │   │   └── app.py
│   └── requirements.txt
└── tests
    ├── requirements-dev.txt
    └── unit
        └── test_1.py

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.