Mercurial > hg > orthanc-book
comparison Sphinx/source/plugins/python.rst @ 1018:868552e0caf8
python venv
author | Alain Mazy <am@osimis.io> |
---|---|
date | Thu, 25 Jan 2024 11:33:50 +0100 |
parents | ab270400aae1 |
children | a3436ae3709c |
comparison
equal
deleted
inserted
replaced
1017:3f3a1b54a839 | 1018:868552e0caf8 |
---|---|
1009 <https://requests.readthedocs.io/en/master/>`__, can then be used to | 1009 <https://requests.readthedocs.io/en/master/>`__, can then be used to |
1010 access the REST API of Orthanc. Here is a minimal example: | 1010 access the REST API of Orthanc. Here is a minimal example: |
1011 | 1011 |
1012 .. literalinclude:: python/multiprocessing-4.py | 1012 .. literalinclude:: python/multiprocessing-4.py |
1013 :language: python | 1013 :language: python |
1014 | |
1015 Working with virtual environments | |
1016 --------------------------------- | |
1017 | |
1018 By default, Orthanc uses the system-wide Python installation and | |
1019 therefore has access to the python modules that have been installed | |
1020 system-wide. | |
1021 | |
1022 As of version 4.1 of the python plugin, there is no built-in support | |
1023 for working with a `virtual environment <https://docs.python.org/3/library/venv.html>`__. | |
1024 However, you may modify the python path at the very beginning of the script | |
1025 to instruct python to look for modules in your environment. | |
1026 | |
1027 **Example 1**: On a Linux system, consider that you have created a virtual environment in ``/tmp/.venv`` | |
1028 and you want to use only the modules that have been installed in this virtual environment. | |
1029 In this case, you may simply rewrite ``sys.path``: | |
1030 | |
1031 .. literalinclude:: python/venv-linux.py | |
1032 :language: python | |
1033 | |
1034 | |
1035 **Example 2**: On a Windows system, consider that you have created a virtual environment in ``C:/tmp/.venv/``. | |
1036 Instead of defining ``sys.path`` from scratch, it is possible to simply insert the venv-packages in | |
1037 the ``sys.path``. By adding the ``venv`` to an early index (``0``), any package required by your code | |
1038 will be looked up in the ``venv`` first. And, as a consequence, if the package is not present, the system-wide | |
1039 installation of that package might be loaded: | |
1040 | |
1041 .. literalinclude:: python/venv-windows.py | |
1042 :language: python |