1

I'm currently trying Poetry to manage my dependencies, for a project using argparse and argcomplete. However, I can't figure out how to handle autocompletion with the run command of Poetry. I have a command mycommand which I can run with poetry run mycommand, but the autocompletion does not works. But it works when I create a virtual environnement with venv and do python main.py (which runs mycommand). In both cases I did activate-global-python-argcomplete before.

So is there any way to make the autocompletion work with Poetry? Also, will it still work if I compile the Python source to an executable (e.g. with pyinstaller)?

Smack Alpha
  • 1,828
  • 1
  • 17
  • 37
Michael Marx
  • 104
  • 8

1 Answers1

0

I ran into the same issue while trying to use argcomplete for the first time, but I found a way to make it work.

I assume that like me, you followed the README and used the script activate-global-python-argcomplete. This script will enable autocompletion on any script that contains PYTHON_ARGCOMPLETE_OK in the fist 1024 characters of the file. However, when you run it with poetry (or from an packaged executable), the script being scanned will not be yours but poetry's executable file (or a small file wrapper in the executable case).

Solution 1

However, you can make it work by registering your command specifically using register-python-argcomplete like this:

register-python-argcomplete my-awesome-command > ~/.bash_completion.d/my-awesome-command

(replace my-awesome-command with the name of your own awesome command)

And then add this to your .bashrc

source ~/.bash_completion.d/my-awesome-command

This worked for me.

Solution 2

I also found a kind of hack that worked to make the executable packaged by poetry compatible with argcomplete's global completion activation:

For this, I renamed my main method to main_PYTHON_ARGCOMPLETE_OK, and I updated my pyproject.toml.

[tool.poetry.scripts]
  # BEFORE
  # my-awesome-command = 'my.awesome.lib:main'
  # AFTER
  my-awesome-command = 'my.awesome.lib:main_PYTHON_ARGCOMPLETE_OK'

This made sure that the wrapper script generated by poetry contained the string PYTHON_ARGCOMPLETE_OK, which did the trick ;-)

FurryMachine
  • 1,543
  • 14
  • 12