Debian Codegraph @

docs/source/cookbook/app_upgrade.rst

raw
1 ===========
2 App Upgrade
3 ===========
4
5 It's best practice for users to install python-based CLIs via pipx_, where each application gets it's own python virtual environment.
6 Whether done via ``pipx`` or standard ``pip``, updating your application can be done via the ``upgrade`` command. i.e.:
7
8 .. code-block:: console
9
10 $ pipx upgrade mypackage
11
12 If you would like your CLI application to be able to upgrade itself, you can add the following command to your application:
13
14 .. code-block:: python
15
16 import mypackage
17 import subprocess
18 import sys
19 from cyclopts import App
20
21 app = App()
22
23 @app.command
24 def upgrade():
25 """Update mypackage to latest stable version."""
26 old_version = mypackage.__version__
27 subprocess.check_output([sys.executable, "-m", "pip", "install", "--upgrade", "pip"])
28 subprocess.check_output([sys.executable, "-m", "pip", "install", "--upgrade", "mypackage"])
29 res = subprocess.run([sys.executable, "-m", "mypackage", "--version"], stdout=subprocess.PIPE, check=True)
30 new_version = res.stdout.decode().strip()
31 if old_version == new_version:
32 print(f"mypackage up-to-date (v{new_version}).")
33 else:
34 print(f"mypackage updated from v{old_version} to v{new_version}.")
35
36 app()
37
38 :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.
39
40 .. _pipx: https://github.com/pypa/pipx