File: app_upgrade.rst

package info (click to toggle)
python-cyclopts 3.12.0-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 3,288 kB
  • sloc: python: 11,445; makefile: 24
file content (40 lines) | stat: -rw-r--r-- 1,523 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
===========
App Upgrade
===========

It's best practice for users to install python-based CLIs via pipx_, where each application gets it's own python virtual environment.
Whether done via ``pipx`` or standard ``pip``, updating your application can be done via the ``upgrade`` command. i.e.:

.. code-block:: console

   $ pipx upgrade mypackage

If you would like your CLI application to be able to upgrade itself, you can add the following command to your application:

.. code-block:: python

   import mypackage
   import subprocess
   import sys
   from cyclopts import App

   app = App()

   @app.command
   def upgrade():
       """Update mypackage to latest stable version."""
       old_version = mypackage.__version__
       subprocess.check_output([sys.executable, "-m", "pip", "install", "--upgrade", "pip"])
       subprocess.check_output([sys.executable, "-m", "pip", "install", "--upgrade", "mypackage"])
       res = subprocess.run([sys.executable, "-m", "mypackage", "--version"], stdout=subprocess.PIPE, check=True)
       new_version = res.stdout.decode().strip()
       if old_version == new_version:
           print(f"mypackage up-to-date (v{new_version}).")
       else:
           print(f"mypackage updated from v{old_version} to v{new_version}.")

   app()

:obj:`sys.executable` points to the currently used python interpreter's path; if your package was installed via pipx_, then it points to the python interpreter in it's respective virtual environment.

.. _pipx: https://github.com/pypa/pipx